diff --git a/src/bitmap_font.h b/src/bitmap_font.h index ef99d6f..6f3fe94 100644 --- a/src/bitmap_font.h +++ b/src/bitmap_font.h @@ -5,32 +5,29 @@ #define _X49GP_BITMAP_FONT_H 1 typedef struct { - const char *name; - int width; - int kern; - int ascent; - int descent; - const unsigned char *bits; + const char* name; + int width; + int kern; + int ascent; + int descent; + const unsigned char* bits; } bitmap_glyph_t; typedef struct { - int ascent; - int descent; - bitmap_glyph_t glyphs[]; + int ascent; + int descent; + bitmap_glyph_t glyphs[]; } bitmap_font_t; -#define GLYPH(font, name) \ - { \ - #name, \ - font##_##name##_width - font##_##name##_x_hot, \ - -font##_##name##_x_hot, \ - font##_##name##_y_hot + 1, \ - font##_##name##_y_hot + 1 - font##_##name##_height, \ - font##_##name##_bits \ - } +#define GLYPH( font, name ) \ + { #name, \ + font##_##name##_width - font##_##name##_x_hot, \ + -font##_##name##_x_hot, \ + font##_##name##_y_hot + 1, \ + font##_##name##_y_hot + 1 - font##_##name##_height, \ + font##_##name##_bits } -#define SPACE(name, width, kern) \ - { name, width, kern, 0, 0, NULL } +#define SPACE( name, width, kern ) { name, width, kern, 0, 0, NULL } extern const bitmap_font_t tiny_font; diff --git a/src/block-qcow.c b/src/block-qcow.c index 800c4e1..7df4634 100644 --- a/src/block-qcow.c +++ b/src/block-qcow.c @@ -1,8 +1,8 @@ /* * Block driver for the QCOW format - * + * * Copyright (c) 2004-2006 Fabrice Bellard - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -26,19 +26,19 @@ #include "block_int.h" #include #if 0 -#include "aes.h" +# include "aes.h" #endif /**************************************************************/ /* QEMU COW block driver with compression and encryption support */ -#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) +#define QCOW_MAGIC ( ( 'Q' << 24 ) | ( 'F' << 16 ) | ( 'I' << 8 ) | 0xfb ) #define QCOW_VERSION 1 #define QCOW_CRYPT_NONE 0 -#define QCOW_CRYPT_AES 1 +#define QCOW_CRYPT_AES 1 -#define QCOW_OFLAG_COMPRESSED (1LL << 63) +#define QCOW_OFLAG_COMPRESSED ( 1LL << 63 ) typedef struct QCowHeader { uint32_t magic; @@ -56,7 +56,7 @@ typedef struct QCowHeader { #define L2_CACHE_SIZE 16 typedef struct BDRVQcowState { - BlockDriverState *hd; + BlockDriverState* hd; int cluster_bits; int cluster_size; int cluster_sectors; @@ -65,106 +65,104 @@ typedef struct BDRVQcowState { int l1_size; uint64_t cluster_offset_mask; uint64_t l1_table_offset; - uint64_t *l1_table; - uint64_t *l2_cache; - uint64_t l2_cache_offsets[L2_CACHE_SIZE]; - uint32_t l2_cache_counts[L2_CACHE_SIZE]; - uint8_t *cluster_cache; - uint8_t *cluster_data; + uint64_t* l1_table; + uint64_t* l2_cache; + uint64_t l2_cache_offsets[ L2_CACHE_SIZE ]; + uint32_t l2_cache_counts[ L2_CACHE_SIZE ]; + uint8_t* cluster_cache; + uint8_t* cluster_data; uint64_t cluster_cache_offset; } BDRVQcowState; -static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); +static int decompress_cluster( BDRVQcowState* s, uint64_t cluster_offset ); -static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) +static int qcow_probe( const uint8_t* buf, int buf_size, const char* filename ) { - const QCowHeader *cow_header = (const void *)buf; - - if (buf_size >= sizeof(QCowHeader) && - be32_to_cpu(cow_header->magic) == QCOW_MAGIC && - be32_to_cpu(cow_header->version) == QCOW_VERSION) + const QCowHeader* cow_header = ( const void* )buf; + + if ( buf_size >= sizeof( QCowHeader ) && be32_to_cpu( cow_header->magic ) == QCOW_MAGIC && + be32_to_cpu( cow_header->version ) == QCOW_VERSION ) return 100; else return 0; } -static int qcow_open(BlockDriverState *bs, const char *filename, int flags) +static int qcow_open( BlockDriverState* bs, const char* filename, int flags ) { - BDRVQcowState *s = bs->opaque; + BDRVQcowState* s = bs->opaque; int len, i, shift, ret; QCowHeader header; - ret = bdrv_file_open(&s->hd, filename, flags); - if (ret < 0) + ret = bdrv_file_open( &s->hd, filename, flags ); + if ( ret < 0 ) return ret; - if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) + if ( bdrv_pread( s->hd, 0, &header, sizeof( header ) ) != sizeof( header ) ) goto fail; - be32_to_cpus(&header.magic); - be32_to_cpus(&header.version); - be64_to_cpus(&header.backing_file_offset); - be32_to_cpus(&header.backing_file_size); - be32_to_cpus(&header.mtime); - be64_to_cpus(&header.size); - be32_to_cpus(&header.crypt_method); - be64_to_cpus(&header.l1_table_offset); - - if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) + be32_to_cpus( &header.magic ); + be32_to_cpus( &header.version ); + be64_to_cpus( &header.backing_file_offset ); + be32_to_cpus( &header.backing_file_size ); + be32_to_cpus( &header.mtime ); + be64_to_cpus( &header.size ); + be32_to_cpus( &header.crypt_method ); + be64_to_cpus( &header.l1_table_offset ); + + if ( header.magic != QCOW_MAGIC || header.version != QCOW_VERSION ) goto fail; - if (header.size <= 1 || header.cluster_bits < 9) + if ( header.size <= 1 || header.cluster_bits < 9 ) goto fail; - if (header.crypt_method > QCOW_CRYPT_AES) + if ( header.crypt_method > QCOW_CRYPT_AES ) goto fail; s->cluster_bits = header.cluster_bits; s->cluster_size = 1 << s->cluster_bits; - s->cluster_sectors = 1 << (s->cluster_bits - 9); + s->cluster_sectors = 1 << ( s->cluster_bits - 9 ); s->l2_bits = header.l2_bits; s->l2_size = 1 << s->l2_bits; bs->total_sectors = header.size / 512; - s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1; + s->cluster_offset_mask = ( 1LL << ( 63 - s->cluster_bits ) ) - 1; /* read the level 1 table */ shift = s->cluster_bits + s->l2_bits; - s->l1_size = (header.size + (1LL << shift) - 1) >> shift; + s->l1_size = ( header.size + ( 1LL << shift ) - 1 ) >> shift; s->l1_table_offset = header.l1_table_offset; - s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); - if (!s->l1_table) + s->l1_table = qemu_malloc( s->l1_size * sizeof( uint64_t ) ); + if ( !s->l1_table ) goto fail; - if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != - s->l1_size * sizeof(uint64_t)) + if ( bdrv_pread( s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof( uint64_t ) ) != s->l1_size * sizeof( uint64_t ) ) goto fail; - for(i = 0;i < s->l1_size; i++) { - be64_to_cpus(&s->l1_table[i]); + for ( i = 0; i < s->l1_size; i++ ) { + be64_to_cpus( &s->l1_table[ i ] ); } /* alloc L2 cache */ - s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); - if (!s->l2_cache) + s->l2_cache = qemu_malloc( s->l2_size * L2_CACHE_SIZE * sizeof( uint64_t ) ); + if ( !s->l2_cache ) goto fail; - s->cluster_cache = qemu_malloc(s->cluster_size); - if (!s->cluster_cache) + s->cluster_cache = qemu_malloc( s->cluster_size ); + if ( !s->cluster_cache ) goto fail; - s->cluster_data = qemu_malloc(s->cluster_size); - if (!s->cluster_data) + s->cluster_data = qemu_malloc( s->cluster_size ); + if ( !s->cluster_data ) goto fail; s->cluster_cache_offset = -1; - + /* read the backing file name */ - if (header.backing_file_offset != 0) { + if ( header.backing_file_offset != 0 ) { len = header.backing_file_size; - if (len > 1023) + if ( len > 1023 ) len = 1023; - if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len) + if ( bdrv_pread( s->hd, header.backing_file_offset, bs->backing_file, len ) != len ) goto fail; - bs->backing_file[len] = '\0'; + bs->backing_file[ len ] = '\0'; } return 0; - fail: - qemu_free(s->l1_table); - qemu_free(s->l2_cache); - qemu_free(s->cluster_cache); - qemu_free(s->cluster_data); - bdrv_delete(s->hd); +fail: + qemu_free( s->l1_table ); + qemu_free( s->l2_cache ); + qemu_free( s->cluster_cache ); + qemu_free( s->cluster_data ); + bdrv_delete( s->hd ); return -1; } @@ -177,174 +175,157 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) * * 2 to allocate a compressed cluster of size * 'compressed_size'. 'compressed_size' must be > 0 and < - * cluster_size + * cluster_size * * return 0 if not allocated. */ -static uint64_t get_cluster_offset(BlockDriverState *bs, - uint64_t offset, int allocate, - int compressed_size, - int n_start, int n_end) +static uint64_t get_cluster_offset( BlockDriverState* bs, uint64_t offset, int allocate, int compressed_size, int n_start, int n_end ) { - BDRVQcowState *s = bs->opaque; + BDRVQcowState* s = bs->opaque; int min_index, i, j, l1_index, l2_index; uint64_t l2_offset, *l2_table, cluster_offset, tmp; uint32_t min_count; int new_l2_table; - - l1_index = offset >> (s->l2_bits + s->cluster_bits); - l2_offset = s->l1_table[l1_index]; + + l1_index = offset >> ( s->l2_bits + s->cluster_bits ); + l2_offset = s->l1_table[ l1_index ]; new_l2_table = 0; - if (!l2_offset) { - if (!allocate) + if ( !l2_offset ) { + if ( !allocate ) return 0; /* allocate a new l2 entry */ - l2_offset = bdrv_getlength(s->hd); + l2_offset = bdrv_getlength( s->hd ); /* round to cluster size */ - l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); + l2_offset = ( l2_offset + s->cluster_size - 1 ) & ~( s->cluster_size - 1 ); /* update the L1 entry */ - s->l1_table[l1_index] = l2_offset; - tmp = cpu_to_be64(l2_offset); - if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), - &tmp, sizeof(tmp)) != sizeof(tmp)) + s->l1_table[ l1_index ] = l2_offset; + tmp = cpu_to_be64( l2_offset ); + if ( bdrv_pwrite( s->hd, s->l1_table_offset + l1_index * sizeof( tmp ), &tmp, sizeof( tmp ) ) != sizeof( tmp ) ) return 0; new_l2_table = 1; } - for(i = 0; i < L2_CACHE_SIZE; i++) { - if (l2_offset == s->l2_cache_offsets[i]) { + for ( i = 0; i < L2_CACHE_SIZE; i++ ) { + if ( l2_offset == s->l2_cache_offsets[ i ] ) { /* increment the hit count */ - if (++s->l2_cache_counts[i] == 0xffffffff) { - for(j = 0; j < L2_CACHE_SIZE; j++) { - s->l2_cache_counts[j] >>= 1; + if ( ++s->l2_cache_counts[ i ] == 0xffffffff ) { + for ( j = 0; j < L2_CACHE_SIZE; j++ ) { + s->l2_cache_counts[ j ] >>= 1; } } - l2_table = s->l2_cache + (i << s->l2_bits); + l2_table = s->l2_cache + ( i << s->l2_bits ); goto found; } } /* not found: load a new entry in the least used one */ min_index = 0; min_count = 0xffffffff; - for(i = 0; i < L2_CACHE_SIZE; i++) { - if (s->l2_cache_counts[i] < min_count) { - min_count = s->l2_cache_counts[i]; + for ( i = 0; i < L2_CACHE_SIZE; i++ ) { + if ( s->l2_cache_counts[ i ] < min_count ) { + min_count = s->l2_cache_counts[ i ]; min_index = i; } } - l2_table = s->l2_cache + (min_index << s->l2_bits); - if (new_l2_table) { - memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); - if (bdrv_pwrite(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != - s->l2_size * sizeof(uint64_t)) + l2_table = s->l2_cache + ( min_index << s->l2_bits ); + if ( new_l2_table ) { + memset( l2_table, 0, s->l2_size * sizeof( uint64_t ) ); + if ( bdrv_pwrite( s->hd, l2_offset, l2_table, s->l2_size * sizeof( uint64_t ) ) != s->l2_size * sizeof( uint64_t ) ) return 0; } else { - if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != - s->l2_size * sizeof(uint64_t)) + if ( bdrv_pread( s->hd, l2_offset, l2_table, s->l2_size * sizeof( uint64_t ) ) != s->l2_size * sizeof( uint64_t ) ) return 0; } - s->l2_cache_offsets[min_index] = l2_offset; - s->l2_cache_counts[min_index] = 1; - found: - l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); - cluster_offset = be64_to_cpu(l2_table[l2_index]); - if (!cluster_offset || - ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) { - if (!allocate) + s->l2_cache_offsets[ min_index ] = l2_offset; + s->l2_cache_counts[ min_index ] = 1; +found: + l2_index = ( offset >> s->cluster_bits ) & ( s->l2_size - 1 ); + cluster_offset = be64_to_cpu( l2_table[ l2_index ] ); + if ( !cluster_offset || ( ( cluster_offset & QCOW_OFLAG_COMPRESSED ) && allocate == 1 ) ) { + if ( !allocate ) return 0; /* allocate a new cluster */ - if ((cluster_offset & QCOW_OFLAG_COMPRESSED) && - (n_end - n_start) < s->cluster_sectors) { + if ( ( cluster_offset & QCOW_OFLAG_COMPRESSED ) && ( n_end - n_start ) < s->cluster_sectors ) { /* if the cluster is already compressed, we must decompress it in the case it is not completely overwritten */ - if (decompress_cluster(s, cluster_offset) < 0) + if ( decompress_cluster( s, cluster_offset ) < 0 ) return 0; - cluster_offset = bdrv_getlength(s->hd); - cluster_offset = (cluster_offset + s->cluster_size - 1) & - ~(s->cluster_size - 1); + cluster_offset = bdrv_getlength( s->hd ); + cluster_offset = ( cluster_offset + s->cluster_size - 1 ) & ~( s->cluster_size - 1 ); /* write the cluster content */ - if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) != - s->cluster_size) + if ( bdrv_pwrite( s->hd, cluster_offset, s->cluster_cache, s->cluster_size ) != s->cluster_size ) return -1; } else { - cluster_offset = bdrv_getlength(s->hd); - if (allocate == 1) { + cluster_offset = bdrv_getlength( s->hd ); + if ( allocate == 1 ) { /* round to cluster size */ - cluster_offset = (cluster_offset + s->cluster_size - 1) & - ~(s->cluster_size - 1); - bdrv_truncate(s->hd, cluster_offset + s->cluster_size); + cluster_offset = ( cluster_offset + s->cluster_size - 1 ) & ~( s->cluster_size - 1 ); + bdrv_truncate( s->hd, cluster_offset + s->cluster_size ); } else { - cluster_offset |= QCOW_OFLAG_COMPRESSED | - (uint64_t)compressed_size << (63 - s->cluster_bits); + cluster_offset |= QCOW_OFLAG_COMPRESSED | ( uint64_t )compressed_size << ( 63 - s->cluster_bits ); } } /* update L2 table */ - tmp = cpu_to_be64(cluster_offset); - l2_table[l2_index] = tmp; - if (bdrv_pwrite(s->hd, - l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp)) + tmp = cpu_to_be64( cluster_offset ); + l2_table[ l2_index ] = tmp; + if ( bdrv_pwrite( s->hd, l2_offset + l2_index * sizeof( tmp ), &tmp, sizeof( tmp ) ) != sizeof( tmp ) ) return 0; } return cluster_offset; } -static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, - int nb_sectors, int *pnum) +static int qcow_is_allocated( BlockDriverState* bs, int64_t sector_num, int nb_sectors, int* pnum ) { - BDRVQcowState *s = bs->opaque; + BDRVQcowState* s = bs->opaque; int index_in_cluster, n; uint64_t cluster_offset; - cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); - index_in_cluster = sector_num & (s->cluster_sectors - 1); + cluster_offset = get_cluster_offset( bs, sector_num << 9, 0, 0, 0, 0 ); + index_in_cluster = sector_num & ( s->cluster_sectors - 1 ); n = s->cluster_sectors - index_in_cluster; - if (n > nb_sectors) + if ( n > nb_sectors ) n = nb_sectors; *pnum = n; - return (cluster_offset != 0); + return ( cluster_offset != 0 ); } -static int decompress_buffer(uint8_t *out_buf, int out_buf_size, - const uint8_t *buf, int buf_size) +static int decompress_buffer( uint8_t* out_buf, int out_buf_size, const uint8_t* buf, int buf_size ) { z_stream strm1, *strm = &strm1; int ret, out_len; - memset(strm, 0, sizeof(*strm)); + memset( strm, 0, sizeof( *strm ) ); - strm->next_in = (uint8_t *)buf; + strm->next_in = ( uint8_t* )buf; strm->avail_in = buf_size; strm->next_out = out_buf; strm->avail_out = out_buf_size; - ret = inflateInit2(strm, -12); - if (ret != Z_OK) + ret = inflateInit2( strm, -12 ); + if ( ret != Z_OK ) return -1; - ret = inflate(strm, Z_FINISH); + ret = inflate( strm, Z_FINISH ); out_len = strm->next_out - out_buf; - if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || - out_len != out_buf_size) { - inflateEnd(strm); + if ( ( ret != Z_STREAM_END && ret != Z_BUF_ERROR ) || out_len != out_buf_size ) { + inflateEnd( strm ); return -1; } - inflateEnd(strm); + inflateEnd( strm ); return 0; } - -static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) + +static int decompress_cluster( BDRVQcowState* s, uint64_t cluster_offset ) { int ret, csize; uint64_t coffset; coffset = cluster_offset & s->cluster_offset_mask; - if (s->cluster_cache_offset != coffset) { - csize = cluster_offset >> (63 - s->cluster_bits); - csize &= (s->cluster_size - 1); - ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize); - if (ret != csize) + if ( s->cluster_cache_offset != coffset ) { + csize = cluster_offset >> ( 63 - s->cluster_bits ); + csize &= ( s->cluster_size - 1 ); + ret = bdrv_pread( s->hd, coffset, s->cluster_data, csize ); + if ( ret != csize ) return -1; - if (decompress_buffer(s->cluster_cache, s->cluster_size, - s->cluster_data, csize) < 0) { + if ( decompress_buffer( s->cluster_cache, s->cluster_size, s->cluster_data, csize ) < 0 ) { return -1; } s->cluster_cache_offset = coffset; @@ -352,35 +333,34 @@ static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) return 0; } -static int qcow_read(BlockDriverState *bs, int64_t sector_num, - uint8_t *buf, int nb_sectors) +static int qcow_read( BlockDriverState* bs, int64_t sector_num, uint8_t* buf, int nb_sectors ) { - BDRVQcowState *s = bs->opaque; + BDRVQcowState* s = bs->opaque; int ret, index_in_cluster, n; uint64_t cluster_offset; - - while (nb_sectors > 0) { - cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); - index_in_cluster = sector_num & (s->cluster_sectors - 1); + + while ( nb_sectors > 0 ) { + cluster_offset = get_cluster_offset( bs, sector_num << 9, 0, 0, 0, 0 ); + index_in_cluster = sector_num & ( s->cluster_sectors - 1 ); n = s->cluster_sectors - index_in_cluster; - if (n > nb_sectors) + if ( n > nb_sectors ) n = nb_sectors; - if (!cluster_offset) { - if (bs->backing_hd) { + if ( !cluster_offset ) { + if ( bs->backing_hd ) { /* read from the base image */ - ret = bdrv_read(bs->backing_hd, sector_num, buf, n); - if (ret < 0) + ret = bdrv_read( bs->backing_hd, sector_num, buf, n ); + if ( ret < 0 ) return -1; } else { - memset(buf, 0, 512 * n); + memset( buf, 0, 512 * n ); } - } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { - if (decompress_cluster(s, cluster_offset) < 0) + } else if ( cluster_offset & QCOW_OFLAG_COMPRESSED ) { + if ( decompress_cluster( s, cluster_offset ) < 0 ) return -1; - memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); + memcpy( buf, s->cluster_cache + index_in_cluster * 512, 512 * n ); } else { - ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); - if (ret != n * 512) + ret = bdrv_pread( s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512 ); + if ( ret != n * 512 ) return -1; } nb_sectors -= n; @@ -390,27 +370,24 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num, return 0; } -static int qcow_write(BlockDriverState *bs, int64_t sector_num, - const uint8_t *buf, int nb_sectors) +static int qcow_write( BlockDriverState* bs, int64_t sector_num, const uint8_t* buf, int nb_sectors ) { - BDRVQcowState *s = bs->opaque; + BDRVQcowState* s = bs->opaque; int ret, index_in_cluster, n; uint64_t cluster_offset; - - while (nb_sectors > 0) { - index_in_cluster = sector_num & (s->cluster_sectors - 1); + + while ( nb_sectors > 0 ) { + index_in_cluster = sector_num & ( s->cluster_sectors - 1 ); n = s->cluster_sectors - index_in_cluster; - if (n > nb_sectors) + if ( n > nb_sectors ) n = nb_sectors; - cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, - index_in_cluster, - index_in_cluster + n); - if (!cluster_offset) + cluster_offset = get_cluster_offset( bs, sector_num << 9, 1, 0, index_in_cluster, index_in_cluster + n ); + if ( !cluster_offset ) return -1; - { - ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); + { + ret = bdrv_pwrite( s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512 ); } - if (ret != n * 512) + if ( ret != n * 512 ) return -1; nb_sectors -= n; sector_num += n; @@ -420,76 +397,75 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num, return 0; } -static void qcow_close(BlockDriverState *bs) +static void qcow_close( BlockDriverState* bs ) { - BDRVQcowState *s = bs->opaque; - qemu_free(s->l1_table); - qemu_free(s->l2_cache); - qemu_free(s->cluster_cache); - qemu_free(s->cluster_data); - bdrv_delete(s->hd); + BDRVQcowState* s = bs->opaque; + qemu_free( s->l1_table ); + qemu_free( s->l2_cache ); + qemu_free( s->cluster_cache ); + qemu_free( s->cluster_data ); + bdrv_delete( s->hd ); } -static int qcow_create(const char *filename, int64_t total_size, - const char *backing_file, int flags) +static int qcow_create( const char* filename, int64_t total_size, const char* backing_file, int flags ) { int fd, header_size, backing_filename_len, l1_size, i, shift; QCowHeader header; uint64_t tmp; - int ret; + int ret; - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); - if (fd < 0) + fd = open( filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644 ); + if ( fd < 0 ) return -1; - memset(&header, 0, sizeof(header)); - header.magic = cpu_to_be32(QCOW_MAGIC); - header.version = cpu_to_be32(QCOW_VERSION); - header.size = cpu_to_be64(total_size * 512); - header_size = sizeof(header); + memset( &header, 0, sizeof( header ) ); + header.magic = cpu_to_be32( QCOW_MAGIC ); + header.version = cpu_to_be32( QCOW_VERSION ); + header.size = cpu_to_be64( total_size * 512 ); + header_size = sizeof( header ); backing_filename_len = 0; - if (backing_file) { - header.backing_file_offset = cpu_to_be64(header_size); - backing_filename_len = strlen(backing_file); - header.backing_file_size = cpu_to_be32(backing_filename_len); + if ( backing_file ) { + header.backing_file_offset = cpu_to_be64( header_size ); + backing_filename_len = strlen( backing_file ); + header.backing_file_size = cpu_to_be32( backing_filename_len ); header_size += backing_filename_len; - header.mtime = cpu_to_be32(0); + header.mtime = cpu_to_be32( 0 ); header.cluster_bits = 9; /* 512 byte cluster to avoid copying unmodifyed sectors */ - header.l2_bits = 12; /* 32 KB L2 tables */ + header.l2_bits = 12; /* 32 KB L2 tables */ } else { header.cluster_bits = 12; /* 4 KB clusters */ - header.l2_bits = 9; /* 4 KB L2 tables */ + header.l2_bits = 9; /* 4 KB L2 tables */ } - header_size = (header_size + 7) & ~7; + header_size = ( header_size + 7 ) & ~7; shift = header.cluster_bits + header.l2_bits; - l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift; + l1_size = ( ( total_size * 512 ) + ( 1LL << shift ) - 1 ) >> shift; - header.l1_table_offset = cpu_to_be64(header_size); - if (flags) { - header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); + header.l1_table_offset = cpu_to_be64( header_size ); + if ( flags ) { + header.crypt_method = cpu_to_be32( QCOW_CRYPT_AES ); } else { - header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); + header.crypt_method = cpu_to_be32( QCOW_CRYPT_NONE ); } - + /* write all the data */ - ret = write(fd, &header, sizeof(header)); - if (ret != sizeof(header)) { + ret = write( fd, &header, sizeof( header ) ); + if ( ret != sizeof( header ) ) { ret = -1; goto exit; } - if (backing_file) { - ret = write(fd, backing_file, backing_filename_len); - if (ret != backing_filename_len) { + if ( backing_file ) { + ret = write( fd, backing_file, backing_filename_len ); + if ( ret != backing_filename_len ) { ret = -1; goto exit; } } - lseek(fd, header_size, SEEK_SET); + lseek( fd, header_size, SEEK_SET ); tmp = 0; - for(i = 0;i < l1_size; i++) { - ret = write(fd, &tmp, sizeof(tmp)); - if (ret != sizeof(tmp)) { + for ( i = 0; i < l1_size; i++ ) { + ret = write( fd, &tmp, sizeof( tmp ) ); + if ( ret != sizeof( tmp ) ) { ret = -1; goto exit; } @@ -497,106 +473,102 @@ static int qcow_create(const char *filename, int64_t total_size, ret = 0; exit: - close(fd); + close( fd ); return ret; } -static int qcow_make_empty(BlockDriverState *bs) +static int qcow_make_empty( BlockDriverState* bs ) { - BDRVQcowState *s = bs->opaque; - uint32_t l1_length = s->l1_size * sizeof(uint64_t); + BDRVQcowState* s = bs->opaque; + uint32_t l1_length = s->l1_size * sizeof( uint64_t ); int ret; - memset(s->l1_table, 0, l1_length); - if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) - return -1; - ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); - if (ret < 0) + memset( s->l1_table, 0, l1_length ); + if ( bdrv_pwrite( s->hd, s->l1_table_offset, s->l1_table, l1_length ) < 0 ) + return -1; + ret = bdrv_truncate( s->hd, s->l1_table_offset + l1_length ); + if ( ret < 0 ) return ret; - memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); - memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); - memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); + memset( s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof( uint64_t ) ); + memset( s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof( uint64_t ) ); + memset( s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof( uint32_t ) ); return 0; } /* XXX: put compressed sectors first, then all the cluster aligned tables to avoid losing bytes in alignment */ -static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, - const uint8_t *buf, int nb_sectors) +static int qcow_write_compressed( BlockDriverState* bs, int64_t sector_num, const uint8_t* buf, int nb_sectors ) { - BDRVQcowState *s = bs->opaque; + BDRVQcowState* s = bs->opaque; z_stream strm; int ret, out_len; - uint8_t *out_buf; + uint8_t* out_buf; uint64_t cluster_offset; - if (nb_sectors != s->cluster_sectors) + if ( nb_sectors != s->cluster_sectors ) return -EINVAL; - out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); - if (!out_buf) + out_buf = qemu_malloc( s->cluster_size + ( s->cluster_size / 1000 ) + 128 ); + if ( !out_buf ) return -1; /* best compression, small window, no zlib header */ - memset(&strm, 0, sizeof(strm)); - ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, - Z_DEFLATED, -12, - 9, Z_DEFAULT_STRATEGY); - if (ret != 0) { - qemu_free(out_buf); + memset( &strm, 0, sizeof( strm ) ); + ret = deflateInit2( &strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -12, 9, Z_DEFAULT_STRATEGY ); + if ( ret != 0 ) { + qemu_free( out_buf ); return -1; } strm.avail_in = s->cluster_size; - strm.next_in = (uint8_t *)buf; + strm.next_in = ( uint8_t* )buf; strm.avail_out = s->cluster_size; strm.next_out = out_buf; - ret = deflate(&strm, Z_FINISH); - if (ret != Z_STREAM_END && ret != Z_OK) { - qemu_free(out_buf); - deflateEnd(&strm); + ret = deflate( &strm, Z_FINISH ); + if ( ret != Z_STREAM_END && ret != Z_OK ) { + qemu_free( out_buf ); + deflateEnd( &strm ); return -1; } out_len = strm.next_out - out_buf; - deflateEnd(&strm); + deflateEnd( &strm ); - if (ret != Z_STREAM_END || out_len >= s->cluster_size) { + if ( ret != Z_STREAM_END || out_len >= s->cluster_size ) { /* could not compress: write normal cluster */ - qcow_write(bs, sector_num, buf, s->cluster_sectors); + qcow_write( bs, sector_num, buf, s->cluster_sectors ); } else { - cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, - out_len, 0, 0); + cluster_offset = get_cluster_offset( bs, sector_num << 9, 2, out_len, 0, 0 ); cluster_offset &= s->cluster_offset_mask; - if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { - qemu_free(out_buf); + if ( bdrv_pwrite( s->hd, cluster_offset, out_buf, out_len ) != out_len ) { + qemu_free( out_buf ); return -1; } } - - qemu_free(out_buf); + + qemu_free( out_buf ); return 0; } -static void qcow_flush(BlockDriverState *bs) +static void qcow_flush( BlockDriverState* bs ) { - BDRVQcowState *s = bs->opaque; - bdrv_flush(s->hd); + BDRVQcowState* s = bs->opaque; + bdrv_flush( s->hd ); } -static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) +static int qcow_get_info( BlockDriverState* bs, BlockDriverInfo* bdi ) { - BDRVQcowState *s = bs->opaque; + BDRVQcowState* s = bs->opaque; bdi->cluster_size = s->cluster_size; return 0; } BlockDriver bdrv_qcow = { "qcow", - sizeof(BDRVQcowState), + sizeof( BDRVQcowState ), qcow_probe, qcow_open, qcow_read, diff --git a/src/block-raw.c b/src/block-raw.c index b991304..7e58e06 100644 --- a/src/block-raw.c +++ b/src/block-raw.c @@ -1,8 +1,8 @@ /* * Block driver for RAW files - * + * * Copyright (c) 2006 Fabrice Bellard - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -27,82 +27,82 @@ #include #ifndef _WIN32 -#ifdef X49GP -#define QEMU_TOOL -#endif +# ifdef X49GP +# define QEMU_TOOL +# endif -#ifdef CONFIG_COCOA -#include -#include -#include -#include -#include -#include -#include -//#include -#include -#endif +# ifdef CONFIG_COCOA +# include +# include +# include +# include +# include +# include +# include +// #include +# include +# endif -#ifdef __sun__ -#define _POSIX_PTHREAD_SEMANTICS 1 -#include -#include -#endif -#ifdef __linux__ -#include -#include -#include -#endif -#ifdef __FreeBSD__ -#include -#endif +# ifdef __sun__ +# define _POSIX_PTHREAD_SEMANTICS 1 +# include +# include +# endif +# ifdef __linux__ +# include +# include +# include +# endif +# ifdef __FreeBSD__ +# include +# endif -//#define DEBUG_FLOPPY +// #define DEBUG_FLOPPY -#define FTYPE_FILE 0 -#define FTYPE_CD 1 -#define FTYPE_FD 2 +# define FTYPE_FILE 0 +# define FTYPE_CD 1 +# define FTYPE_FD 2 /* if the FD is not accessed during that time (in ms), we try to reopen it to see if the disk has been changed */ -#define FD_OPEN_TIMEOUT 1000 +# define FD_OPEN_TIMEOUT 1000 typedef struct BDRVRawState { int fd; int type; -#if defined(__linux__) +# if defined( __linux__ ) /* linux floppy specific */ int fd_open_flags; int64_t fd_open_time; int64_t fd_error_time; int fd_got_error; int fd_media_changed; -#endif +# endif } BDRVRawState; -static int fd_open(BlockDriverState *bs); +static int fd_open( BlockDriverState* bs ); -static int raw_open(BlockDriverState *bs, const char *filename, int flags) +static int raw_open( BlockDriverState* bs, const char* filename, int flags ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int fd, open_flags, ret; open_flags = O_BINARY; - if ((flags & BDRV_O_ACCESS) == O_RDWR) { + if ( ( flags & BDRV_O_ACCESS ) == O_RDWR ) { open_flags |= O_RDWR; } else { open_flags |= O_RDONLY; bs->read_only = 1; } - if (flags & BDRV_O_CREAT) + if ( flags & BDRV_O_CREAT ) open_flags |= O_CREAT | O_TRUNC; s->type = FTYPE_FILE; - fd = open(filename, open_flags, 0644); - if (fd < 0) { + fd = open( filename, open_flags, 0644 ); + if ( fd < 0 ) { ret = -errno; - if (ret == -EROFS) + if ( ret == -EROFS ) ret = -EACCES; return ret; } @@ -127,135 +127,131 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags) #endif */ -static int raw_pread(BlockDriverState *bs, int64_t offset, - uint8_t *buf, int count) +static int raw_pread( BlockDriverState* bs, int64_t offset, uint8_t* buf, int count ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int ret; - - ret = fd_open(bs); - if (ret < 0) + + ret = fd_open( bs ); + if ( ret < 0 ) return ret; - lseek(s->fd, offset, SEEK_SET); - ret = read(s->fd, buf, count); + lseek( s->fd, offset, SEEK_SET ); + ret = read( s->fd, buf, count ); return ret; } -static int raw_pwrite(BlockDriverState *bs, int64_t offset, - const uint8_t *buf, int count) +static int raw_pwrite( BlockDriverState* bs, int64_t offset, const uint8_t* buf, int count ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int ret; - - ret = fd_open(bs); - if (ret < 0) + + ret = fd_open( bs ); + if ( ret < 0 ) return ret; - lseek(s->fd, offset, SEEK_SET); - ret = write(s->fd, buf, count); + lseek( s->fd, offset, SEEK_SET ); + ret = write( s->fd, buf, count ); return ret; } -static void raw_close(BlockDriverState *bs) +static void raw_close( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; - if (s->fd >= 0) { - close(s->fd); + BDRVRawState* s = bs->opaque; + if ( s->fd >= 0 ) { + close( s->fd ); s->fd = -1; } } -static int raw_truncate(BlockDriverState *bs, int64_t offset) +static int raw_truncate( BlockDriverState* bs, int64_t offset ) { - BDRVRawState *s = bs->opaque; - if (s->type != FTYPE_FILE) + BDRVRawState* s = bs->opaque; + if ( s->type != FTYPE_FILE ) return -ENOTSUP; - if (ftruncate(s->fd, offset) < 0) + if ( ftruncate( s->fd, offset ) < 0 ) return -errno; return 0; } -static int64_t raw_getlength(BlockDriverState *bs) +static int64_t raw_getlength( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int fd = s->fd; int64_t size; -#ifdef _BSD +# ifdef _BSD struct stat sb; -#endif -#ifdef __sun__ +# endif +# ifdef __sun__ struct dk_minfo minfo; int rv; -#endif +# endif int ret; - ret = fd_open(bs); - if (ret < 0) + ret = fd_open( bs ); + if ( ret < 0 ) return ret; -#ifdef _BSD - if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { -#ifdef DIOCGMEDIASIZE - if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) -#endif -#ifdef CONFIG_COCOA - size = LONG_LONG_MAX; -#else - size = lseek(fd, 0LL, SEEK_END); -#endif +# ifdef _BSD + if ( !fstat( fd, &sb ) && ( S_IFCHR & sb.st_mode ) ) { +# ifdef DIOCGMEDIASIZE + if ( ioctl( fd, DIOCGMEDIASIZE, ( off_t* )&size ) ) +# endif +# ifdef CONFIG_COCOA + size = LONG_LONG_MAX; +# else + size = lseek( fd, 0LL, SEEK_END ); +# endif } else -#endif -#ifdef __sun__ - /* - * use the DKIOCGMEDIAINFO ioctl to read the size. - */ - rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); +# endif +# ifdef __sun__ + /* + * use the DKIOCGMEDIAINFO ioctl to read the size. + */ + rv = ioctl( fd, DKIOCGMEDIAINFO, &minfo ); if ( rv != -1 ) { size = minfo.dki_lbsize * minfo.dki_capacity; } else /* there are reports that lseek on some devices fails, but irc discussion said that contingency on contingency was overkill */ -#endif +# endif { - size = lseek(fd, 0, SEEK_END); + size = lseek( fd, 0, SEEK_END ); } return size; } -static int raw_create(const char *filename, int64_t total_size, - const char *backing_file, int flags) +static int raw_create( const char* filename, int64_t total_size, const char* backing_file, int flags ) { int fd; - int result = 0; + int result = 0; - if (flags || backing_file) + if ( flags || backing_file ) return -ENOTSUP; - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, - 0644); - if (fd < 0) { - result=-errno; + fd = open( filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644 ); + if ( fd < 0 ) { + result = -errno; } else { - if (ftruncate(fd, total_size * 512)) { - result=-errno; + if ( ftruncate( fd, total_size * 512 ) ) { + result = -errno; } - if (close(fd) != 0) { - result=-errno; + if ( close( fd ) != 0 ) { + result = -errno; } - } + } return result; } -static void raw_flush(BlockDriverState *bs) +static void raw_flush( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; - fsync(s->fd); + BDRVRawState* s = bs->opaque; + fsync( s->fd ); } BlockDriver bdrv_raw = { "raw", - sizeof(BDRVRawState), + sizeof( BDRVRawState ), NULL, /* no probe for protocols */ raw_open, NULL, @@ -273,46 +269,44 @@ BlockDriver bdrv_raw = { /***********************************************/ /* host device */ -#ifdef CONFIG_COCOA -static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); -static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); +# ifdef CONFIG_COCOA +static kern_return_t FindEjectableCDMedia( io_iterator_t* mediaIterator ); +static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char* bsdPath, CFIndex maxPathSize ); -kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) +kern_return_t FindEjectableCDMedia( io_iterator_t* mediaIterator ) { - kern_return_t kernResult; - mach_port_t masterPort; - CFMutableDictionaryRef classesToMatch; + kern_return_t kernResult; + mach_port_t masterPort; + CFMutableDictionaryRef classesToMatch; kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); if ( KERN_SUCCESS != kernResult ) { printf( "IOMasterPort returned %d\n", kernResult ); } - - classesToMatch = IOServiceMatching( kIOCDMediaClass ); + + classesToMatch = IOServiceMatching( kIOCDMediaClass ); if ( classesToMatch == NULL ) { printf( "IOServiceMatching returned a NULL dictionary.\n" ); } else { - CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); + CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); } kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); - if ( KERN_SUCCESS != kernResult ) - { + if ( KERN_SUCCESS != kernResult ) { printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); } - + return kernResult; } -kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) +kern_return_t GetBSDPath( io_iterator_t mediaIterator, char* bsdPath, CFIndex maxPathSize ) { - io_object_t nextMedia; - kern_return_t kernResult = KERN_FAILURE; + io_object_t nextMedia; + kern_return_t kernResult = KERN_FAILURE; *bsdPath = '\0'; nextMedia = IOIteratorNext( mediaIterator ); - if ( nextMedia ) - { - CFTypeRef bsdPathAsCFString; - bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); + if ( nextMedia ) { + CFTypeRef bsdPathAsCFString; + bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); if ( bsdPathAsCFString ) { size_t devPathLength; strcpy( bsdPath, _PATH_DEV ); @@ -325,45 +319,45 @@ kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex ma } IOObjectRelease( nextMedia ); } - + return kernResult; } -#endif +# endif -static int hdev_open(BlockDriverState *bs, const char *filename, int flags) +static int hdev_open( BlockDriverState* bs, const char* filename, int flags ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int fd, open_flags, ret; -#ifdef CONFIG_COCOA - if (strstart(filename, "/dev/cdrom", NULL)) { +# ifdef CONFIG_COCOA + if ( strstart( filename, "/dev/cdrom", NULL ) ) { kern_return_t kernResult; io_iterator_t mediaIterator; char bsdPath[ MAXPATHLEN ]; int fd; - + kernResult = FindEjectableCDMedia( &mediaIterator ); kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); - + if ( bsdPath[ 0 ] != '\0' ) { - strcat(bsdPath,"s0"); + strcat( bsdPath, "s0" ); /* some CDs don't have a partition 0 */ - fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); - if (fd < 0) { - bsdPath[strlen(bsdPath)-1] = '1'; + fd = open( bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE ); + if ( fd < 0 ) { + bsdPath[ strlen( bsdPath ) - 1 ] = '1'; } else { - close(fd); + close( fd ); } filename = bsdPath; } - + if ( mediaIterator ) IOObjectRelease( mediaIterator ); } -#endif +# endif open_flags = O_BINARY; - if ((flags & BDRV_O_ACCESS) == O_RDWR) { + if ( ( flags & BDRV_O_ACCESS ) == O_RDWR ) { open_flags |= O_RDWR; } else { open_flags |= O_RDONLY; @@ -371,170 +365,155 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags) } s->type = FTYPE_FILE; -#if defined(__linux__) - if (strstart(filename, "/dev/cd", NULL)) { +# if defined( __linux__ ) + if ( strstart( filename, "/dev/cd", NULL ) ) { /* open will not fail even if no CD is inserted */ open_flags |= O_NONBLOCK; s->type = FTYPE_CD; - } else if (strstart(filename, "/dev/fd", NULL)) { + } else if ( strstart( filename, "/dev/fd", NULL ) ) { s->type = FTYPE_FD; s->fd_open_flags = open_flags; /* open will not fail even if no floppy is inserted */ open_flags |= O_NONBLOCK; } -#endif - fd = open(filename, open_flags, 0644); - if (fd < 0) { +# endif + fd = open( filename, open_flags, 0644 ); + if ( fd < 0 ) { ret = -errno; - if (ret == -EROFS) + if ( ret == -EROFS ) ret = -EACCES; return ret; } s->fd = fd; -#if defined(__linux__) +# if defined( __linux__ ) /* close fd so that we can reopen it as needed */ - if (s->type == FTYPE_FD) { - close(s->fd); + if ( s->type == FTYPE_FD ) { + close( s->fd ); s->fd = -1; s->fd_media_changed = 1; } -#endif +# endif return 0; } -static int fd_open(BlockDriverState *bs) -{ - return 0; -} +static int fd_open( BlockDriverState* bs ) { return 0; } -#if defined(__linux__) +# if defined( __linux__ ) -static int raw_is_inserted(BlockDriverState *bs) +static int raw_is_inserted( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int ret; - switch(s->type) { - case FTYPE_CD: - ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); - if (ret == CDS_DISC_OK) + switch ( s->type ) { + case FTYPE_CD: + ret = ioctl( s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT ); + if ( ret == CDS_DISC_OK ) + return 1; + else + return 0; + break; + case FTYPE_FD: + ret = fd_open( bs ); + return ( ret >= 0 ); + default: return 1; - else - return 0; - break; - case FTYPE_FD: - ret = fd_open(bs); - return (ret >= 0); - default: - return 1; } } /* currently only used by fdc.c, but a CD version would be good too */ -static int raw_media_changed(BlockDriverState *bs) +static int raw_media_changed( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; - switch(s->type) { - case FTYPE_FD: - { - int ret; - /* XXX: we do not have a true media changed indication. It - does not work if the floppy is changed without trying - to read it */ - fd_open(bs); - ret = s->fd_media_changed; - s->fd_media_changed = 0; -#ifdef DEBUG_FLOPPY - printf("Floppy changed=%d\n", ret); -#endif - return ret; - } - default: - return -ENOTSUP; + switch ( s->type ) { + case FTYPE_FD: + { + int ret; + /* XXX: we do not have a true media changed indication. It + does not work if the floppy is changed without trying + to read it */ + fd_open( bs ); + ret = s->fd_media_changed; + s->fd_media_changed = 0; +# ifdef DEBUG_FLOPPY + printf( "Floppy changed=%d\n", ret ); +# endif + return ret; + } + default: + return -ENOTSUP; } } -static int raw_eject(BlockDriverState *bs, int eject_flag) +static int raw_eject( BlockDriverState* bs, int eject_flag ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; - switch(s->type) { - case FTYPE_CD: - if (eject_flag) { - if (ioctl (s->fd, CDROMEJECT, NULL) < 0) - perror("CDROMEJECT"); - } else { - if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0) - perror("CDROMEJECT"); - } - break; - case FTYPE_FD: - { - int fd; - if (s->fd >= 0) { - close(s->fd); - s->fd = -1; + switch ( s->type ) { + case FTYPE_CD: + if ( eject_flag ) { + if ( ioctl( s->fd, CDROMEJECT, NULL ) < 0 ) + perror( "CDROMEJECT" ); + } else { + if ( ioctl( s->fd, CDROMCLOSETRAY, NULL ) < 0 ) + perror( "CDROMEJECT" ); } - fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK); - if (fd >= 0) { - if (ioctl(fd, FDEJECT, 0) < 0) - perror("FDEJECT"); - close(fd); + break; + case FTYPE_FD: + { + int fd; + if ( s->fd >= 0 ) { + close( s->fd ); + s->fd = -1; + } + fd = open( bs->filename, s->fd_open_flags | O_NONBLOCK ); + if ( fd >= 0 ) { + if ( ioctl( fd, FDEJECT, 0 ) < 0 ) + perror( "FDEJECT" ); + close( fd ); + } } - } - break; - default: - return -ENOTSUP; + break; + default: + return -ENOTSUP; } return 0; } -static int raw_set_locked(BlockDriverState *bs, int locked) +static int raw_set_locked( BlockDriverState* bs, int locked ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; - switch(s->type) { - case FTYPE_CD: - if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) { - /* Note: an error can happen if the distribution automatically - mounts the CD-ROM */ - // perror("CDROM_LOCKDOOR"); - } - break; - default: - return -ENOTSUP; + switch ( s->type ) { + case FTYPE_CD: + if ( ioctl( s->fd, CDROM_LOCKDOOR, locked ) < 0 ) { + /* Note: an error can happen if the distribution automatically + mounts the CD-ROM */ + // perror("CDROM_LOCKDOOR"); + } + break; + default: + return -ENOTSUP; } return 0; } -#else +# else -static int raw_is_inserted(BlockDriverState *bs) -{ - return 1; -} +static int raw_is_inserted( BlockDriverState* bs ) { return 1; } -static int raw_media_changed(BlockDriverState *bs) -{ - return -ENOTSUP; -} +static int raw_media_changed( BlockDriverState* bs ) { return -ENOTSUP; } -static int raw_eject(BlockDriverState *bs, int eject_flag) -{ - return -ENOTSUP; -} +static int raw_eject( BlockDriverState* bs, int eject_flag ) { return -ENOTSUP; } -static int raw_set_locked(BlockDriverState *bs, int locked) -{ - return -ENOTSUP; -} +static int raw_set_locked( BlockDriverState* bs, int locked ) { return -ENOTSUP; } -#endif /* !linux */ +# endif /* !linux */ BlockDriver bdrv_host_device = { "host_device", - sizeof(BDRVRawState), + sizeof( BDRVRawState ), NULL, /* no probe for protocols */ hdev_open, NULL, @@ -556,104 +535,100 @@ BlockDriver bdrv_host_device = { #else /* _WIN32 */ /* XXX: use another file ? */ -#include +# include -#define FTYPE_FILE 0 -#define FTYPE_CD 1 -#define FTYPE_HARDDISK 2 +# define FTYPE_FILE 0 +# define FTYPE_CD 1 +# define FTYPE_HARDDISK 2 typedef struct BDRVRawState { HANDLE hfile; int type; - char drive_path[16]; /* format: "d:\" */ + char drive_path[ 16 ]; /* format: "d:\" */ } BDRVRawState; -int qemu_ftruncate64(int fd, int64_t length) +int qemu_ftruncate64( int fd, int64_t length ) { LARGE_INTEGER li; LONG high; HANDLE h; BOOL res; - if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) - return -1; + if ( ( GetVersion() & 0x80000000UL ) && ( length >> 32 ) != 0 ) + return -1; - h = (HANDLE)_get_osfhandle(fd); + h = ( HANDLE )_get_osfhandle( fd ); /* get current position, ftruncate do not change position */ li.HighPart = 0; - li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT); - if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) - return -1; + li.LowPart = SetFilePointer( h, 0, &li.HighPart, FILE_CURRENT ); + if ( li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR ) + return -1; high = length >> 32; - if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN)) - return -1; - res = SetEndOfFile(h); + if ( !SetFilePointer( h, ( DWORD )length, &high, FILE_BEGIN ) ) + return -1; + res = SetEndOfFile( h ); /* back to old position */ - SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); + SetFilePointer( h, li.LowPart, &li.HighPart, FILE_BEGIN ); return res ? 0 : -1; } -static int set_sparse(int fd) +static int set_sparse( int fd ) { DWORD returned; - return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE, - NULL, 0, NULL, 0, &returned, NULL); + return ( int )DeviceIoControl( ( HANDLE )_get_osfhandle( fd ), FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &returned, NULL ); } -static int raw_open(BlockDriverState *bs, const char *filename, int flags) +static int raw_open( BlockDriverState* bs, const char* filename, int flags ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int access_flags, create_flags; DWORD overlapped; s->type = FTYPE_FILE; - if ((flags & BDRV_O_ACCESS) == O_RDWR) { + if ( ( flags & BDRV_O_ACCESS ) == O_RDWR ) { access_flags = GENERIC_READ | GENERIC_WRITE; } else { access_flags = GENERIC_READ; } - if (flags & BDRV_O_CREAT) { + if ( flags & BDRV_O_CREAT ) { create_flags = CREATE_ALWAYS; } else { create_flags = OPEN_EXISTING; } -#ifdef QEMU_TOOL +# ifdef QEMU_TOOL overlapped = FILE_ATTRIBUTE_NORMAL; -#else +# else overlapped = FILE_FLAG_OVERLAPPED; -#endif - s->hfile = CreateFile(filename, access_flags, - FILE_SHARE_READ, NULL, - create_flags, overlapped, NULL); - if (s->hfile == INVALID_HANDLE_VALUE) { +# endif + s->hfile = CreateFile( filename, access_flags, FILE_SHARE_READ, NULL, create_flags, overlapped, NULL ); + if ( s->hfile == INVALID_HANDLE_VALUE ) { int err = GetLastError(); - if (err == ERROR_ACCESS_DENIED) + if ( err == ERROR_ACCESS_DENIED ) return -EACCES; return -1; } return 0; } -static int raw_pread(BlockDriverState *bs, int64_t offset, - uint8_t *buf, int count) +static int raw_pread( BlockDriverState* bs, int64_t offset, uint8_t* buf, int count ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; OVERLAPPED ov; DWORD ret_count; int ret; - - memset(&ov, 0, sizeof(ov)); + + memset( &ov, 0, sizeof( ov ) ); ov.Offset = offset; ov.OffsetHigh = offset >> 32; - ret = ReadFile(s->hfile, buf, count, &ret_count, &ov); - if (!ret) { - ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); - if (!ret) + ret = ReadFile( s->hfile, buf, count, &ret_count, &ov ); + if ( !ret ) { + ret = GetOverlappedResult( s->hfile, &ov, &ret_count, TRUE ); + if ( !ret ) return -EIO; else return ret_count; @@ -661,21 +636,20 @@ static int raw_pread(BlockDriverState *bs, int64_t offset, return ret_count; } -static int raw_pwrite(BlockDriverState *bs, int64_t offset, - const uint8_t *buf, int count) +static int raw_pwrite( BlockDriverState* bs, int64_t offset, const uint8_t* buf, int count ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; OVERLAPPED ov; DWORD ret_count; int ret; - - memset(&ov, 0, sizeof(ov)); + + memset( &ov, 0, sizeof( ov ) ); ov.Offset = offset; ov.OffsetHigh = offset >> 32; - ret = WriteFile(s->hfile, buf, count, &ret_count, &ov); - if (!ret) { - ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); - if (!ret) + ret = WriteFile( s->hfile, buf, count, &ret_count, &ov ); + if ( !ret ) { + ret = GetOverlappedResult( s->hfile, &ov, &ret_count, TRUE ); + if ( !ret ) return -EIO; else return ret_count; @@ -683,87 +657,83 @@ static int raw_pwrite(BlockDriverState *bs, int64_t offset, return ret_count; } -static void raw_flush(BlockDriverState *bs) +static void raw_flush( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; - FlushFileBuffers(s->hfile); + BDRVRawState* s = bs->opaque; + FlushFileBuffers( s->hfile ); } -static void raw_close(BlockDriverState *bs) +static void raw_close( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; - CloseHandle(s->hfile); + BDRVRawState* s = bs->opaque; + CloseHandle( s->hfile ); } -static int raw_truncate(BlockDriverState *bs, int64_t offset) +static int raw_truncate( BlockDriverState* bs, int64_t offset ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; DWORD low, high; low = offset; high = offset >> 32; - if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN)) - return -EIO; - if (!SetEndOfFile(s->hfile)) + if ( !SetFilePointer( s->hfile, low, &high, FILE_BEGIN ) ) + return -EIO; + if ( !SetEndOfFile( s->hfile ) ) return -EIO; return 0; } -static int64_t raw_getlength(BlockDriverState *bs) +static int64_t raw_getlength( BlockDriverState* bs ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; LARGE_INTEGER l; - ULARGE_INTEGER available, total, total_free; + ULARGE_INTEGER available, total, total_free; DISK_GEOMETRY dg; DWORD count; BOOL status; - switch(s->type) { - case FTYPE_FILE: - l.LowPart = GetFileSize(s->hfile, &l.HighPart); - if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) + switch ( s->type ) { + case FTYPE_FILE: + l.LowPart = GetFileSize( s->hfile, &l.HighPart ); + if ( l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR ) + return -EIO; + break; + case FTYPE_CD: + if ( !GetDiskFreeSpaceEx( s->drive_path, &available, &total, &total_free ) ) + return -EIO; + l.QuadPart = total.QuadPart; + break; + case FTYPE_HARDDISK: + status = DeviceIoControl( s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &dg, sizeof( dg ), &count, NULL ); + if ( status != FALSE ) { + l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder * dg.SectorsPerTrack * dg.BytesPerSector; + } + break; + default: return -EIO; - break; - case FTYPE_CD: - if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free)) - return -EIO; - l.QuadPart = total.QuadPart; - break; - case FTYPE_HARDDISK: - status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY, - NULL, 0, &dg, sizeof(dg), &count, NULL); - if (status != FALSE) { - l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder - * dg.SectorsPerTrack * dg.BytesPerSector; - } - break; - default: - return -EIO; } return l.QuadPart; } -static int raw_create(const char *filename, int64_t total_size, - const char *backing_file, int flags) +static int raw_create( const char* filename, int64_t total_size, const char* backing_file, int flags ) { int fd; - if (flags || backing_file) + if ( flags || backing_file ) return -ENOTSUP; - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, - 0644); - if (fd < 0) + fd = open( filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644 ); + if ( fd < 0 ) return -EIO; - set_sparse(fd); - ftruncate(fd, total_size * 512); - close(fd); + set_sparse( fd ); + ftruncate( fd, total_size * 512 ); + close( fd ); return 0; } BlockDriver bdrv_raw = { "raw", - sizeof(BDRVRawState), + sizeof( BDRVRawState ), NULL, /* no probe for protocols */ raw_open, NULL, @@ -782,39 +752,38 @@ BlockDriver bdrv_raw = { /***********************************************/ /* host device */ -static int find_cdrom(char *cdrom_name, int cdrom_name_size) +static int find_cdrom( char* cdrom_name, int cdrom_name_size ) { - char drives[256], *pdrv = drives; + char drives[ 256 ], *pdrv = drives; UINT type; - memset(drives, 0, sizeof(drives)); - GetLogicalDriveStrings(sizeof(drives), drives); - while(pdrv[0] != '\0') { - type = GetDriveType(pdrv); - switch(type) { - case DRIVE_CDROM: - snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); - return 0; - break; + memset( drives, 0, sizeof( drives ) ); + GetLogicalDriveStrings( sizeof( drives ), drives ); + while ( pdrv[ 0 ] != '\0' ) { + type = GetDriveType( pdrv ); + switch ( type ) { + case DRIVE_CDROM: + snprintf( cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[ 0 ] ); + return 0; + break; } - pdrv += lstrlen(pdrv) + 1; + pdrv += lstrlen( pdrv ) + 1; } return -1; } -static int find_device_type(BlockDriverState *bs, const char *filename) +static int find_device_type( BlockDriverState* bs, const char* filename ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; UINT type; - const char *p; + const char* p; - if (strstart(filename, "\\\\.\\", &p) || - strstart(filename, "//./", &p)) { - if (stristart(p, "PhysicalDrive", NULL)) + if ( strstart( filename, "\\\\.\\", &p ) || strstart( filename, "//./", &p ) ) { + if ( stristart( p, "PhysicalDrive", NULL ) ) return FTYPE_HARDDISK; - snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); - type = GetDriveType(s->drive_path); - if (type == DRIVE_CDROM) + snprintf( s->drive_path, sizeof( s->drive_path ), "%c:\\", p[ 0 ] ); + type = GetDriveType( s->drive_path ); + if ( type == DRIVE_CDROM ) return FTYPE_CD; else return FTYPE_FILE; @@ -823,47 +792,44 @@ static int find_device_type(BlockDriverState *bs, const char *filename) } } -static int hdev_open(BlockDriverState *bs, const char *filename, int flags) +static int hdev_open( BlockDriverState* bs, const char* filename, int flags ) { - BDRVRawState *s = bs->opaque; + BDRVRawState* s = bs->opaque; int access_flags, create_flags; DWORD overlapped; - char device_name[64]; + char device_name[ 64 ]; - if (strstart(filename, "/dev/cdrom", NULL)) { - if (find_cdrom(device_name, sizeof(device_name)) < 0) + if ( strstart( filename, "/dev/cdrom", NULL ) ) { + if ( find_cdrom( device_name, sizeof( device_name ) ) < 0 ) return -ENOENT; filename = device_name; } else { /* transform drive letters into device name */ - if (((filename[0] >= 'a' && filename[0] <= 'z') || - (filename[0] >= 'A' && filename[0] <= 'Z')) && - filename[1] == ':' && filename[2] == '\0') { - snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); + if ( ( ( filename[ 0 ] >= 'a' && filename[ 0 ] <= 'z' ) || ( filename[ 0 ] >= 'A' && filename[ 0 ] <= 'Z' ) ) && + filename[ 1 ] == ':' && filename[ 2 ] == '\0' ) { + snprintf( device_name, sizeof( device_name ), "\\\\.\\%c:", filename[ 0 ] ); filename = device_name; } } - s->type = find_device_type(bs, filename); - - if ((flags & BDRV_O_ACCESS) == O_RDWR) { + s->type = find_device_type( bs, filename ); + + if ( ( flags & BDRV_O_ACCESS ) == O_RDWR ) { access_flags = GENERIC_READ | GENERIC_WRITE; } else { access_flags = GENERIC_READ; } create_flags = OPEN_EXISTING; -#ifdef QEMU_TOOL +# ifdef QEMU_TOOL overlapped = FILE_ATTRIBUTE_NORMAL; -#else +# else overlapped = FILE_FLAG_OVERLAPPED; -#endif - s->hfile = CreateFile(filename, access_flags, - FILE_SHARE_READ, NULL, - create_flags, overlapped, NULL); - if (s->hfile == INVALID_HANDLE_VALUE) { +# endif + s->hfile = CreateFile( filename, access_flags, FILE_SHARE_READ, NULL, create_flags, overlapped, NULL ); + if ( s->hfile == INVALID_HANDLE_VALUE ) { int err = GetLastError(); - if (err == ERROR_ACCESS_DENIED) + if ( err == ERROR_ACCESS_DENIED ) return -EACCES; return -1; } @@ -872,7 +838,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags) BlockDriver bdrv_host_device = { "host_device", - sizeof(BDRVRawState), + sizeof( BDRVRawState ), NULL, /* no probe for protocols */ hdev_open, NULL, diff --git a/src/block-vvfat.c b/src/block-vvfat.c index 30a1221..4d48d24 100644 --- a/src/block-vvfat.c +++ b/src/block-vvfat.c @@ -1,9 +1,9 @@ /* vim:set shiftwidth=4 ts=8: */ /* * QEMU Block driver for virtual VFAT (shadows a local directory) - * + * * Copyright (c) 2004,2005 Johannes E. Schindelin - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -30,198 +30,200 @@ #include "block_int.h" #ifndef S_IWGRP -#define S_IWGRP 0 +# define S_IWGRP 0 #endif #ifndef S_IWOTH -#define S_IWOTH 0 +# define S_IWOTH 0 #endif /* TODO: add ":bootsector=blabla.img:" */ /* LATER TODO: add automatic boot sector generation from BOOTEASY.ASM and Ranish Partition Manager - Note that DOS assumes the system files to be the first files in the + Note that DOS assumes the system files to be the first files in the file system (test if the boot sector still relies on that fact)! */ /* MAYBE TODO: write block-visofs.c */ -//#define DEBUG +// #define DEBUG #undef DEBUG_SECTORS #ifdef DEBUG -#define DLOG(a) a +# define DLOG( a ) a -#undef stderr -#define stderr STDERR +# undef stderr +# define stderr STDERR FILE* stderr = NULL; -static void checkpoint(const char *where); +static void checkpoint( const char* where ); -#ifdef __MINGW32__ -void nonono(const char* file, int line, const char* msg) { - fprintf(stderr, "Nonono! %s:%d %s\n", file, line, msg); - exit(-5); +# ifdef __MINGW32__ +void nonono( const char* file, int line, const char* msg ) +{ + fprintf( stderr, "Nonono! %s:%d %s\n", file, line, msg ); + exit( -5 ); } -#undef assert -#define assert(a) do {if (!(a)) nonono(__FILE__, __LINE__, #a);}while(0) -#endif +# undef assert +# define assert( a ) \ + do { \ + if ( !( a ) ) \ + nonono( __FILE__, __LINE__, #a ); \ + } while ( 0 ) +# endif #else -#define DLOG(a) +# define DLOG( a ) #endif -#define VOLUME_LABEL "X49GP-VVFAT" +#define VOLUME_LABEL "X49GP-VVFAT" /* dynamic array functions */ typedef struct array_t { unsigned char* pointer; - unsigned int size,next,item_size; + unsigned int size, next, item_size; } array_t; -static inline void array_init(array_t* array,unsigned int item_size) +static inline void array_init( array_t* array, unsigned int item_size ) { - array->pointer=0; - array->size=0; - array->next=0; - array->item_size=item_size; + array->pointer = 0; + array->size = 0; + array->next = 0; + array->item_size = item_size; } -static inline void array_free(array_t* array) +static inline void array_free( array_t* array ) { - if(array->pointer) - free(array->pointer); - array->size=array->next=0; + if ( array->pointer ) + free( array->pointer ); + array->size = array->next = 0; } /* does not automatically grow */ -static inline void* array_get(array_t* array,unsigned int index) { - assert(index >= 0); - assert(index < array->next); +static inline void* array_get( array_t* array, unsigned int index ) +{ + assert( index >= 0 ); + assert( index < array->next ); return array->pointer + index * array->item_size; } -static inline int array_ensure_allocated(array_t* array, int index) +static inline int array_ensure_allocated( array_t* array, int index ) { - if((index + 1) * array->item_size > array->size) { - int new_size = (index + 32) * array->item_size; - array->pointer = realloc(array->pointer, new_size); - if (!array->pointer) - return -1; - array->size = new_size; - array->next = index + 1; + if ( ( index + 1 ) * array->item_size > array->size ) { + int new_size = ( index + 32 ) * array->item_size; + array->pointer = realloc( array->pointer, new_size ); + if ( !array->pointer ) + return -1; + array->size = new_size; + array->next = index + 1; } return 0; } -static inline void* array_get_next(array_t* array) { +static inline void* array_get_next( array_t* array ) +{ unsigned int next = array->next; void* result; - if (array_ensure_allocated(array, next) < 0) - return NULL; + if ( array_ensure_allocated( array, next ) < 0 ) + return NULL; array->next = next + 1; - result = array_get(array, next); + result = array_get( array, next ); return result; } -static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) { - if((array->next+count)*array->item_size>array->size) { - int increment=count*array->item_size; - array->pointer=realloc(array->pointer,array->size+increment); - if(!array->pointer) - return 0; - array->size+=increment; +static inline void* array_insert( array_t* array, unsigned int index, unsigned int count ) +{ + if ( ( array->next + count ) * array->item_size > array->size ) { + int increment = count * array->item_size; + array->pointer = realloc( array->pointer, array->size + increment ); + if ( !array->pointer ) + return 0; + array->size += increment; } - memmove(array->pointer+(index+count)*array->item_size, - array->pointer+index*array->item_size, - (array->next-index)*array->item_size); - array->next+=count; - return array->pointer+index*array->item_size; + memmove( array->pointer + ( index + count ) * array->item_size, array->pointer + index * array->item_size, + ( array->next - index ) * array->item_size ); + array->next += count; + return array->pointer + index * array->item_size; } /* this performs a "roll", so that the element which was at index_from becomes * index_to, but the order of all other elements is preserved. */ -static inline int array_roll(array_t* array,int index_to,int index_from,int count) +static inline int array_roll( array_t* array, int index_to, int index_from, int count ) { unsigned char* buf; unsigned char* from; unsigned char* to; int is; - if(!array || - index_to<0 || index_to>=array->next || - index_from<0 || index_from>=array->next) - return -1; - - if(index_to==index_from) - return 0; + if ( !array || index_to < 0 || index_to >= array->next || index_from < 0 || index_from >= array->next ) + return -1; - is=array->item_size; - from=array->pointer+index_from*is; - to=array->pointer+index_to*is; - buf=malloc(is*count); - memcpy(buf,from,is*count); + if ( index_to == index_from ) + return 0; - if(index_toitem_size; + from = array->pointer + index_from * is; + to = array->pointer + index_to * is; + buf = malloc( is * count ); + memcpy( buf, from, is * count ); + + if ( index_to < index_from ) + memmove( to + is * count, to, from - to ); else - memmove(from,from+is*count,to-from); - - memcpy(to,buf,is*count); + memmove( from, from + is * count, to - from ); - free(buf); + memcpy( to, buf, is * count ); + + free( buf ); return 0; } -static inline int array_remove_slice(array_t* array,int index, int count) +static inline int array_remove_slice( array_t* array, int index, int count ) { - assert(index >=0); - assert(count > 0); - assert(index + count <= array->next); - if(array_roll(array,array->next-1,index,count)) - return -1; + assert( index >= 0 ); + assert( count > 0 ); + assert( index + count <= array->next ); + if ( array_roll( array, array->next - 1, index, count ) ) + return -1; array->next -= count; return 0; } -static int array_remove(array_t* array,int index) -{ - return array_remove_slice(array, index, 1); -} +static int array_remove( array_t* array, int index ) { return array_remove_slice( array, index, 1 ); } /* return the index for a given member */ -static int array_index(array_t* array, void* pointer) +static int array_index( array_t* array, void* pointer ) { - size_t offset = (unsigned char*)pointer - array->pointer; - assert(offset >= 0); - assert((offset % array->item_size) == 0); - assert(offset/array->item_size < array->next); - return offset/array->item_size; + size_t offset = ( unsigned char* )pointer - array->pointer; + assert( offset >= 0 ); + assert( ( offset % array->item_size ) == 0 ); + assert( offset / array->item_size < array->next ); + return offset / array->item_size; } /* These structures are used to fake a disk and the VFAT filesystem. * For this reason we need to use __attribute__((packed)). */ -#define BOOTCODE_SIZE 448 -#define BOOTCODE_FAT32_SIZE 420 +#define BOOTCODE_SIZE 448 +#define BOOTCODE_FAT32_SIZE 420 typedef struct msdos_volume_info { - uint8_t drive_number; - uint8_t ignored; - uint8_t signature; - uint32_t id; - uint8_t volume_label[11]; - uint8_t fs_type[8]; -} __attribute__((packed)) msdos_volume_info_t; + uint8_t drive_number; + uint8_t ignored; + uint8_t signature; + uint32_t id; + uint8_t volume_label[ 11 ]; + uint8_t fs_type[ 8 ]; +} __attribute__( ( packed ) ) msdos_volume_info_t; typedef struct bootsector_t { - uint8_t jump[3]; - uint8_t name[8]; + uint8_t jump[ 3 ]; + uint8_t name[ 8 ]; uint16_t sector_size; uint8_t sectors_per_cluster; uint16_t reserved_sectors; @@ -236,54 +238,51 @@ typedef struct bootsector_t { uint32_t total_sectors; union { struct { - msdos_volume_info_t vi; - uint8_t boot_code[BOOTCODE_SIZE]; - } __attribute__((packed)) fat16; - struct { - uint32_t sectors_per_fat; - uint16_t flags; - uint8_t major,minor; - uint32_t first_cluster_of_root_directory; - uint16_t info_sector; - uint16_t backup_boot_sector; - uint16_t ignored[6]; - msdos_volume_info_t vi; - uint8_t boot_code[BOOTCODE_FAT32_SIZE]; - } __attribute__((packed)) fat32; + msdos_volume_info_t vi; + uint8_t boot_code[ BOOTCODE_SIZE ]; + } __attribute__( ( packed ) ) fat16; + struct { + uint32_t sectors_per_fat; + uint16_t flags; + uint8_t major, minor; + uint32_t first_cluster_of_root_directory; + uint16_t info_sector; + uint16_t backup_boot_sector; + uint16_t ignored[ 6 ]; + msdos_volume_info_t vi; + uint8_t boot_code[ BOOTCODE_FAT32_SIZE ]; + } __attribute__( ( packed ) ) fat32; } u; - uint8_t magic[2]; -} __attribute__((packed)) bootsector_t; + uint8_t magic[ 2 ]; +} __attribute__( ( packed ) ) bootsector_t; -static -uint8_t dummy_boot_jump[3] = { 0xeb, 0x3c, 0x90 }; +static uint8_t dummy_boot_jump[ 3 ] = { 0xeb, 0x3c, 0x90 }; -static -uint8_t dummy_boot_code[BOOTCODE_SIZE] = - "\x0e" /* push cs */ - "\x1f" /* pop ds */ - "\xbe\x5b\x7c" /* mov si, */ -/* write_msg: */ - "\xac" /* lodsb */ - "\x22\xc0" /* and al, al */ - "\x74\x0b" /* jz key_press */ - "\x56" /* push si */ - "\xb4\x0e" /* mov ah, 0eh */ - "\xbb\x07\x00" /* mov bx, 0007h */ - "\xcd\x10" /* int 10h */ - "\x5e" /* pop si */ - "\xeb\xf0" /* jmp write_msg */ -/* key_press: */ - "\x32\xe4" /* xor ah, ah */ - "\xcd\x16" /* int 16h */ - "\xcd\x19" /* int 19h */ -/* foo: */ - "\xeb\xfe" /* jmp foo */ -/* message_text: */ - "This is not a bootable disk. Please insert a bootable floppy and\r\n" - "press any key to try again...\r\n"; +static uint8_t dummy_boot_code[ BOOTCODE_SIZE ] = "\x0e" /* push cs */ + "\x1f" /* pop ds */ + "\xbe\x5b\x7c" /* mov si, */ + /* write_msg: */ + "\xac" /* lodsb */ + "\x22\xc0" /* and al, al */ + "\x74\x0b" /* jz key_press */ + "\x56" /* push si */ + "\xb4\x0e" /* mov ah, 0eh */ + "\xbb\x07\x00" /* mov bx, 0007h */ + "\xcd\x10" /* int 10h */ + "\x5e" /* pop si */ + "\xeb\xf0" /* jmp write_msg */ + /* key_press: */ + "\x32\xe4" /* xor ah, ah */ + "\xcd\x16" /* int 16h */ + "\xcd\x19" /* int 19h */ + /* foo: */ + "\xeb\xfe" /* jmp foo */ + /* message_text: */ + "This is not a bootable disk. Please insert a bootable floppy and\r\n" + "press any key to try again...\r\n"; -#define MSG_OFFSET_OFFSET 3 -#define MESSAGE_OFFSET 29 /* Offset of message in above code */ +#define MSG_OFFSET_OFFSET 3 +#define MESSAGE_OFFSET 29 /* Offset of message in above code */ typedef struct partition_t { uint8_t attributes; /* 0x80 = bootable */ @@ -296,19 +295,19 @@ typedef struct partition_t { uint8_t end_cylinder; uint32_t start_sector_long; uint32_t end_sector_long; -} __attribute__((packed)) partition_t; +} __attribute__( ( packed ) ) partition_t; typedef struct mbr_t { - uint8_t ignored[0x1be]; - partition_t partition[4]; - uint8_t magic[2]; -} __attribute__((packed)) mbr_t; + uint8_t ignored[ 0x1be ]; + partition_t partition[ 4 ]; + uint8_t magic[ 2 ]; +} __attribute__( ( packed ) ) mbr_t; typedef struct direntry_t { - uint8_t name[8]; - uint8_t extension[3]; + uint8_t name[ 8 ]; + uint8_t extension[ 3 ]; uint8_t attributes; - uint8_t reserved[2]; + uint8_t reserved[ 2 ]; uint16_t ctime; uint16_t cdate; uint16_t adate; @@ -317,68 +316,74 @@ typedef struct direntry_t { uint16_t mdate; uint16_t begin; uint32_t size; -} __attribute__((packed)) direntry_t; +} __attribute__( ( packed ) ) direntry_t; /* this structure are used to transparently access the files */ typedef struct mapping_t { /* begin is the first cluster, end is the last+1 */ - uint32_t begin,end; + uint32_t begin, end; /* as s->directory is growable, no pointer may be used here */ unsigned int dir_index; /* the clusters of a file may be in any order; this points to the first */ int first_mapping_index; union { - /* offset is - * - the offset in the file (in clusters) for a file, or - * - the next cluster of the directory for a directory, and - * - the address of the buffer for a faked entry - */ - struct { - uint32_t offset; - } file; - struct { - int parent_mapping_index; - int first_dir_index; - } dir; + /* offset is + * - the offset in the file (in clusters) for a file, or + * - the next cluster of the directory for a directory, and + * - the address of the buffer for a faked entry + */ + struct { + uint32_t offset; + } file; + struct { + int parent_mapping_index; + int first_dir_index; + } dir; } info; /* path contains the full path, i.e. it always starts with s->path */ char* path; - enum { MODE_UNDEFINED = 0, MODE_NORMAL = 1, MODE_MODIFIED = 2, - MODE_DIRECTORY = 4, MODE_FAKED = 8, - MODE_DELETED = 16, MODE_RENAMED = 32 } mode; + enum { + MODE_UNDEFINED = 0, + MODE_NORMAL = 1, + MODE_MODIFIED = 2, + MODE_DIRECTORY = 4, + MODE_FAKED = 8, + MODE_DELETED = 16, + MODE_RENAMED = 32 + } mode; int read_only; } mapping_t; #ifdef DEBUG -static void print_direntry(const struct direntry_t*); -static void print_mapping(const struct mapping_t* mapping); +static void print_direntry( const struct direntry_t* ); +static void print_mapping( const struct mapping_t* mapping ); #endif /* here begins the real VVFAT driver */ typedef struct BDRVVVFATState { - BlockDriverState* bs; /* pointer to parent */ + BlockDriverState* bs; /* pointer to parent */ unsigned int first_sectors_number; /* 1 for a single partition, 0x40 for a disk with partition table */ - unsigned char first_sectors[0x40*0x200]; - + unsigned char first_sectors[ 0x40 * 0x200 ]; + int fat_type; /* 16 or 32 */ - array_t fat,directory,mapping; - + array_t fat, directory, mapping; + unsigned int cluster_size; unsigned int sectors_per_cluster; unsigned int sectors_per_fat; unsigned int sectors_of_root_directory; uint32_t last_cluster_of_root_directory; unsigned int faked_sectors; /* how many sectors are faked before file data */ - uint32_t sector_count; /* total number of sectors of the partition */ - uint32_t cluster_count; /* total number of clusters of this partition */ + uint32_t sector_count; /* total number of sectors of the partition */ + uint32_t cluster_count; /* total number of clusters of this partition */ uint32_t max_fat_value; - + int current_fd; mapping_t* current_mapping; - unsigned char* cluster; /* points to current cluster */ + unsigned char* cluster; /* points to current cluster */ unsigned char* cluster_buffer; /* points to a buffer to hold temp data */ unsigned int current_cluster; @@ -390,307 +395,298 @@ typedef struct BDRVVVFATState { array_t commits; const char* path; int downcase_short_names; - QEMUTimer *write_timer; + QEMUTimer* write_timer; } BDRVVVFATState; - -static void init_mbr(BDRVVVFATState* s) +static void init_mbr( BDRVVVFATState* s ) { /* TODO: if the files mbr.img and bootsect.img exist, use them */ - mbr_t* real_mbr=(mbr_t*)s->first_sectors; - partition_t* partition=&(real_mbr->partition[0]); + mbr_t* real_mbr = ( mbr_t* )s->first_sectors; + partition_t* partition = &( real_mbr->partition[ 0 ] ); - memset(s->first_sectors,0,512); - - partition->attributes=0x80; /* bootable */ - partition->start_head=1; - partition->start_sector=1; - partition->start_cylinder=0; + memset( s->first_sectors, 0, 512 ); + + partition->attributes = 0x80; /* bootable */ + partition->start_head = 1; + partition->start_sector = 1; + partition->start_cylinder = 0; /* FAT12/FAT16/FAT32 */ - partition->fs_type=(s->fat_type==12?0x1:s->fat_type==16?0x6:0xb); - partition->end_head=s->bs->heads-1; - partition->end_sector=0xff; /* end sector & upper 2 bits of cylinder */; - partition->end_cylinder=0xff; /* lower 8 bits of end cylinder */; - partition->start_sector_long=cpu_to_le32(s->bs->secs); - partition->end_sector_long=cpu_to_le32(s->sector_count); + partition->fs_type = ( s->fat_type == 12 ? 0x1 : s->fat_type == 16 ? 0x6 : 0xb ); + partition->end_head = s->bs->heads - 1; + partition->end_sector = 0xff; /* end sector & upper 2 bits of cylinder */ + ; + partition->end_cylinder = 0xff; /* lower 8 bits of end cylinder */ + ; + partition->start_sector_long = cpu_to_le32( s->bs->secs ); + partition->end_sector_long = cpu_to_le32( s->sector_count ); - real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa; + real_mbr->magic[ 0 ] = 0x55; + real_mbr->magic[ 1 ] = 0xaa; } /* direntry functions */ /* dest is assumed to hold 258 bytes, and pads with 0xffff up to next multiple of 26 */ -static inline int short2long_name(char* dest,const char* src) +static inline int short2long_name( char* dest, const char* src ) { int i; - for(i=0;i<129 && src[i];i++) { - dest[2*i]=src[i]; - dest[2*i+1]=0; + for ( i = 0; i < 129 && src[ i ]; i++ ) { + dest[ 2 * i ] = src[ i ]; + dest[ 2 * i + 1 ] = 0; } - dest[2*i]=dest[2*i+1]=0; - for(i=2*i+2;(i%26);i++) - dest[i]=0xff; + dest[ 2 * i ] = dest[ 2 * i + 1 ] = 0; + for ( i = 2 * i + 2; ( i % 26 ); i++ ) + dest[ i ] = 0xff; return i; } -static inline direntry_t* create_long_filename(BDRVVVFATState* s,const char* filename) +static inline direntry_t* create_long_filename( BDRVVVFATState* s, const char* filename ) { - char buffer[258]; - int length=short2long_name(buffer,filename), - number_of_entries=(length+25)/26,i; + char buffer[ 258 ]; + int length = short2long_name( buffer, filename ), number_of_entries = ( length + 25 ) / 26, i; direntry_t* entry; - for(i=0;idirectory)); - entry->attributes=0xf; - entry->reserved[0]=0; - entry->begin=0; - entry->name[0]=(number_of_entries-i)|(i==0?0x40:0); + for ( i = 0; i < number_of_entries; i++ ) { + entry = array_get_next( &( s->directory ) ); + entry->attributes = 0xf; + entry->reserved[ 0 ] = 0; + entry->begin = 0; + entry->name[ 0 ] = ( number_of_entries - i ) | ( i == 0 ? 0x40 : 0 ); } - for(i=0;idirectory),s->directory.next-1-(i/26)); - entry->name[offset]=buffer[i]; + for ( i = 0; i < length; i++ ) { + int offset = ( i % 26 ); + if ( offset < 10 ) + offset = 1 + offset; + else if ( offset < 22 ) + offset = 14 + offset - 10; + else + offset = 28 + offset - 22; + entry = array_get( &( s->directory ), s->directory.next - 1 - ( i / 26 ) ); + entry->name[ offset ] = buffer[ i ]; } - return array_get(&(s->directory),s->directory.next-number_of_entries); + return array_get( &( s->directory ), s->directory.next - number_of_entries ); } -static char is_free(const direntry_t* direntry) +static char is_free( const direntry_t* direntry ) { /* return direntry->name[0]==0 ; */ - return direntry->attributes == 0 || direntry->name[0]==0xe5; + return direntry->attributes == 0 || direntry->name[ 0 ] == 0xe5; } -static char is_volume_label(const direntry_t* direntry) +static char is_volume_label( const direntry_t* direntry ) { return ( direntry->attributes & 0x1f ) == 0x08; } + +static char is_long_name( const direntry_t* direntry ) { return direntry->attributes == 0xf; } + +static char is_short_name( const direntry_t* direntry ) { - return (direntry->attributes & 0x1f) == 0x08; + return !is_volume_label( direntry ) && !is_long_name( direntry ) && !is_free( direntry ); } -static char is_long_name(const direntry_t* direntry) +static char is_directory( const direntry_t* direntry ) { return direntry->attributes & 0x10 && direntry->name[ 0 ] != 0xe5; } + +static inline char is_dot( const direntry_t* direntry ) { return is_short_name( direntry ) && direntry->name[ 0 ] == '.'; } + +static char is_file( const direntry_t* direntry ) { return is_short_name( direntry ) && !is_directory( direntry ); } + +static inline uint32_t begin_of_direntry( const direntry_t* direntry ) { - return direntry->attributes == 0xf; + return le16_to_cpu( direntry->begin ) | ( le16_to_cpu( direntry->begin_hi ) << 16 ); } -static char is_short_name(const direntry_t* direntry) -{ - return !is_volume_label(direntry) && !is_long_name(direntry) - && !is_free(direntry); -} +static inline uint32_t filesize_of_direntry( const direntry_t* direntry ) { return le32_to_cpu( direntry->size ); } -static char is_directory(const direntry_t* direntry) +static void set_begin_of_direntry( direntry_t* direntry, uint32_t begin ) { - return direntry->attributes & 0x10 && direntry->name[0] != 0xe5; -} - -static inline char is_dot(const direntry_t* direntry) -{ - return is_short_name(direntry) && direntry->name[0] == '.'; -} - -static char is_file(const direntry_t* direntry) -{ - return is_short_name(direntry) && !is_directory(direntry); -} - -static inline uint32_t begin_of_direntry(const direntry_t* direntry) -{ - return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16); -} - -static inline uint32_t filesize_of_direntry(const direntry_t* direntry) -{ - return le32_to_cpu(direntry->size); -} - -static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin) -{ - direntry->begin = cpu_to_le16(begin & 0xffff); - direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff); + direntry->begin = cpu_to_le16( begin & 0xffff ); + direntry->begin_hi = cpu_to_le16( ( begin >> 16 ) & 0xffff ); } /* fat functions */ -static inline uint8_t fat_chksum(const direntry_t* entry) +static inline uint8_t fat_chksum( const direntry_t* entry ) { - uint8_t chksum=0; + uint8_t chksum = 0; int i; - for(i=0;i<11;i++) - chksum=(((chksum&0xfe)>>1)|((chksum&0x01)?0x80:0)) - +(unsigned char)entry->name[i]; - + for ( i = 0; i < 11; i++ ) + chksum = ( ( ( chksum & 0xfe ) >> 1 ) | ( ( chksum & 0x01 ) ? 0x80 : 0 ) ) + ( unsigned char )entry->name[ i ]; + return chksum; } /* if return_time==0, this returns the fat_date, else the fat_time */ -static uint16_t fat_datetime(time_t time,int return_time) { +static uint16_t fat_datetime( time_t time, int return_time ) +{ struct tm* t; #ifdef _WIN32 - t=localtime(&time); /* this is not thread safe */ + t = localtime( &time ); /* this is not thread safe */ #else struct tm t1; - t=&t1; - localtime_r(&time,t); + t = &t1; + localtime_r( &time, t ); #endif - if(return_time) - return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11)); - return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9)); + if ( return_time ) + return cpu_to_le16( ( t->tm_sec / 2 ) | ( t->tm_min << 5 ) | ( t->tm_hour << 11 ) ); + return cpu_to_le16( ( t->tm_mday ) | ( ( t->tm_mon + 1 ) << 5 ) | ( ( t->tm_year - 80 ) << 9 ) ); } -static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value) +static inline void fat_set( BDRVVVFATState* s, unsigned int cluster, uint32_t value ) { - if(s->fat_type==32) { - uint32_t* entry=array_get(&(s->fat),cluster); - DLOG(fprintf(stderr, "%s:%u: cluster %u: %08x\n", __FUNCTION__, __LINE__, cluster, value)); - *entry=cpu_to_le32(value); - } else if(s->fat_type==16) { - uint16_t* entry=array_get(&(s->fat),cluster); - DLOG(fprintf(stderr, "%s:%u: cluster %u: %04x\n", __FUNCTION__, __LINE__, cluster, value & 0xffff)); - *entry=cpu_to_le16(value&0xffff); + if ( s->fat_type == 32 ) { + uint32_t* entry = array_get( &( s->fat ), cluster ); + DLOG( fprintf( stderr, "%s:%u: cluster %u: %08x\n", __FUNCTION__, __LINE__, cluster, value ) ); + *entry = cpu_to_le32( value ); + } else if ( s->fat_type == 16 ) { + uint16_t* entry = array_get( &( s->fat ), cluster ); + DLOG( fprintf( stderr, "%s:%u: cluster %u: %04x\n", __FUNCTION__, __LINE__, cluster, value & 0xffff ) ); + *entry = cpu_to_le16( value & 0xffff ); } else { - int offset = (cluster*3/2); - unsigned char* p = array_get(&(s->fat), offset); - DLOG(fprintf(stderr, "%s:%u: cluster %u: %03x\n", __FUNCTION__, __LINE__, cluster, value & 0xfff)); - switch (cluster&1) { - case 0: - p[0] = value&0xff; - p[1] = (p[1]&0xf0) | ((value>>8)&0xf); - break; - case 1: - p[0] = (p[0]&0xf) | ((value&0xf)<<4); - p[1] = (value>>4); - break; - } + int offset = ( cluster * 3 / 2 ); + unsigned char* p = array_get( &( s->fat ), offset ); + DLOG( fprintf( stderr, "%s:%u: cluster %u: %03x\n", __FUNCTION__, __LINE__, cluster, value & 0xfff ) ); + switch ( cluster & 1 ) { + case 0: + p[ 0 ] = value & 0xff; + p[ 1 ] = ( p[ 1 ] & 0xf0 ) | ( ( value >> 8 ) & 0xf ); + break; + case 1: + p[ 0 ] = ( p[ 0 ] & 0xf ) | ( ( value & 0xf ) << 4 ); + p[ 1 ] = ( value >> 4 ); + break; + } } } -static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster) +static inline uint32_t fat_get( BDRVVVFATState* s, unsigned int cluster ) { - if(s->fat_type==32) { - uint32_t* entry=array_get(&(s->fat),cluster); - return le32_to_cpu(*entry); - } else if(s->fat_type==16) { - uint16_t* entry=array_get(&(s->fat),cluster); - return le16_to_cpu(*entry); + if ( s->fat_type == 32 ) { + uint32_t* entry = array_get( &( s->fat ), cluster ); + return le32_to_cpu( *entry ); + } else if ( s->fat_type == 16 ) { + uint16_t* entry = array_get( &( s->fat ), cluster ); + return le16_to_cpu( *entry ); } else { - const uint8_t* x=s->fat.pointer+cluster*3/2; - return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff; + const uint8_t* x = s->fat.pointer + cluster * 3 / 2; + return ( ( x[ 0 ] | ( x[ 1 ] << 8 ) ) >> ( cluster & 1 ? 4 : 0 ) ) & 0x0fff; } } -static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry) +static inline int fat_eof( BDRVVVFATState* s, uint32_t fat_entry ) { - if(fat_entry>s->max_fat_value-8) - return -1; + if ( fat_entry > s->max_fat_value - 8 ) + return -1; return 0; } -static inline void init_fat(BDRVVVFATState* s) +static inline void init_fat( BDRVVVFATState* s ) { - if (s->fat_type == 12) { - array_init(&(s->fat),1); - array_ensure_allocated(&(s->fat), - s->sectors_per_fat * 0x200 * 3 / 2 - 1); + if ( s->fat_type == 12 ) { + array_init( &( s->fat ), 1 ); + array_ensure_allocated( &( s->fat ), s->sectors_per_fat * 0x200 * 3 / 2 - 1 ); } else { - array_init(&(s->fat),(s->fat_type==32?4:2)); - array_ensure_allocated(&(s->fat), - s->sectors_per_fat * 0x200 / s->fat.item_size - 1); + array_init( &( s->fat ), ( s->fat_type == 32 ? 4 : 2 ) ); + array_ensure_allocated( &( s->fat ), s->sectors_per_fat * 0x200 / s->fat.item_size - 1 ); } - memset(s->fat.pointer,0,s->fat.size); - - switch(s->fat_type) { - case 12: s->max_fat_value=0xfff; break; - case 16: s->max_fat_value=0xffff; break; - case 32: s->max_fat_value=0x0fffffff; break; - default: s->max_fat_value=0; /* error... */ + memset( s->fat.pointer, 0, s->fat.size ); + + switch ( s->fat_type ) { + case 12: + s->max_fat_value = 0xfff; + break; + case 16: + s->max_fat_value = 0xffff; + break; + case 32: + s->max_fat_value = 0x0fffffff; + break; + default: + s->max_fat_value = 0; /* error... */ } } /* TODO: in create_short_filename, 0xe5->0x05 is not yet handled! */ /* TODO: in parse_short_filename, 0x05->0xe5 is not yet handled! */ -static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s, - unsigned int directory_start, const char* filename, int is_dot) +static inline direntry_t* create_short_and_long_name( BDRVVVFATState* s, unsigned int directory_start, const char* filename, int is_dot ) { - int i,j,long_index=s->directory.next; - direntry_t* entry=0; - direntry_t* entry_long=0; + int i, j, long_index = s->directory.next; + direntry_t* entry = 0; + direntry_t* entry_long = 0; - if(is_dot) { - entry=array_get_next(&(s->directory)); - memset(entry->name,' ',11); - memcpy(entry->name,filename,strlen(filename)); - return entry; + if ( is_dot ) { + entry = array_get_next( &( s->directory ) ); + memset( entry->name, ' ', 11 ); + memcpy( entry->name, filename, strlen( filename ) ); + return entry; } - - entry_long=create_long_filename(s,filename); - - i = strlen(filename); - for(j = i - 1; j>0 && filename[j]!='.';j--); - if (j > 0) - i = (j > 8 ? 8 : j); - else if (i > 8) - i = 8; - entry=array_get_next(&(s->directory)); - memset(entry->name,' ',11); - memcpy(entry->name,filename,i); - - if(j > 0) - for (i = 0; i < 3 && filename[j+1+i]; i++) - entry->extension[i] = filename[j+1+i]; + entry_long = create_long_filename( s, filename ); + + i = strlen( filename ); + for ( j = i - 1; j > 0 && filename[ j ] != '.'; j-- ) + ; + if ( j > 0 ) + i = ( j > 8 ? 8 : j ); + else if ( i > 8 ) + i = 8; + + entry = array_get_next( &( s->directory ) ); + memset( entry->name, ' ', 11 ); + memcpy( entry->name, filename, i ); + + if ( j > 0 ) + for ( i = 0; i < 3 && filename[ j + 1 + i ]; i++ ) + entry->extension[ i ] = filename[ j + 1 + i ]; /* upcase & remove unwanted characters */ - for(i=10;i>=0;i--) { - if(i==10 || i==7) for(;i>0 && entry->name[i]==' ';i--); - if(entry->name[i]<=' ' || entry->name[i]>0x7f - || strchr(".*?<>|\":/\\[];,+='",entry->name[i])) - entry->name[i]='_'; - else if(entry->name[i]>='a' && entry->name[i]<='z') - entry->name[i]+='A'-'a'; + for ( i = 10; i >= 0; i-- ) { + if ( i == 10 || i == 7 ) + for ( ; i > 0 && entry->name[ i ] == ' '; i-- ) + ; + if ( entry->name[ i ] <= ' ' || entry->name[ i ] > 0x7f || strchr( ".*?<>|\":/\\[];,+='", entry->name[ i ] ) ) + entry->name[ i ] = '_'; + else if ( entry->name[ i ] >= 'a' && entry->name[ i ] <= 'z' ) + entry->name[ i ] += 'A' - 'a'; } /* mangle duplicates */ - while(1) { - direntry_t* entry1=array_get(&(s->directory),directory_start); - int j; + while ( 1 ) { + direntry_t* entry1 = array_get( &( s->directory ), directory_start ); + int j; - for(;entry1name,entry->name,11)) - break; /* found dupe */ - if(entry1==entry) /* no dupe found */ - break; + for ( ; entry1 < entry; entry1++ ) + if ( !is_long_name( entry1 ) && !memcmp( entry1->name, entry->name, 11 ) ) + break; /* found dupe */ + if ( entry1 == entry ) /* no dupe found */ + break; - /* use all 8 characters of name */ - if(entry->name[7]==' ') { - int j; - for(j=6;j>0 && entry->name[j]==' ';j--) - entry->name[j]='~'; - } + /* use all 8 characters of name */ + if ( entry->name[ 7 ] == ' ' ) { + int j; + for ( j = 6; j > 0 && entry->name[ j ] == ' '; j-- ) + entry->name[ j ] = '~'; + } - /* increment number */ - for(j=7;j>0 && entry->name[j]=='9';j--) - entry->name[j]='0'; - if(j>0) { - if(entry->name[j]<'0' || entry->name[j]>'9') - entry->name[j]='0'; - else - entry->name[j]++; - } + /* increment number */ + for ( j = 7; j > 0 && entry->name[ j ] == '9'; j-- ) + entry->name[ j ] = '0'; + if ( j > 0 ) { + if ( entry->name[ j ] < '0' || entry->name[ j ] > '9' ) + entry->name[ j ] = '0'; + else + entry->name[ j ]++; + } } /* calculate checksum; propagate to long name */ - if(entry_long) { - uint8_t chksum=fat_chksum(entry); + if ( entry_long ) { + uint8_t chksum = fat_chksum( entry ); - /* calculate anew, because realloc could have taken place */ - entry_long=array_get(&(s->directory),long_index); - while(entry_longreserved[1]=chksum; - entry_long++; - } + /* calculate anew, because realloc could have taken place */ + entry_long = array_get( &( s->directory ), long_index ); + while ( entry_long < entry && is_long_name( entry_long ) ) { + entry_long->reserved[ 1 ] = chksum; + entry_long++; + } } return entry; @@ -699,175 +695,167 @@ static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s, /* * Read a directory. (the index of the corresponding mapping must be passed). */ -static int read_directory(BDRVVVFATState* s, int mapping_index) +static int read_directory( BDRVVVFATState* s, int mapping_index ) { - mapping_t* mapping = array_get(&(s->mapping), mapping_index); + mapping_t* mapping = array_get( &( s->mapping ), mapping_index ); direntry_t* direntry; const char* dirname = mapping->path; int first_cluster = mapping->begin; int parent_index = mapping->info.dir.parent_mapping_index; - mapping_t* parent_mapping = (mapping_t*) - (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : 0); + mapping_t* parent_mapping = ( mapping_t* )( parent_index >= 0 ? array_get( &( s->mapping ), parent_index ) : 0 ); int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1; - DIR* dir=opendir(dirname); + DIR* dir = opendir( dirname ); struct dirent* entry; int i; - assert(mapping->mode & MODE_DIRECTORY); + assert( mapping->mode & MODE_DIRECTORY ); - if(!dir) { - mapping->end = mapping->begin; - return -1; + if ( !dir ) { + mapping->end = mapping->begin; + return -1; } - - i = mapping->info.dir.first_dir_index = - first_cluster == 0 ? 0 : s->directory.next; - /* actually read the directory, and allocate the mappings */ - while((entry=readdir(dir))) { - unsigned int length=strlen(dirname)+2+strlen(entry->d_name); + i = mapping->info.dir.first_dir_index = first_cluster == 0 ? 0 : s->directory.next; + + /* actually read the directory, and allocate the mappings */ + while ( ( entry = readdir( dir ) ) ) { + unsigned int length = strlen( dirname ) + 2 + strlen( entry->d_name ); char* buffer; - direntry_t* direntry; + direntry_t* direntry; struct stat st; - int is_dot=!strcmp(entry->d_name,"."); - int is_dotdot=!strcmp(entry->d_name,".."); + int is_dot = !strcmp( entry->d_name, "." ); + int is_dotdot = !strcmp( entry->d_name, ".." ); - if(first_cluster == 0 && (is_dotdot || is_dot)) - continue; - - buffer=(char*)malloc(length); - assert(buffer); - snprintf(buffer,length,"%s/%s",dirname,entry->d_name); - - if(stat(buffer,&st)<0) { - free(buffer); + if ( first_cluster == 0 && ( is_dotdot || is_dot ) ) continue; - } - DLOG(fprintf(stderr, "%s:%u: create direntry for '%s'\n", __FUNCTION__, __LINE__, entry->d_name)); + buffer = ( char* )malloc( length ); + assert( buffer ); + snprintf( buffer, length, "%s/%s", dirname, entry->d_name ); - /* create directory entry for this file */ - direntry=create_short_and_long_name(s, i, entry->d_name, - is_dot || is_dotdot); - direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20); - direntry->reserved[0]=direntry->reserved[1]=0; - direntry->ctime=fat_datetime(st.st_ctime,1); - direntry->cdate=fat_datetime(st.st_ctime,0); - direntry->adate=fat_datetime(st.st_atime,0); - direntry->begin_hi=0; - direntry->mtime=fat_datetime(st.st_mtime,1); - direntry->mdate=fat_datetime(st.st_mtime,0); - if(is_dotdot) - set_begin_of_direntry(direntry, first_cluster_of_parent); - else if(is_dot) - set_begin_of_direntry(direntry, first_cluster); - else - direntry->begin=0; /* do that later */ - if (st.st_size > 0x7fffffff) { - fprintf(stderr, "File %s is larger than 2GB\n", buffer); - free(buffer); - return -2; + if ( stat( buffer, &st ) < 0 ) { + free( buffer ); + continue; } - direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size); - /* create mapping for this file */ - if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) { - DLOG(fprintf(stderr, "%s:%u: create mapping for '%s'\n", __FUNCTION__, __LINE__, entry->d_name)); + DLOG( fprintf( stderr, "%s:%u: create direntry for '%s'\n", __FUNCTION__, __LINE__, entry->d_name ) ); - s->current_mapping=(mapping_t*)array_get_next(&(s->mapping)); - s->current_mapping->begin=0; - s->current_mapping->end=st.st_size; - /* - * we get the direntry of the most recent direntry, which - * contains the short name and all the relevant information. - */ - s->current_mapping->dir_index=s->directory.next-1; - s->current_mapping->first_mapping_index = -1; - if (S_ISDIR(st.st_mode)) { - s->current_mapping->mode = MODE_DIRECTORY; - s->current_mapping->info.dir.parent_mapping_index = - mapping_index; - } else { - s->current_mapping->mode = MODE_UNDEFINED; - s->current_mapping->info.file.offset = 0; - } - s->current_mapping->path=buffer; - s->current_mapping->read_only = - (st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0; - } + /* create directory entry for this file */ + direntry = create_short_and_long_name( s, i, entry->d_name, is_dot || is_dotdot ); + direntry->attributes = ( S_ISDIR( st.st_mode ) ? 0x10 : 0x20 ); + direntry->reserved[ 0 ] = direntry->reserved[ 1 ] = 0; + direntry->ctime = fat_datetime( st.st_ctime, 1 ); + direntry->cdate = fat_datetime( st.st_ctime, 0 ); + direntry->adate = fat_datetime( st.st_atime, 0 ); + direntry->begin_hi = 0; + direntry->mtime = fat_datetime( st.st_mtime, 1 ); + direntry->mdate = fat_datetime( st.st_mtime, 0 ); + if ( is_dotdot ) + set_begin_of_direntry( direntry, first_cluster_of_parent ); + else if ( is_dot ) + set_begin_of_direntry( direntry, first_cluster ); + else + direntry->begin = 0; /* do that later */ + if ( st.st_size > 0x7fffffff ) { + fprintf( stderr, "File %s is larger than 2GB\n", buffer ); + free( buffer ); + return -2; + } + direntry->size = cpu_to_le32( S_ISDIR( st.st_mode ) ? 0 : st.st_size ); + + /* create mapping for this file */ + if ( !is_dot && !is_dotdot && ( S_ISDIR( st.st_mode ) || st.st_size ) ) { + DLOG( fprintf( stderr, "%s:%u: create mapping for '%s'\n", __FUNCTION__, __LINE__, entry->d_name ) ); + + s->current_mapping = ( mapping_t* )array_get_next( &( s->mapping ) ); + s->current_mapping->begin = 0; + s->current_mapping->end = st.st_size; + /* + * we get the direntry of the most recent direntry, which + * contains the short name and all the relevant information. + */ + s->current_mapping->dir_index = s->directory.next - 1; + s->current_mapping->first_mapping_index = -1; + if ( S_ISDIR( st.st_mode ) ) { + s->current_mapping->mode = MODE_DIRECTORY; + s->current_mapping->info.dir.parent_mapping_index = mapping_index; + } else { + s->current_mapping->mode = MODE_UNDEFINED; + s->current_mapping->info.file.offset = 0; + } + s->current_mapping->path = buffer; + s->current_mapping->read_only = ( st.st_mode & ( S_IWUSR | S_IWGRP | S_IWOTH ) ) == 0; + } } - closedir(dir); + closedir( dir ); /* fill with zeroes up to the end of the cluster */ - while(s->directory.next%(0x10*s->sectors_per_cluster)) { - direntry_t* direntry=array_get_next(&(s->directory)); - memset(direntry,0,sizeof(direntry_t)); + while ( s->directory.next % ( 0x10 * s->sectors_per_cluster ) ) { + direntry_t* direntry = array_get_next( &( s->directory ) ); + memset( direntry, 0, sizeof( direntry_t ) ); } /* TODO: if there are more entries, bootsector has to be adjusted! */ -#define ROOT_ENTRIES (0x02 * 0x10 * s->sectors_per_cluster) - if (mapping_index == 0 && s->directory.next < ROOT_ENTRIES) { - /* root directory */ - int cur = s->directory.next; - array_ensure_allocated(&(s->directory), ROOT_ENTRIES - 1); - memset(array_get(&(s->directory), cur), 0, - (ROOT_ENTRIES - cur) * sizeof(direntry_t)); +#define ROOT_ENTRIES ( 0x02 * 0x10 * s->sectors_per_cluster ) + if ( mapping_index == 0 && s->directory.next < ROOT_ENTRIES ) { + /* root directory */ + int cur = s->directory.next; + array_ensure_allocated( &( s->directory ), ROOT_ENTRIES - 1 ); + memset( array_get( &( s->directory ), cur ), 0, ( ROOT_ENTRIES - cur ) * sizeof( direntry_t ) ); } - - /* reget the mapping, since s->mapping was possibly realloc()ed */ - mapping = (mapping_t*)array_get(&(s->mapping), mapping_index); - first_cluster += (s->directory.next - mapping->info.dir.first_dir_index) - * 0x20 / s->cluster_size; + + /* reget the mapping, since s->mapping was possibly realloc()ed */ + mapping = ( mapping_t* )array_get( &( s->mapping ), mapping_index ); + first_cluster += ( s->directory.next - mapping->info.dir.first_dir_index ) * 0x20 / s->cluster_size; mapping->end = first_cluster; - direntry = (direntry_t*)array_get(&(s->directory), mapping->dir_index); - set_begin_of_direntry(direntry, mapping->begin); - + direntry = ( direntry_t* )array_get( &( s->directory ), mapping->dir_index ); + set_begin_of_direntry( direntry, mapping->begin ); + return 0; } -static inline int32_t sector2cluster(BDRVVVFATState* s, off_t sector_num) +static inline int32_t sector2cluster( BDRVVVFATState* s, off_t sector_num ) { - return (sector_num - s->faked_sectors) / s->sectors_per_cluster; + return ( sector_num - s->faked_sectors ) / s->sectors_per_cluster; } -static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num) +static inline off_t cluster2sector( BDRVVVFATState* s, uint32_t cluster_num ) { return s->faked_sectors + s->sectors_per_cluster * cluster_num; } -static inline uint32_t sector_offset_in_cluster(BDRVVVFATState* s,off_t sector_num) +static inline uint32_t sector_offset_in_cluster( BDRVVVFATState* s, off_t sector_num ) { - return (sector_num-s->first_sectors_number-2*s->sectors_per_fat)%s->sectors_per_cluster; + return ( sector_num - s->first_sectors_number - 2 * s->sectors_per_fat ) % s->sectors_per_cluster; } #ifdef DBG -static direntry_t* get_direntry_for_mapping(BDRVVVFATState* s,mapping_t* mapping) +static direntry_t* get_direntry_for_mapping( BDRVVVFATState* s, mapping_t* mapping ) { - if(mapping->mode==MODE_UNDEFINED) - return 0; - return (direntry_t*)(s->directory.pointer+sizeof(direntry_t)*mapping->dir_index); + if ( mapping->mode == MODE_UNDEFINED ) + return 0; + return ( direntry_t* )( s->directory.pointer + sizeof( direntry_t ) * mapping->dir_index ); } #endif -static int init_directories(BDRVVVFATState* s, - const char* dirname) +static int init_directories( BDRVVVFATState* s, const char* dirname ) { - msdos_volume_info_t *vi; + msdos_volume_info_t* vi; bootsector_t* bootsector; mapping_t* mapping; unsigned int i; unsigned int cluster; - direntry_t *entry; + direntry_t* entry; struct stat st; size_t namelen; - memset(&(s->first_sectors[0]),0,0x40*0x200); + memset( &( s->first_sectors[ 0 ] ), 0, 0x40 * 0x200 ); s->cluster_size = s->sectors_per_cluster * 0x200; - s->cluster_buffer = malloc(s->cluster_size); - assert(s->cluster_buffer); + s->cluster_buffer = malloc( s->cluster_size ); + assert( s->cluster_buffer ); /* * The formula: sc = spf+1+spf*spc*(512*8/fat_type), @@ -877,186 +865,190 @@ static int init_directories(BDRVVVFATState* s, * fat_type = 12, 16 or 32. */ i = 1 + s->sectors_per_cluster * 0x200 * 8 / s->fat_type; - s->sectors_per_fat = (s->sector_count + i) / i; /* round up */ - - array_init(&(s->mapping),sizeof(mapping_t)); - array_init(&(s->directory),sizeof(direntry_t)); + s->sectors_per_fat = ( s->sector_count + i ) / i; /* round up */ - DLOG(fprintf(stderr, "%s:%u: create direntry for '%s'\n", __FUNCTION__, __LINE__, dirname)); + array_init( &( s->mapping ), sizeof( mapping_t ) ); + array_init( &( s->directory ), sizeof( direntry_t ) ); + + DLOG( fprintf( stderr, "%s:%u: create direntry for '%s'\n", __FUNCTION__, __LINE__, dirname ) ); /* add volume label */ - stat(dirname, &st); - entry = array_get_next(&(s->directory)); + stat( dirname, &st ); + entry = array_get_next( &( s->directory ) ); entry->attributes = 0x08; /* archive | volume label */ - memset(entry->name, ' ', 8); - memset(entry->extension, ' ', 3); - namelen = strlen(VOLUME_LABEL); - if (namelen > 11) - namelen = 11; - memcpy(entry->name, VOLUME_LABEL, namelen > 8 ? 8 : namelen); - if (namelen > 8) - memcpy(entry->extension, VOLUME_LABEL+8, namelen-8); - entry->reserved[0] = entry->reserved[1] = 0; - entry->ctime = fat_datetime(st.st_ctime, 1); - entry->cdate = fat_datetime(st.st_ctime, 0); - entry->adate = fat_datetime(st.st_atime, 0); + memset( entry->name, ' ', 8 ); + memset( entry->extension, ' ', 3 ); + namelen = strlen( VOLUME_LABEL ); + if ( namelen > 11 ) + namelen = 11; + memcpy( entry->name, VOLUME_LABEL, namelen > 8 ? 8 : namelen ); + if ( namelen > 8 ) + memcpy( entry->extension, VOLUME_LABEL + 8, namelen - 8 ); + entry->reserved[ 0 ] = entry->reserved[ 1 ] = 0; + entry->ctime = fat_datetime( st.st_ctime, 1 ); + entry->cdate = fat_datetime( st.st_ctime, 0 ); + entry->adate = fat_datetime( st.st_atime, 0 ); entry->begin_hi = 0; - entry->mtime = fat_datetime(st.st_ctime, 1); - entry->mdate = fat_datetime(st.st_ctime, 0); + entry->mtime = fat_datetime( st.st_ctime, 1 ); + entry->mdate = fat_datetime( st.st_ctime, 0 ); entry->begin = 0; entry->size = 0; /* Now build FAT, and write back information into directory */ - init_fat(s); + init_fat( s ); - s->faked_sectors=s->first_sectors_number+s->sectors_per_fat*2; - s->cluster_count=sector2cluster(s, s->sector_count); + s->faked_sectors = s->first_sectors_number + s->sectors_per_fat * 2; + s->cluster_count = sector2cluster( s, s->sector_count ); - DLOG(fprintf(stderr, "%s:%u: create mapping for '%s'\n", __FUNCTION__, __LINE__, dirname)); + DLOG( fprintf( stderr, "%s:%u: create mapping for '%s'\n", __FUNCTION__, __LINE__, dirname ) ); - mapping = array_get_next(&(s->mapping)); + mapping = array_get_next( &( s->mapping ) ); mapping->begin = 0; mapping->dir_index = 0; mapping->info.dir.parent_mapping_index = -1; mapping->first_mapping_index = -1; - mapping->path = strdup(dirname); - i = strlen(mapping->path); - if (i > 0 && mapping->path[i - 1] == '/') - mapping->path[i - 1] = '\0'; + mapping->path = strdup( dirname ); + i = strlen( mapping->path ); + if ( i > 0 && mapping->path[ i - 1 ] == '/' ) + mapping->path[ i - 1 ] = '\0'; mapping->mode = MODE_DIRECTORY; mapping->read_only = 0; s->path = mapping->path; - for (i = 0, cluster = 0; i < s->mapping.next; i++) { - int j; - /* MS-DOS expects the FAT to be 0 for the root directory - * (except for the media byte). */ - /* LATER TODO: still true for FAT32? */ - int fix_fat = (i != 0); - mapping = array_get(&(s->mapping), i); + for ( i = 0, cluster = 0; i < s->mapping.next; i++ ) { + int j; + /* MS-DOS expects the FAT to be 0 for the root directory + * (except for the media byte). */ + /* LATER TODO: still true for FAT32? */ + int fix_fat = ( i != 0 ); + mapping = array_get( &( s->mapping ), i ); - if (mapping->mode & MODE_DIRECTORY) { - mapping->begin = cluster; - if(read_directory(s, i)) { - fprintf(stderr, "Could not read directory %s\n", - mapping->path); - return -1; - } - mapping = array_get(&(s->mapping), i); - } else { - assert(mapping->mode == MODE_UNDEFINED); - mapping->mode=MODE_NORMAL; - mapping->begin = cluster; - if (mapping->end > 0) { - direntry_t* direntry = array_get(&(s->directory), - mapping->dir_index); + if ( mapping->mode & MODE_DIRECTORY ) { + mapping->begin = cluster; + if ( read_directory( s, i ) ) { + fprintf( stderr, "Could not read directory %s\n", mapping->path ); + return -1; + } + mapping = array_get( &( s->mapping ), i ); + } else { + assert( mapping->mode == MODE_UNDEFINED ); + mapping->mode = MODE_NORMAL; + mapping->begin = cluster; + if ( mapping->end > 0 ) { + direntry_t* direntry = array_get( &( s->directory ), mapping->dir_index ); - mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size; - set_begin_of_direntry(direntry, mapping->begin); - } else { - mapping->end = cluster + 1; - fix_fat = 0; - } - } + mapping->end = cluster + 1 + ( mapping->end - 1 ) / s->cluster_size; + set_begin_of_direntry( direntry, mapping->begin ); + } else { + mapping->end = cluster + 1; + fix_fat = 0; + } + } - assert(mapping->begin < mapping->end); + assert( mapping->begin < mapping->end ); - /* fix fat for entry */ - if (fix_fat) { - for(j = mapping->begin; j < mapping->end - 1; j++) - fat_set(s, j, j+1); - fat_set(s, mapping->end - 1, s->max_fat_value); - } + /* fix fat for entry */ + if ( fix_fat ) { + for ( j = mapping->begin; j < mapping->end - 1; j++ ) + fat_set( s, j, j + 1 ); + fat_set( s, mapping->end - 1, s->max_fat_value ); + } - /* next free cluster */ - cluster = mapping->end; + /* next free cluster */ + cluster = mapping->end; - if(cluster > s->cluster_count) { - fprintf(stderr,"Directory does not fit in FAT%d\n",s->fat_type); - return -1; - } + if ( cluster > s->cluster_count ) { + fprintf( stderr, "Directory does not fit in FAT%d\n", s->fat_type ); + return -1; + } } - mapping = array_get(&(s->mapping), 0); + mapping = array_get( &( s->mapping ), 0 ); s->sectors_of_root_directory = mapping->end * s->sectors_per_cluster; s->last_cluster_of_root_directory = mapping->end; /* the FAT signature */ - switch (s->fat_type) { - case 12: fat_set(s, 0, s->sector_count == 5760 ? 0xff9 : 0xff8); break; - case 16: fat_set(s, 0, 0xfff8); break; - case 32: fat_set(s, 0, 0x0ffffff0); break; - default: fat_set(s,0,s->max_fat_value); break; + switch ( s->fat_type ) { + case 12: + fat_set( s, 0, s->sector_count == 5760 ? 0xff9 : 0xff8 ); + break; + case 16: + fat_set( s, 0, 0xfff8 ); + break; + case 32: + fat_set( s, 0, 0x0ffffff0 ); + break; + default: + fat_set( s, 0, s->max_fat_value ); + break; } - fat_set(s,1,s->max_fat_value); + fat_set( s, 1, s->max_fat_value ); s->current_mapping = NULL; - bootsector=(bootsector_t*)(s->first_sectors+(s->first_sectors_number-1)*0x200); + bootsector = ( bootsector_t* )( s->first_sectors + ( s->first_sectors_number - 1 ) * 0x200 ); - memcpy(bootsector->jump, dummy_boot_jump, 3); - bootsector->jump[1] = ((s->fat_type == 32 ? - (char *) &bootsector->u.fat32.boot_code : - (char *) &bootsector->u.fat16.boot_code) - - (char *) &bootsector) - 2; - if (s->fat_type == 32) { - int offset = (char *) &bootsector->u.fat32.boot_code - - (char *) &bootsector + MESSAGE_OFFSET + 0x7c00; - memcpy(bootsector->u.fat32.boot_code, dummy_boot_code, - BOOTCODE_FAT32_SIZE); - bootsector->u.fat32.boot_code[MSG_OFFSET_OFFSET + 0] = offset & 0xff; - bootsector->u.fat32.boot_code[MSG_OFFSET_OFFSET + 1] = offset >> 8; + memcpy( bootsector->jump, dummy_boot_jump, 3 ); + bootsector->jump[ 1 ] = ( ( s->fat_type == 32 ? ( char* )&bootsector->u.fat32.boot_code : ( char* )&bootsector->u.fat16.boot_code ) - + ( char* )&bootsector ) - + 2; + if ( s->fat_type == 32 ) { + int offset = ( char* )&bootsector->u.fat32.boot_code - ( char* )&bootsector + MESSAGE_OFFSET + 0x7c00; + memcpy( bootsector->u.fat32.boot_code, dummy_boot_code, BOOTCODE_FAT32_SIZE ); + bootsector->u.fat32.boot_code[ MSG_OFFSET_OFFSET + 0 ] = offset & 0xff; + bootsector->u.fat32.boot_code[ MSG_OFFSET_OFFSET + 1 ] = offset >> 8; } else { - memcpy(bootsector->u.fat16.boot_code, dummy_boot_code, BOOTCODE_SIZE); + memcpy( bootsector->u.fat16.boot_code, dummy_boot_code, BOOTCODE_SIZE ); } - memcpy(bootsector->name,"X49GP ",8); - bootsector->sector_size=cpu_to_le16(0x200); - bootsector->sectors_per_cluster=s->sectors_per_cluster; - bootsector->reserved_sectors=cpu_to_le16(1); - bootsector->number_of_fats=0x2; /* number of FATs */ - bootsector->root_entries=cpu_to_le16(s->sectors_of_root_directory*0x10); - bootsector->total_sectors16=s->sector_count>0xffff?0:cpu_to_le16(s->sector_count); - bootsector->media_type=(s->fat_type!=12?0xf8:s->sector_count==5760?0xf9:0xf8); /* media descriptor */ - bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat); - bootsector->sectors_per_track=cpu_to_le16(s->bs->secs); - bootsector->number_of_heads=cpu_to_le16(s->bs->heads); - bootsector->hidden_sectors=cpu_to_le32(s->first_sectors_number==1?0:0x3f); - bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0); + memcpy( bootsector->name, "X49GP ", 8 ); + bootsector->sector_size = cpu_to_le16( 0x200 ); + bootsector->sectors_per_cluster = s->sectors_per_cluster; + bootsector->reserved_sectors = cpu_to_le16( 1 ); + bootsector->number_of_fats = 0x2; /* number of FATs */ + bootsector->root_entries = cpu_to_le16( s->sectors_of_root_directory * 0x10 ); + bootsector->total_sectors16 = s->sector_count > 0xffff ? 0 : cpu_to_le16( s->sector_count ); + bootsector->media_type = ( s->fat_type != 12 ? 0xf8 : s->sector_count == 5760 ? 0xf9 : 0xf8 ); /* media descriptor */ + bootsector->sectors_per_fat = cpu_to_le16( s->sectors_per_fat ); + bootsector->sectors_per_track = cpu_to_le16( s->bs->secs ); + bootsector->number_of_heads = cpu_to_le16( s->bs->heads ); + bootsector->hidden_sectors = cpu_to_le32( s->first_sectors_number == 1 ? 0 : 0x3f ); + bootsector->total_sectors = cpu_to_le32( s->sector_count > 0xffff ? s->sector_count : 0 ); /* LATER TODO: if FAT32, this is wrong */ - if (s->fat_type == 32) { - vi = &bootsector->u.fat32.vi; - memcpy(vi->fs_type, "FAT32 ", 8); + if ( s->fat_type == 32 ) { + vi = &bootsector->u.fat32.vi; + memcpy( vi->fs_type, "FAT32 ", 8 ); } else { - vi = &bootsector->u.fat16.vi; - memcpy(vi->fs_type, (s->fat_type == 12 ? "FAT12 " : "FAT16 "), 8); + vi = &bootsector->u.fat16.vi; + memcpy( vi->fs_type, ( s->fat_type == 12 ? "FAT12 " : "FAT16 " ), 8 ); } /* assume this is hda (TODO) */ vi->drive_number = s->fat_type == 12 ? 0 : 0x80; vi->signature = 0x29; - vi->id = cpu_to_le32(0xfabe1afd); - memset(vi->volume_label, ' ', 11); - namelen = strlen(VOLUME_LABEL); - if (namelen > 11) - namelen = 11; - memcpy(vi->volume_label, VOLUME_LABEL, namelen); + vi->id = cpu_to_le32( 0xfabe1afd ); + memset( vi->volume_label, ' ', 11 ); + namelen = strlen( VOLUME_LABEL ); + if ( namelen > 11 ) + namelen = 11; + memcpy( vi->volume_label, VOLUME_LABEL, namelen ); - bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa; + bootsector->magic[ 0 ] = 0x55; + bootsector->magic[ 1 ] = 0xaa; return 0; } #ifdef DEBUG -static BDRVVVFATState *vvv = NULL; +static BDRVVVFATState* vvv = NULL; #endif -static int enable_write(BDRVVVFATState *s); -static int is_consistent(BDRVVVFATState *s); +static int enable_write( BDRVVVFATState* s ); +static int is_consistent( BDRVVVFATState* s ); -static int vvfat_open(BlockDriverState *bs, const char* dirname, int flags) +static int vvfat_open( BlockDriverState* bs, const char* dirname, int flags ) { - BDRVVVFATState *s = bs->opaque; + BDRVVVFATState* s = bs->opaque; int floppy = 0; int i; @@ -1064,98 +1056,102 @@ static int vvfat_open(BlockDriverState *bs, const char* dirname, int flags) vvv = s; #endif - DLOG(if (stderr == NULL) { - stderr = fopen("vvfat.log", "a"); - setbuf(stderr, NULL); - }) + DLOG( if ( stderr == NULL ) { + stderr = fopen( "vvfat.log", "a" ); + setbuf( stderr, NULL ); + } ) s->bs = bs; - s->fat_type=16; + s->fat_type = 16; /* LATER TODO: if FAT32, adjust */ - s->sector_count=0xec04f; - s->sectors_per_cluster=0x10; + s->sector_count = 0xec04f; + s->sectors_per_cluster = 0x10; /* LATER TODO: this could be wrong for FAT32 */ - bs->cyls=1023; bs->heads=15; bs->secs=63; + bs->cyls = 1023; + bs->heads = 15; + bs->secs = 63; - s->current_cluster=0xffffffff; + s->current_cluster = 0xffffffff; - s->first_sectors_number=0x40; + s->first_sectors_number = 0x40; /* read only is the default for safety */ bs->read_only = 1; s->qcow = NULL; s->qcow_filename = NULL; s->fat2 = NULL; s->downcase_short_names = 1; - - if (!strstart(dirname, "fat:", NULL)) { - DLOG(fprintf(stderr, "%s:%u: dirname '%s' not \"fat:\"\n", __FUNCTION__, __LINE__, dirname)); - return -1; + + if ( !strstart( dirname, "fat:", NULL ) ) { + DLOG( fprintf( stderr, "%s:%u: dirname '%s' not \"fat:\"\n", __FUNCTION__, __LINE__, dirname ) ); + return -1; } - if (strstr(dirname, ":rw:")) { - if (enable_write(s)) { - return -1; - } - bs->read_only = 0; + if ( strstr( dirname, ":rw:" ) ) { + if ( enable_write( s ) ) { + return -1; + } + bs->read_only = 0; } - if (strstr(dirname, ":floppy:")) { - floppy = 1; - s->fat_type = 12; - s->first_sectors_number = 1; - s->sectors_per_cluster=2; - bs->cyls = 80; bs->heads = 2; bs->secs = 36; + if ( strstr( dirname, ":floppy:" ) ) { + floppy = 1; + s->fat_type = 12; + s->first_sectors_number = 1; + s->sectors_per_cluster = 2; + bs->cyls = 80; + bs->heads = 2; + bs->secs = 36; } - if (strstr(dirname, ":32:")) { - fprintf(stderr, "Big fat greek warning: FAT32 has not been tested. You are welcome to do so!\n"); - s->fat_type = 32; - } else if (strstr(dirname, ":16:")) { - s->fat_type = 16; - } else if (strstr(dirname, ":12:")) { - s->fat_type = 12; - s->sector_count=2880; + if ( strstr( dirname, ":32:" ) ) { + fprintf( stderr, "Big fat greek warning: FAT32 has not been tested. You are welcome to do so!\n" ); + s->fat_type = 32; + } else if ( strstr( dirname, ":16:" ) ) { + s->fat_type = 16; + } else if ( strstr( dirname, ":12:" ) ) { + s->fat_type = 12; + s->sector_count = 2880; } - i = strrchr(dirname, ':') - dirname; - assert(i >= 3); - if (dirname[i-2] == ':' && isalpha(dirname[i-1])) - /* workaround for DOS drive names */ - dirname += i-1; + i = strrchr( dirname, ':' ) - dirname; + assert( i >= 3 ); + if ( dirname[ i - 2 ] == ':' && isalpha( dirname[ i - 1 ] ) ) + /* workaround for DOS drive names */ + dirname += i - 1; else - dirname += i+1; + dirname += i + 1; - bs->total_sectors=bs->cyls*bs->heads*bs->secs; - if (s->sector_count > bs->total_sectors) - s->sector_count = bs->total_sectors; + bs->total_sectors = bs->cyls * bs->heads * bs->secs; + if ( s->sector_count > bs->total_sectors ) + s->sector_count = bs->total_sectors; - if (init_directories(s, dirname)) { - DLOG(fprintf(stderr, "%s:%u: init_directories failed\n", __FUNCTION__, __LINE__)); - return -1; + if ( init_directories( s, dirname ) ) { + DLOG( fprintf( stderr, "%s:%u: init_directories failed\n", __FUNCTION__, __LINE__ ) ); + return -1; } - if(s->first_sectors_number==0x40) - init_mbr(s); + if ( s->first_sectors_number == 0x40 ) + init_mbr( s ); /* for some reason or other, MS-DOS does not like to know about CHS... */ - if (floppy) - bs->heads = bs->cyls = bs->secs = 0; + if ( floppy ) + bs->heads = bs->cyls = bs->secs = 0; // assert(is_consistent(s)); - DLOG(fprintf(stderr, "%s:%u: return 0\n", __FUNCTION__, __LINE__)); + DLOG( fprintf( stderr, "%s:%u: return 0\n", __FUNCTION__, __LINE__ ) ); return 0; } -static inline void vvfat_close_current_file(BDRVVVFATState *s) +static inline void vvfat_close_current_file( BDRVVVFATState* s ) { - if(s->current_mapping) { - s->current_mapping = NULL; - if (s->current_fd) { - close(s->current_fd); - s->current_fd = 0; - } + if ( s->current_mapping ) { + s->current_mapping = NULL; + if ( s->current_fd ) { + close( s->current_fd ); + s->current_fd = 0; + } } s->current_cluster = -1; } @@ -1163,43 +1159,40 @@ static inline void vvfat_close_current_file(BDRVVVFATState *s) /* mappings between index1 and index2-1 are supposed to be ordered * return value is the index of the last mapping for which end>cluster_num */ -static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2) +static inline int find_mapping_for_cluster_aux( BDRVVVFATState* s, int cluster_num, int index1, int index2 ) { - int index3=index1+1; - while(1) { - mapping_t* mapping; - index3=(index1+index2)/2; - mapping=array_get(&(s->mapping),index3); - assert(mapping->begin < mapping->end); - if(mapping->begin>=cluster_num) { - assert(index2!=index3 || index2==0); - if(index2==index3) - return index1; - index2=index3; - } else { - if(index1==index3) - return mapping->end<=cluster_num ? index2 : index1; - index1=index3; - } - assert(index1<=index2); - DLOG(mapping=array_get(&(s->mapping),index1); - assert(mapping->begin<=cluster_num); - assert(index2 >= s->mapping.next || - ((mapping = array_get(&(s->mapping),index2)) && - mapping->end>cluster_num))); + int index3 = index1 + 1; + while ( 1 ) { + mapping_t* mapping; + index3 = ( index1 + index2 ) / 2; + mapping = array_get( &( s->mapping ), index3 ); + assert( mapping->begin < mapping->end ); + if ( mapping->begin >= cluster_num ) { + assert( index2 != index3 || index2 == 0 ); + if ( index2 == index3 ) + return index1; + index2 = index3; + } else { + if ( index1 == index3 ) + return mapping->end <= cluster_num ? index2 : index1; + index1 = index3; + } + assert( index1 <= index2 ); + DLOG( mapping = array_get( &( s->mapping ), index1 ); assert( mapping->begin <= cluster_num ); + assert( index2 >= s->mapping.next || ( ( mapping = array_get( &( s->mapping ), index2 ) ) && mapping->end > cluster_num ) ) ); } } -static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num) +static inline mapping_t* find_mapping_for_cluster( BDRVVVFATState* s, int cluster_num ) { - int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next); + int index = find_mapping_for_cluster_aux( s, cluster_num, 0, s->mapping.next ); mapping_t* mapping; - if(index>=s->mapping.next) - return 0; - mapping=array_get(&(s->mapping),index); - if(mapping->begin>cluster_num) - return 0; - assert(mapping->begin<=cluster_num && mapping->end>cluster_num); + if ( index >= s->mapping.next ) + return 0; + mapping = array_get( &( s->mapping ), index ); + if ( mapping->begin > cluster_num ) + return 0; + assert( mapping->begin <= cluster_num && mapping->end > cluster_num ); return mapping; } @@ -1207,207 +1200,205 @@ static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_ * This function simply compares path == mapping->path. Since the mappings * are sorted by cluster, this is expensive: O(n). */ -static inline mapping_t* find_mapping_for_path(BDRVVVFATState* s, - const char* path) +static inline mapping_t* find_mapping_for_path( BDRVVVFATState* s, const char* path ) { int i; - for (i = 0; i < s->mapping.next; i++) { - mapping_t* mapping = array_get(&(s->mapping), i); - if (mapping->first_mapping_index < 0 && - !strcmp(path, mapping->path)) - return mapping; + for ( i = 0; i < s->mapping.next; i++ ) { + mapping_t* mapping = array_get( &( s->mapping ), i ); + if ( mapping->first_mapping_index < 0 && !strcmp( path, mapping->path ) ) + return mapping; } return NULL; } -static int open_file(BDRVVVFATState* s,mapping_t* mapping) +static int open_file( BDRVVVFATState* s, mapping_t* mapping ) { - if(!mapping) - return -1; - if(!s->current_mapping || - strcmp(s->current_mapping->path,mapping->path)) { - /* open file */ - int fd = open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE); - if(fd<0) - return -1; - vvfat_close_current_file(s); - s->current_fd = fd; - s->current_mapping = mapping; + if ( !mapping ) + return -1; + if ( !s->current_mapping || strcmp( s->current_mapping->path, mapping->path ) ) { + /* open file */ + int fd = open( mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE ); + if ( fd < 0 ) + return -1; + vvfat_close_current_file( s ); + s->current_fd = fd; + s->current_mapping = mapping; } return 0; } -static inline int read_cluster(BDRVVVFATState *s,int cluster_num) +static inline int read_cluster( BDRVVVFATState* s, int cluster_num ) { - if(s->current_cluster != cluster_num) { - int result=0; - off_t offset; - assert(!s->current_mapping || s->current_fd || (s->current_mapping->mode & MODE_DIRECTORY)); - if(!s->current_mapping - || s->current_mapping->begin>cluster_num - || s->current_mapping->end<=cluster_num) { - /* binary search of mappings for file */ - mapping_t* mapping=find_mapping_for_cluster(s,cluster_num); + if ( s->current_cluster != cluster_num ) { + int result = 0; + off_t offset; + assert( !s->current_mapping || s->current_fd || ( s->current_mapping->mode & MODE_DIRECTORY ) ); + if ( !s->current_mapping || s->current_mapping->begin > cluster_num || s->current_mapping->end <= cluster_num ) { + /* binary search of mappings for file */ + mapping_t* mapping = find_mapping_for_cluster( s, cluster_num ); - assert(!mapping || (cluster_num>=mapping->begin && cluster_numend)); + assert( !mapping || ( cluster_num >= mapping->begin && cluster_num < mapping->end ) ); - if (mapping && mapping->mode & MODE_DIRECTORY) { - vvfat_close_current_file(s); - s->current_mapping = mapping; + if ( mapping && mapping->mode & MODE_DIRECTORY ) { + vvfat_close_current_file( s ); + s->current_mapping = mapping; read_cluster_directory: - offset = s->cluster_size*(cluster_num-s->current_mapping->begin); - s->cluster = s->directory.pointer+offset - + 0x20*s->current_mapping->info.dir.first_dir_index; - assert(((s->cluster-(unsigned char*)s->directory.pointer)%s->cluster_size)==0); - assert(s->cluster+s->cluster_size <= s->directory.pointer+s->directory.next*s->directory.item_size); - s->current_cluster = cluster_num; - return 0; - } + offset = s->cluster_size * ( cluster_num - s->current_mapping->begin ); + s->cluster = s->directory.pointer + offset + 0x20 * s->current_mapping->info.dir.first_dir_index; + assert( ( ( s->cluster - ( unsigned char* )s->directory.pointer ) % s->cluster_size ) == 0 ); + assert( s->cluster + s->cluster_size <= s->directory.pointer + s->directory.next * s->directory.item_size ); + s->current_cluster = cluster_num; + return 0; + } - if(open_file(s,mapping)) - return -2; - } else if (s->current_mapping->mode & MODE_DIRECTORY) - goto read_cluster_directory; + if ( open_file( s, mapping ) ) + return -2; + } else if ( s->current_mapping->mode & MODE_DIRECTORY ) + goto read_cluster_directory; - assert(s->current_fd); + assert( s->current_fd ); - offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset; - if(lseek(s->current_fd, offset, SEEK_SET)!=offset) - return -3; - s->cluster=s->cluster_buffer; - result=read(s->current_fd,s->cluster,s->cluster_size); - if(result<0) { - s->current_cluster = -1; - return -1; - } - s->current_cluster = cluster_num; + offset = s->cluster_size * ( cluster_num - s->current_mapping->begin ) + s->current_mapping->info.file.offset; + if ( lseek( s->current_fd, offset, SEEK_SET ) != offset ) + return -3; + s->cluster = s->cluster_buffer; + result = read( s->current_fd, s->cluster, s->cluster_size ); + if ( result < 0 ) { + s->current_cluster = -1; + return -1; + } + s->current_cluster = cluster_num; } return 0; } #ifdef DEBUG -static void hexdump(const void* address, uint32_t len) +static void hexdump( const void* address, uint32_t len ) { const unsigned char* p = address; int i, j; - for (i = 0; i < len; i += 16) { - for (j = 0; j < 16 && i + j < len; j++) - fprintf(stderr, "%02x ", p[i + j]); - for (; j < 16; j++) - fprintf(stderr, " "); - fprintf(stderr, " "); - for (j = 0; j < 16 && i + j < len; j++) - fprintf(stderr, "%c", (p[i + j] < ' ' || p[i + j] > 0x7f) ? '.' : p[i + j]); - fprintf(stderr, "\n"); + for ( i = 0; i < len; i += 16 ) { + for ( j = 0; j < 16 && i + j < len; j++ ) + fprintf( stderr, "%02x ", p[ i + j ] ); + for ( ; j < 16; j++ ) + fprintf( stderr, " " ); + fprintf( stderr, " " ); + for ( j = 0; j < 16 && i + j < len; j++ ) + fprintf( stderr, "%c", ( p[ i + j ] < ' ' || p[ i + j ] > 0x7f ) ? '.' : p[ i + j ] ); + fprintf( stderr, "\n" ); } } -static void print_direntry(const direntry_t* direntry) +static void print_direntry( const direntry_t* direntry ) { int j = 0; - char buffer[1024]; + char buffer[ 1024 ]; - fprintf(stderr, "direntry 0x%p: ", direntry); - if(!direntry) - return; - if(is_long_name(direntry)) { - unsigned char* c=(unsigned char*)direntry; - int i; - for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2) -#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = '°'; j++;} - ADD_CHAR(c[i]); - for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2) - ADD_CHAR(c[i]); - for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2) - ADD_CHAR(c[i]); - buffer[j] = 0; - fprintf(stderr, "%s\n", buffer); + fprintf( stderr, "direntry 0x%p: ", direntry ); + if ( !direntry ) + return; + if ( is_long_name( direntry ) ) { + unsigned char* c = ( unsigned char* )direntry; + int i; + for ( i = 1; i < 11 && c[ i ] && c[ i ] != 0xff; i += 2 ) +# define ADD_CHAR( c ) \ + { \ + buffer[ j ] = ( c ); \ + if ( buffer[ j ] < ' ' ) \ + buffer[ j ] = '°'; \ + j++; \ + } + ADD_CHAR( c[ i ] ); + for ( i = 14; i < 26 && c[ i ] && c[ i ] != 0xff; i += 2 ) + ADD_CHAR( c[ i ] ); + for ( i = 28; i < 32 && c[ i ] && c[ i ] != 0xff; i += 2 ) + ADD_CHAR( c[ i ] ); + buffer[ j ] = 0; + fprintf( stderr, "%s\n", buffer ); } else { - int i; - for(i=0;i<11;i++) - ADD_CHAR(direntry->name[i]); - buffer[j] = 0; - fprintf(stderr,"%s attributes=0x%02x begin=%d size=%d\n", - buffer, - direntry->attributes, - begin_of_direntry(direntry),le32_to_cpu(direntry->size)); + int i; + for ( i = 0; i < 11; i++ ) + ADD_CHAR( direntry->name[ i ] ); + buffer[ j ] = 0; + fprintf( stderr, "%s attributes=0x%02x begin=%d size=%d\n", buffer, direntry->attributes, begin_of_direntry( direntry ), + le32_to_cpu( direntry->size ) ); } { - unsigned char *p = (void *) direntry; - int i; + unsigned char* p = ( void* )direntry; + int i; - fprintf(stderr, "%p:\t", p); - for (i = 0; i < 16; i++) - fprintf(stderr, " %02x", *p++); - fprintf(stderr, "\n%p:\t", p); - for ( ; i < 32; i++) - fprintf(stderr, " %02x", *p++); - fprintf(stderr, "\n"); + fprintf( stderr, "%p:\t", p ); + for ( i = 0; i < 16; i++ ) + fprintf( stderr, " %02x", *p++ ); + fprintf( stderr, "\n%p:\t", p ); + for ( ; i < 32; i++ ) + fprintf( stderr, " %02x", *p++ ); + fprintf( stderr, "\n" ); } } -static void print_mapping(const mapping_t* mapping) +static void print_mapping( const mapping_t* mapping ) { - fprintf(stderr, "mapping (0x%p): begin, end = %d, %d, dir_index = %d, first_mapping_index = %d, name = %s, mode = 0x%x, " , mapping, mapping->begin, mapping->end, mapping->dir_index, mapping->first_mapping_index, mapping->path, mapping->mode); - if (mapping->mode & MODE_DIRECTORY) - fprintf(stderr, "parent_mapping_index = %d, first_dir_index = %d\n", mapping->info.dir.parent_mapping_index, mapping->info.dir.first_dir_index); + fprintf( stderr, "mapping (0x%p): begin, end = %d, %d, dir_index = %d, first_mapping_index = %d, name = %s, mode = 0x%x, ", mapping, + mapping->begin, mapping->end, mapping->dir_index, mapping->first_mapping_index, mapping->path, mapping->mode ); + if ( mapping->mode & MODE_DIRECTORY ) + fprintf( stderr, "parent_mapping_index = %d, first_dir_index = %d\n", mapping->info.dir.parent_mapping_index, + mapping->info.dir.first_dir_index ); else - fprintf(stderr, "offset = %d\n", mapping->info.file.offset); + fprintf( stderr, "offset = %d\n", mapping->info.file.offset ); } #endif -static int vvfat_read(BlockDriverState *bs, int64_t sector_num, - uint8_t *buf, int nb_sectors) +static int vvfat_read( BlockDriverState* bs, int64_t sector_num, uint8_t* buf, int nb_sectors ) { - BDRVVVFATState *s = bs->opaque; + BDRVVVFATState* s = bs->opaque; #ifdef DEBUG_SECTORS int32_t orig_sector = sector_num; #endif int i; - for(i=0;i= s->sector_count) - return -1; - if (s->qcow) { - int n; - if (s->qcow->drv->bdrv_is_allocated(s->qcow, - sector_num, nb_sectors-i, &n)) { -DLOG(fprintf(stderr, "sectors %d+%d allocated\n", (int)sector_num, n)); - if (s->qcow->drv->bdrv_read(s->qcow, sector_num, buf+i*0x200, n)) - return -1; - i += n - 1; - sector_num += n - 1; - continue; - } -// DLOG(fprintf(stderr, "sector %d not allocated\n", (int)sector_num)); - } - if(sector_numfaked_sectors) { - if(sector_numfirst_sectors_number) - memcpy(buf+i*0x200,&(s->first_sectors[sector_num*0x200]),0x200); - else if(sector_num-s->first_sectors_numbersectors_per_fat) - memcpy(buf+i*0x200,&(s->fat.pointer[(sector_num-s->first_sectors_number)*0x200]),0x200); - else if(sector_num-s->first_sectors_number-s->sectors_per_fatsectors_per_fat) - memcpy(buf+i*0x200,&(s->fat.pointer[(sector_num-s->first_sectors_number-s->sectors_per_fat)*0x200]),0x200); - } else { - uint32_t sector=sector_num-s->faked_sectors, - sector_offset_in_cluster=(sector%s->sectors_per_cluster), - cluster_num=sector/s->sectors_per_cluster; - if(read_cluster(s, cluster_num) != 0) { - /* LATER TODO: strict: return -1; */ - memset(buf+i*0x200,0,0x200); - continue; - } - memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200); - } + for ( i = 0; i < nb_sectors; i++, sector_num++ ) { + if ( sector_num >= s->sector_count ) + return -1; + if ( s->qcow ) { + int n; + if ( s->qcow->drv->bdrv_is_allocated( s->qcow, sector_num, nb_sectors - i, &n ) ) { + DLOG( fprintf( stderr, "sectors %d+%d allocated\n", ( int )sector_num, n ) ); + if ( s->qcow->drv->bdrv_read( s->qcow, sector_num, buf + i * 0x200, n ) ) + return -1; + i += n - 1; + sector_num += n - 1; + continue; + } + // DLOG(fprintf(stderr, "sector %d not allocated\n", (int)sector_num)); + } + if ( sector_num < s->faked_sectors ) { + if ( sector_num < s->first_sectors_number ) + memcpy( buf + i * 0x200, &( s->first_sectors[ sector_num * 0x200 ] ), 0x200 ); + else if ( sector_num - s->first_sectors_number < s->sectors_per_fat ) + memcpy( buf + i * 0x200, &( s->fat.pointer[ ( sector_num - s->first_sectors_number ) * 0x200 ] ), 0x200 ); + else if ( sector_num - s->first_sectors_number - s->sectors_per_fat < s->sectors_per_fat ) + memcpy( buf + i * 0x200, &( s->fat.pointer[ ( sector_num - s->first_sectors_number - s->sectors_per_fat ) * 0x200 ] ), + 0x200 ); + } else { + uint32_t sector = sector_num - s->faked_sectors, sector_offset_in_cluster = ( sector % s->sectors_per_cluster ), + cluster_num = sector / s->sectors_per_cluster; + if ( read_cluster( s, cluster_num ) != 0 ) { + /* LATER TODO: strict: return -1; */ + memset( buf + i * 0x200, 0, 0x200 ); + continue; + } + memcpy( buf + i * 0x200, s->cluster + sector_offset_in_cluster * 0x200, 0x200 ); + } } #ifdef DEBUG_SECTORS - for (i = 0; i < nb_sectors; i++) { - fprintf(stderr, "%s:%u: sector %u:\n", __FUNCTION__, __LINE__, orig_sector + i); - hexdump(buf + i * 0x200, 0x200); + for ( i = 0; i < nb_sectors; i++ ) { + fprintf( stderr, "%s:%u: sector %u:\n", __FUNCTION__, __LINE__, orig_sector + i ); + hexdump( buf + i * 0x200, 0x200 ); } #endif @@ -1439,206 +1430,208 @@ DLOG(fprintf(stderr, "sectors %d+%d allocated\n", (int)sector_num, n)); typedef struct commit_t { char* path; union { - struct { uint32_t cluster; } rename; - struct { int dir_index; uint32_t modified_offset; } writeout; - struct { uint32_t first_cluster; } new_file; - struct { uint32_t cluster; } mkdir; + struct { + uint32_t cluster; + } rename; + struct { + int dir_index; + uint32_t modified_offset; + } writeout; + struct { + uint32_t first_cluster; + } new_file; + struct { + uint32_t cluster; + } mkdir; } param; /* DELETEs and RMDIRs are handled differently: see handle_deletes() */ - enum { - ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR - } action; + enum { ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR } action; } commit_t; -static void clear_commits(BDRVVVFATState* s) +static void clear_commits( BDRVVVFATState* s ) { int i; -DLOG(fprintf(stderr, "clear_commits (%d commits)\n", s->commits.next)); - for (i = 0; i < s->commits.next; i++) { - commit_t* commit = array_get(&(s->commits), i); - assert(commit->path || commit->action == ACTION_WRITEOUT); - if (commit->action != ACTION_WRITEOUT) { - assert(commit->path); - free(commit->path); - } else - assert(commit->path == NULL); + DLOG( fprintf( stderr, "clear_commits (%d commits)\n", s->commits.next ) ); + for ( i = 0; i < s->commits.next; i++ ) { + commit_t* commit = array_get( &( s->commits ), i ); + assert( commit->path || commit->action == ACTION_WRITEOUT ); + if ( commit->action != ACTION_WRITEOUT ) { + assert( commit->path ); + free( commit->path ); + } else + assert( commit->path == NULL ); } s->commits.next = 0; } -static void schedule_rename(BDRVVVFATState* s, - uint32_t cluster, char* new_path) +static void schedule_rename( BDRVVVFATState* s, uint32_t cluster, char* new_path ) { - commit_t* commit = array_get_next(&(s->commits)); + commit_t* commit = array_get_next( &( s->commits ) ); commit->path = new_path; commit->param.rename.cluster = cluster; commit->action = ACTION_RENAME; } -static void schedule_writeout(BDRVVVFATState* s, - int dir_index, uint32_t modified_offset) +static void schedule_writeout( BDRVVVFATState* s, int dir_index, uint32_t modified_offset ) { - commit_t* commit = array_get_next(&(s->commits)); + commit_t* commit = array_get_next( &( s->commits ) ); commit->path = NULL; commit->param.writeout.dir_index = dir_index; commit->param.writeout.modified_offset = modified_offset; commit->action = ACTION_WRITEOUT; } -static void schedule_new_file(BDRVVVFATState* s, - char* path, uint32_t first_cluster) +static void schedule_new_file( BDRVVVFATState* s, char* path, uint32_t first_cluster ) { - commit_t* commit = array_get_next(&(s->commits)); + commit_t* commit = array_get_next( &( s->commits ) ); commit->path = path; commit->param.new_file.first_cluster = first_cluster; commit->action = ACTION_NEW_FILE; } -static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path) +static void schedule_mkdir( BDRVVVFATState* s, uint32_t cluster, char* path ) { - commit_t* commit = array_get_next(&(s->commits)); + commit_t* commit = array_get_next( &( s->commits ) ); commit->path = path; commit->param.mkdir.cluster = cluster; commit->action = ACTION_MKDIR; } typedef struct { - char name[1024]; + char name[ 1024 ]; int checksum, len; int sequence_number; } long_file_name; -static void lfn_init(long_file_name* lfn) +static void lfn_init( long_file_name* lfn ) { - lfn->sequence_number = lfn->len = 0; - lfn->checksum = 0x100; + lfn->sequence_number = lfn->len = 0; + lfn->checksum = 0x100; } /* return 0 if parsed successfully, > 0 if no long name, < 0 if error */ -static int parse_long_name(long_file_name* lfn, - const direntry_t* direntry) +static int parse_long_name( long_file_name* lfn, const direntry_t* direntry ) { int i, j, offset; - const unsigned char* pointer = (const unsigned char*)direntry; + const unsigned char* pointer = ( const unsigned char* )direntry; - if (!is_long_name(direntry)) - return 1; + if ( !is_long_name( direntry ) ) + return 1; - if (pointer[0] & 0x40) { - lfn->sequence_number = pointer[0] & 0x3f; - lfn->checksum = pointer[13]; - lfn->name[0] = 0; - } else if ((pointer[0] & 0x3f) != --lfn->sequence_number) - return -1; - else if (pointer[13] != lfn->checksum) - return -2; - else if (pointer[12] || pointer[26] || pointer[27]) - return -3; + if ( pointer[ 0 ] & 0x40 ) { + lfn->sequence_number = pointer[ 0 ] & 0x3f; + lfn->checksum = pointer[ 13 ]; + lfn->name[ 0 ] = 0; + } else if ( ( pointer[ 0 ] & 0x3f ) != --lfn->sequence_number ) + return -1; + else if ( pointer[ 13 ] != lfn->checksum ) + return -2; + else if ( pointer[ 12 ] || pointer[ 26 ] || pointer[ 27 ] ) + return -3; - offset = 13 * (lfn->sequence_number - 1); - for (i = 0, j = 1; i < 13; i++, j+=2) { - if (j == 11) - j = 14; - else if (j == 26) - j = 28; + offset = 13 * ( lfn->sequence_number - 1 ); + for ( i = 0, j = 1; i < 13; i++, j += 2 ) { + if ( j == 11 ) + j = 14; + else if ( j == 26 ) + j = 28; - if (pointer[j+1] == 0) - lfn->name[offset + i] = pointer[j]; - else if (pointer[j+1] != 0xff || (pointer[0] & 0x40) == 0) - return -4; - else - lfn->name[offset + i] = 0; + if ( pointer[ j + 1 ] == 0 ) + lfn->name[ offset + i ] = pointer[ j ]; + else if ( pointer[ j + 1 ] != 0xff || ( pointer[ 0 ] & 0x40 ) == 0 ) + return -4; + else + lfn->name[ offset + i ] = 0; } - if (pointer[0] & 0x40) - lfn->len = offset + strlen(lfn->name + offset); + if ( pointer[ 0 ] & 0x40 ) + lfn->len = offset + strlen( lfn->name + offset ); return 0; } /* returns 0 if successful, >0 if no short_name, and <0 on error */ -static int parse_short_name(BDRVVVFATState* s, - long_file_name* lfn, direntry_t* direntry) +static int parse_short_name( BDRVVVFATState* s, long_file_name* lfn, direntry_t* direntry ) { int i, j; - if (!is_short_name(direntry)) - return 1; + if ( !is_short_name( direntry ) ) + return 1; - for (j = 7; j >= 0 && direntry->name[j] == ' '; j--); - for (i = 0; i <= j; i++) { - if (direntry->name[i] <= ' ' || direntry->name[i] > 0x7f) - return -1; - else if (s->downcase_short_names) - lfn->name[i] = tolower(direntry->name[i]); - else - lfn->name[i] = direntry->name[i]; + for ( j = 7; j >= 0 && direntry->name[ j ] == ' '; j-- ) + ; + for ( i = 0; i <= j; i++ ) { + if ( direntry->name[ i ] <= ' ' || direntry->name[ i ] > 0x7f ) + return -1; + else if ( s->downcase_short_names ) + lfn->name[ i ] = tolower( direntry->name[ i ] ); + else + lfn->name[ i ] = direntry->name[ i ]; } - for (j = 2; j >= 0 && direntry->extension[j] == ' '; j--); - if (j >= 0) { - lfn->name[i++] = '.'; - lfn->name[i + j + 1] = '\0'; - for (;j >= 0; j--) { - if (direntry->extension[j] <= ' ' || direntry->extension[j] > 0x7f) - return -2; - else if (s->downcase_short_names) - lfn->name[i + j] = tolower(direntry->extension[j]); - else - lfn->name[i + j] = direntry->extension[j]; - } + for ( j = 2; j >= 0 && direntry->extension[ j ] == ' '; j-- ) + ; + if ( j >= 0 ) { + lfn->name[ i++ ] = '.'; + lfn->name[ i + j + 1 ] = '\0'; + for ( ; j >= 0; j-- ) { + if ( direntry->extension[ j ] <= ' ' || direntry->extension[ j ] > 0x7f ) + return -2; + else if ( s->downcase_short_names ) + lfn->name[ i + j ] = tolower( direntry->extension[ j ] ); + else + lfn->name[ i + j ] = direntry->extension[ j ]; + } } else - lfn->name[i + j + 1] = '\0'; + lfn->name[ i + j + 1 ] = '\0'; - lfn->len = strlen(lfn->name); + lfn->len = strlen( lfn->name ); return 0; } -static inline uint32_t modified_fat_get(BDRVVVFATState* s, - unsigned int cluster) +static inline uint32_t modified_fat_get( BDRVVVFATState* s, unsigned int cluster ) { - if (cluster < s->last_cluster_of_root_directory) { - if (cluster + 1 == s->last_cluster_of_root_directory) - return s->max_fat_value; - else - return cluster + 1; + if ( cluster < s->last_cluster_of_root_directory ) { + if ( cluster + 1 == s->last_cluster_of_root_directory ) + return s->max_fat_value; + else + return cluster + 1; } - if (s->fat_type==32) { - uint32_t* entry=((uint32_t*)s->fat2)+cluster; - return le32_to_cpu(*entry); - } else if (s->fat_type==16) { - uint16_t* entry=((uint16_t*)s->fat2)+cluster; - return le16_to_cpu(*entry); + if ( s->fat_type == 32 ) { + uint32_t* entry = ( ( uint32_t* )s->fat2 ) + cluster; + return le32_to_cpu( *entry ); + } else if ( s->fat_type == 16 ) { + uint16_t* entry = ( ( uint16_t* )s->fat2 ) + cluster; + return le16_to_cpu( *entry ); } else { - const uint8_t* x=s->fat2+cluster*3/2; - return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff; + const uint8_t* x = s->fat2 + cluster * 3 / 2; + return ( ( x[ 0 ] | ( x[ 1 ] << 8 ) ) >> ( cluster & 1 ? 4 : 0 ) ) & 0x0fff; } } -static inline int cluster_was_modified(BDRVVVFATState* s, uint32_t cluster_num) +static inline int cluster_was_modified( BDRVVVFATState* s, uint32_t cluster_num ) { int was_modified = 0; int i, dummy; - if (s->qcow == NULL) - return 0; + if ( s->qcow == NULL ) + return 0; - for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) - was_modified = s->qcow->drv->bdrv_is_allocated(s->qcow, - cluster2sector(s, cluster_num) + i, 1, &dummy); + for ( i = 0; !was_modified && i < s->sectors_per_cluster; i++ ) + was_modified = s->qcow->drv->bdrv_is_allocated( s->qcow, cluster2sector( s, cluster_num ) + i, 1, &dummy ); return was_modified; } -static const char* get_basename(const char* path) +static const char* get_basename( const char* path ) { - char* basename = strrchr(path, '/'); - if (basename == NULL) - return path; + char* basename = strrchr( path, '/' ); + if ( basename == NULL ) + return path; else - return basename + 1; /* strip '/' */ + return basename + 1; /* strip '/' */ } /* @@ -1648,9 +1641,7 @@ static const char* get_basename(const char* path) * If any cluster is allocated, but not part of a file or directory, this * driver refuses to commit. */ -typedef enum { - USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4 -} used_t; +typedef enum { USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4 } used_t; /* * get_cluster_count_for_direntry() not only determines how many clusters @@ -1662,8 +1653,7 @@ typedef enum { * Further, the files/directories handled by this function are * assumed to be *not* deleted (and *only* those). */ -static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s, - direntry_t* direntry, const char* path) +static uint32_t get_cluster_count_for_direntry( BDRVVVFATState* s, direntry_t* direntry, const char* path ) { /* * This is a little bit tricky: @@ -1686,262 +1676,250 @@ static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s, int was_modified = 0; int32_t ret = 0; - uint32_t cluster_num = begin_of_direntry(direntry); + uint32_t cluster_num = begin_of_direntry( direntry ); uint32_t offset = 0; int first_mapping_index = -1; mapping_t* mapping = NULL; const char* basename2 = NULL; - vvfat_close_current_file(s); + vvfat_close_current_file( s ); /* the root directory */ - if (cluster_num == 0) - return 0; + if ( cluster_num == 0 ) + return 0; /* write support */ - if (s->qcow) { - basename2 = get_basename(path); + if ( s->qcow ) { + basename2 = get_basename( path ); - mapping = find_mapping_for_cluster(s, cluster_num); + mapping = find_mapping_for_cluster( s, cluster_num ); - if (mapping) { - const char* basename; + if ( mapping ) { + const char* basename; - assert(mapping->mode & MODE_DELETED); - DLOG(fprintf(stderr, "%s:%u: clear delete: ", __FUNCTION__, __LINE__); print_mapping(mapping)); - mapping->mode &= ~MODE_DELETED; + assert( mapping->mode & MODE_DELETED ); + DLOG( fprintf( stderr, "%s:%u: clear delete: ", __FUNCTION__, __LINE__ ); print_mapping( mapping ) ); + mapping->mode &= ~MODE_DELETED; - basename = get_basename(mapping->path); + basename = get_basename( mapping->path ); - assert(mapping->mode & MODE_NORMAL); + assert( mapping->mode & MODE_NORMAL ); - /* rename */ - if (strcmp(basename, basename2)) - schedule_rename(s, cluster_num, strdup(path)); - } else if (is_file(direntry)) - /* new file */ - schedule_new_file(s, strdup(path), cluster_num); - else { - assert(0); - return 0; - } + /* rename */ + if ( strcmp( basename, basename2 ) ) + schedule_rename( s, cluster_num, strdup( path ) ); + } else if ( is_file( direntry ) ) + /* new file */ + schedule_new_file( s, strdup( path ), cluster_num ); + else { + assert( 0 ); + return 0; + } } - while(1) { - if (s->qcow) { - if (!copy_it && cluster_was_modified(s, cluster_num)) { - if (mapping == NULL || - mapping->begin > cluster_num || - mapping->end <= cluster_num) - mapping = find_mapping_for_cluster(s, cluster_num); + while ( 1 ) { + if ( s->qcow ) { + if ( !copy_it && cluster_was_modified( s, cluster_num ) ) { + if ( mapping == NULL || mapping->begin > cluster_num || mapping->end <= cluster_num ) + mapping = find_mapping_for_cluster( s, cluster_num ); + if ( mapping && ( mapping->mode & MODE_DIRECTORY ) == 0 ) { - if (mapping && - (mapping->mode & MODE_DIRECTORY) == 0) { + /* was modified in qcow */ + if ( offset != mapping->info.file.offset + s->cluster_size * ( cluster_num - mapping->begin ) ) { + /* offset of this cluster in file chain has changed */ + assert( 0 ); + copy_it = 1; + } else if ( offset == 0 ) { + const char* basename = get_basename( mapping->path ); - /* was modified in qcow */ - if (offset != mapping->info.file.offset + s->cluster_size - * (cluster_num - mapping->begin)) { - /* offset of this cluster in file chain has changed */ - assert(0); - copy_it = 1; - } else if (offset == 0) { - const char* basename = get_basename(mapping->path); + if ( strcmp( basename, basename2 ) ) + copy_it = 1; + first_mapping_index = array_index( &( s->mapping ), mapping ); + } - if (strcmp(basename, basename2)) - copy_it = 1; - first_mapping_index = array_index(&(s->mapping), mapping); - } + if ( mapping->first_mapping_index != first_mapping_index && mapping->info.file.offset > 0 ) { + assert( 0 ); + copy_it = 1; + } - if (mapping->first_mapping_index != first_mapping_index - && mapping->info.file.offset > 0) { - assert(0); - copy_it = 1; - } + /* need to write out? */ + if ( !was_modified && is_file( direntry ) ) { + was_modified = 1; + schedule_writeout( s, mapping->dir_index, offset ); + } + } + } - /* need to write out? */ - if (!was_modified && is_file(direntry)) { - was_modified = 1; - schedule_writeout(s, mapping->dir_index, offset); - } - } - } + if ( copy_it ) { + int i, dummy; + /* + * This is horribly inefficient, but that is okay, since + * it is rarely executed, if at all. + */ + int64_t offset = cluster2sector( s, cluster_num ); - if (copy_it) { - int i, dummy; - /* - * This is horribly inefficient, but that is okay, since - * it is rarely executed, if at all. - */ - int64_t offset = cluster2sector(s, cluster_num); + vvfat_close_current_file( s ); + for ( i = 0; i < s->sectors_per_cluster; i++ ) + if ( !s->qcow->drv->bdrv_is_allocated( s->qcow, offset + i, 1, &dummy ) ) { + if ( vvfat_read( s->bs, offset, s->cluster_buffer, 1 ) ) + return -1; + if ( s->qcow->drv->bdrv_write( s->qcow, offset, s->cluster_buffer, 1 ) ) + return -2; + } + } + } - vvfat_close_current_file(s); - for (i = 0; i < s->sectors_per_cluster; i++) - if (!s->qcow->drv->bdrv_is_allocated(s->qcow, - offset + i, 1, &dummy)) { - if (vvfat_read(s->bs, - offset, s->cluster_buffer, 1)) - return -1; - if (s->qcow->drv->bdrv_write(s->qcow, - offset, s->cluster_buffer, 1)) - return -2; - } - } - } + ret++; + if ( s->used_clusters[ cluster_num ] & USED_ANY ) + return 0; + s->used_clusters[ cluster_num ] = USED_FILE; - ret++; - if (s->used_clusters[cluster_num] & USED_ANY) - return 0; - s->used_clusters[cluster_num] = USED_FILE; + cluster_num = modified_fat_get( s, cluster_num ); - cluster_num = modified_fat_get(s, cluster_num); + if ( fat_eof( s, cluster_num ) ) + return ret; + else if ( cluster_num < 2 || cluster_num > s->max_fat_value - 16 ) + return -1; - if (fat_eof(s, cluster_num)) - return ret; - else if (cluster_num < 2 || cluster_num > s->max_fat_value - 16) - return -1; - - offset += s->cluster_size; + offset += s->cluster_size; } } /* - * This function looks at the modified data (qcow). + * This function looks at the modified data (qcow). * It returns 0 upon inconsistency or error, and the number of clusters * used by the directory, its subdirectories and their files. */ -static int check_directory_consistency(BDRVVVFATState *s, - int cluster_num, const char* path) +static int check_directory_consistency( BDRVVVFATState* s, int cluster_num, const char* path ) { int ret = 0; - unsigned char* cluster = malloc(s->cluster_size); - direntry_t* direntries = (direntry_t*)cluster; - mapping_t* mapping = find_mapping_for_cluster(s, cluster_num); + unsigned char* cluster = malloc( s->cluster_size ); + direntry_t* direntries = ( direntry_t* )cluster; + mapping_t* mapping = find_mapping_for_cluster( s, cluster_num ); long_file_name lfn; - int path_len = strlen(path); - char path2[PATH_MAX]; + int path_len = strlen( path ); + char path2[ PATH_MAX ]; - assert(path_len < PATH_MAX); /* len was tested before! */ - strcpy(path2, path); - path2[path_len] = '/'; - path2[path_len + 1] = '\0'; + assert( path_len < PATH_MAX ); /* len was tested before! */ + strcpy( path2, path ); + path2[ path_len ] = '/'; + path2[ path_len + 1 ] = '\0'; - if (mapping) { - const char* basename = get_basename(mapping->path); - const char* basename2 = get_basename(path); + if ( mapping ) { + const char* basename = get_basename( mapping->path ); + const char* basename2 = get_basename( path ); - assert(mapping->mode & MODE_DIRECTORY); + assert( mapping->mode & MODE_DIRECTORY ); - assert(mapping->mode & MODE_DELETED); - DLOG(fprintf(stderr, "%s:%u: clear delete: ", __FUNCTION__, __LINE__); print_mapping(mapping)); - mapping->mode &= ~MODE_DELETED; + assert( mapping->mode & MODE_DELETED ); + DLOG( fprintf( stderr, "%s:%u: clear delete: ", __FUNCTION__, __LINE__ ); print_mapping( mapping ) ); + mapping->mode &= ~MODE_DELETED; - if (strcmp(basename, basename2)) - schedule_rename(s, cluster_num, strdup(path)); + if ( strcmp( basename, basename2 ) ) + schedule_rename( s, cluster_num, strdup( path ) ); } else - /* new directory */ - schedule_mkdir(s, cluster_num, strdup(path)); - - lfn_init(&lfn); + /* new directory */ + schedule_mkdir( s, cluster_num, strdup( path ) ); + + lfn_init( &lfn ); do { - int i; - int subret = 0; + int i; + int subret = 0; - ret++; + ret++; - if (s->used_clusters[cluster_num] & USED_ANY) { - fprintf(stderr, "cluster %d used more than once\n", (int)cluster_num); - return 0; - } - s->used_clusters[cluster_num] = USED_DIRECTORY; + if ( s->used_clusters[ cluster_num ] & USED_ANY ) { + fprintf( stderr, "cluster %d used more than once\n", ( int )cluster_num ); + return 0; + } + s->used_clusters[ cluster_num ] = USED_DIRECTORY; -DLOG(fprintf(stderr, "read cluster %d (sector %d)\n", (int)cluster_num, (int)cluster2sector(s, cluster_num))); - subret = vvfat_read(s->bs, cluster2sector(s, cluster_num), cluster, - s->sectors_per_cluster); - if (subret) { - fprintf(stderr, "Error fetching direntries\n"); - fail: - free(cluster); - return 0; - } + DLOG( fprintf( stderr, "read cluster %d (sector %d)\n", ( int )cluster_num, ( int )cluster2sector( s, cluster_num ) ) ); + subret = vvfat_read( s->bs, cluster2sector( s, cluster_num ), cluster, s->sectors_per_cluster ); + if ( subret ) { + fprintf( stderr, "Error fetching direntries\n" ); +fail: + free( cluster ); + return 0; + } - for (i = 0; i < 0x10 * s->sectors_per_cluster; i++) { - int cluster_count; + for ( i = 0; i < 0x10 * s->sectors_per_cluster; i++ ) { + int cluster_count; -DLOG(if (!is_free(direntries + i)) { fprintf(stderr, "check direntry %d: \n", i); print_direntry(direntries + i); }) + DLOG( if ( !is_free( direntries + i ) ) { + fprintf( stderr, "check direntry %d: \n", i ); + print_direntry( direntries + i ); + } ) - if (is_volume_label(direntries + i) || is_dot(direntries + i) || is_free(direntries + i)) - continue; + if ( is_volume_label( direntries + i ) || is_dot( direntries + i ) || is_free( direntries + i ) ) + continue; - subret = parse_long_name(&lfn, direntries + i); - if (subret < 0) { - fprintf(stderr, "Error in long name\n"); - goto fail; - } - if (subret == 0 || is_free(direntries + i)) - continue; + subret = parse_long_name( &lfn, direntries + i ); + if ( subret < 0 ) { + fprintf( stderr, "Error in long name\n" ); + goto fail; + } + if ( subret == 0 || is_free( direntries + i ) ) + continue; - if (fat_chksum(direntries+i) != lfn.checksum) { - subret = parse_short_name(s, &lfn, direntries + i); - if (subret < 0) { - fprintf(stderr, "Error in short name (%d)\n", subret); - goto fail; - } - if (subret > 0 || !strcmp(lfn.name, ".") - || !strcmp(lfn.name, "..")) - continue; - } - lfn.checksum = 0x100; /* cannot use long name twice */ + if ( fat_chksum( direntries + i ) != lfn.checksum ) { + subret = parse_short_name( s, &lfn, direntries + i ); + if ( subret < 0 ) { + fprintf( stderr, "Error in short name (%d)\n", subret ); + goto fail; + } + if ( subret > 0 || !strcmp( lfn.name, "." ) || !strcmp( lfn.name, ".." ) ) + continue; + } + lfn.checksum = 0x100; /* cannot use long name twice */ - if (path_len + 1 + lfn.len >= PATH_MAX) { - fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name); - goto fail; - } - strcpy(path2 + path_len + 1, lfn.name); + if ( path_len + 1 + lfn.len >= PATH_MAX ) { + fprintf( stderr, "Name too long: %s/%s\n", path, lfn.name ); + goto fail; + } + strcpy( path2 + path_len + 1, lfn.name ); - if (is_directory(direntries + i)) { - if (begin_of_direntry(direntries + i) == 0) { - DLOG(fprintf(stderr, "invalid begin for directory: %s\n", path2); print_direntry(direntries + i)); - goto fail; - } - cluster_count = check_directory_consistency(s, - begin_of_direntry(direntries + i), path2); - if (cluster_count == 0) { - DLOG(fprintf(stderr, "problem in directory %s:\n", path2); print_direntry(direntries + i)); - goto fail; - } - } else if (is_file(direntries + i)) { - /* check file size with FAT */ - cluster_count = get_cluster_count_for_direntry(s, direntries + i, path2); - if (cluster_count != - (le32_to_cpu(direntries[i].size) + s->cluster_size - - 1) / s->cluster_size) { - DLOG(fprintf(stderr, "Cluster count mismatch\n")); - goto fail; - } - } else - assert(0); /* cluster_count = 0; */ + if ( is_directory( direntries + i ) ) { + if ( begin_of_direntry( direntries + i ) == 0 ) { + DLOG( fprintf( stderr, "invalid begin for directory: %s\n", path2 ); print_direntry( direntries + i ) ); + goto fail; + } + cluster_count = check_directory_consistency( s, begin_of_direntry( direntries + i ), path2 ); + if ( cluster_count == 0 ) { + DLOG( fprintf( stderr, "problem in directory %s:\n", path2 ); print_direntry( direntries + i ) ); + goto fail; + } + } else if ( is_file( direntries + i ) ) { + /* check file size with FAT */ + cluster_count = get_cluster_count_for_direntry( s, direntries + i, path2 ); + if ( cluster_count != ( le32_to_cpu( direntries[ i ].size ) + s->cluster_size - 1 ) / s->cluster_size ) { + DLOG( fprintf( stderr, "Cluster count mismatch\n" ) ); + goto fail; + } + } else + assert( 0 ); /* cluster_count = 0; */ - ret += cluster_count; - } + ret += cluster_count; + } - cluster_num = modified_fat_get(s, cluster_num); - } while(!fat_eof(s, cluster_num)); + cluster_num = modified_fat_get( s, cluster_num ); + } while ( !fat_eof( s, cluster_num ) ); - free(cluster); + free( cluster ); return ret; } /* returns 1 on success */ -static int is_consistent(BDRVVVFATState* s) +static int is_consistent( BDRVVVFATState* s ) { int i, check; int used_clusters_count = 0; -DLOG(checkpoint(__FUNCTION__)); + DLOG( checkpoint( __FUNCTION__ ) ); - DLOG(fprintf(stderr, "%s:%u:\n", __FUNCTION__, __LINE__)); + DLOG( fprintf( stderr, "%s:%u:\n", __FUNCTION__, __LINE__ ) ); /* * - get modified FAT @@ -1956,89 +1934,86 @@ DLOG(checkpoint(__FUNCTION__)); * FAT * - if all is fine, return number of used clusters */ - if (s->fat2 == NULL) { - int size = 0x200 * s->sectors_per_fat; - s->fat2 = malloc(size); - memcpy(s->fat2, s->fat.pointer, size); + if ( s->fat2 == NULL ) { + int size = 0x200 * s->sectors_per_fat; + s->fat2 = malloc( size ); + memcpy( s->fat2, s->fat.pointer, size ); } - check = vvfat_read(s->bs, s->first_sectors_number, - s->fat2, s->sectors_per_fat); - if (check) { - fprintf(stderr, "Could not copy fat\n"); - DLOG(fprintf(stderr, "%s:%u: copy fat failed\n", __FUNCTION__, __LINE__)); - return 0; + check = vvfat_read( s->bs, s->first_sectors_number, s->fat2, s->sectors_per_fat ); + if ( check ) { + fprintf( stderr, "Could not copy fat\n" ); + DLOG( fprintf( stderr, "%s:%u: copy fat failed\n", __FUNCTION__, __LINE__ ) ); + return 0; } - assert (s->used_clusters); - for (i = 0; i < sector2cluster(s, s->sector_count); i++) - s->used_clusters[i] &= ~USED_ANY; + assert( s->used_clusters ); + for ( i = 0; i < sector2cluster( s, s->sector_count ); i++ ) + s->used_clusters[ i ] &= ~USED_ANY; - clear_commits(s); + clear_commits( s ); /* mark every mapped file/directory as deleted. * (check_directory_consistency() will unmark those still present). */ - if (s->qcow) { - for (i = 0; i < s->mapping.next; i++) { - mapping_t* mapping = array_get(&(s->mapping), i); - if (mapping->first_mapping_index < 0) { - DLOG(fprintf(stderr, "%s:%u: mark delete: ", __FUNCTION__, __LINE__); print_mapping(mapping)); - mapping->mode |= MODE_DELETED; - } - } + if ( s->qcow ) { + for ( i = 0; i < s->mapping.next; i++ ) { + mapping_t* mapping = array_get( &( s->mapping ), i ); + if ( mapping->first_mapping_index < 0 ) { + DLOG( fprintf( stderr, "%s:%u: mark delete: ", __FUNCTION__, __LINE__ ); print_mapping( mapping ) ); + mapping->mode |= MODE_DELETED; + } + } } - used_clusters_count = check_directory_consistency(s, 0, s->path); - if (used_clusters_count <= 0) { - DLOG(fprintf(stderr, "problem in directory\n")); - return 0; + used_clusters_count = check_directory_consistency( s, 0, s->path ); + if ( used_clusters_count <= 0 ) { + DLOG( fprintf( stderr, "problem in directory\n" ) ); + return 0; } check = s->last_cluster_of_root_directory; - for (i = check; i < sector2cluster(s, s->sector_count); i++) { - if (modified_fat_get(s, i)) { - if(!s->used_clusters[i]) { - DLOG(fprintf(stderr, "FAT was modified (%d), but cluster is not used?\n", i)); - return 0; - } - check++; - } + for ( i = check; i < sector2cluster( s, s->sector_count ); i++ ) { + if ( modified_fat_get( s, i ) ) { + if ( !s->used_clusters[ i ] ) { + DLOG( fprintf( stderr, "FAT was modified (%d), but cluster is not used?\n", i ) ); + return 0; + } + check++; + } - if (s->used_clusters[i] == USED_ALLOCATED) { - /* allocated, but not used... */ - DLOG(fprintf(stderr, "unused, modified cluster: %d\n", i)); - return 0; - } + if ( s->used_clusters[ i ] == USED_ALLOCATED ) { + /* allocated, but not used... */ + DLOG( fprintf( stderr, "unused, modified cluster: %d\n", i ) ); + return 0; + } } - if (check != used_clusters_count) { - DLOG(fprintf(stderr, "%s:%u: check: %u, used %u\n", __FUNCTION__, __LINE__, check, used_clusters_count)); - return 0; + if ( check != used_clusters_count ) { + DLOG( fprintf( stderr, "%s:%u: check: %u, used %u\n", __FUNCTION__, __LINE__, check, used_clusters_count ) ); + return 0; } - DLOG(fprintf(stderr, "%s:%u: return used %u\n", __FUNCTION__, __LINE__, used_clusters_count)); + DLOG( fprintf( stderr, "%s:%u: return used %u\n", __FUNCTION__, __LINE__, used_clusters_count ) ); return used_clusters_count; } -static inline void adjust_mapping_indices(BDRVVVFATState* s, - int offset, int adjust) +static inline void adjust_mapping_indices( BDRVVVFATState* s, int offset, int adjust ) { int i; - for (i = 0; i < s->mapping.next; i++) { - mapping_t* mapping = array_get(&(s->mapping), i); + for ( i = 0; i < s->mapping.next; i++ ) { + mapping_t* mapping = array_get( &( s->mapping ), i ); -#define ADJUST_MAPPING_INDEX(name) \ - if (mapping->name >= offset) \ - mapping->name += adjust +#define ADJUST_MAPPING_INDEX( name ) \ + if ( mapping->name >= offset ) \ + mapping->name += adjust - ADJUST_MAPPING_INDEX(first_mapping_index); - if (mapping->mode & MODE_DIRECTORY) - ADJUST_MAPPING_INDEX(info.dir.parent_mapping_index); + ADJUST_MAPPING_INDEX( first_mapping_index ); + if ( mapping->mode & MODE_DIRECTORY ) + ADJUST_MAPPING_INDEX( info.dir.parent_mapping_index ); } } /* insert or update mapping */ -static mapping_t* insert_mapping(BDRVVVFATState* s, - uint32_t begin, uint32_t end) +static mapping_t* insert_mapping( BDRVVVFATState* s, uint32_t begin, uint32_t end ) { /* * - find mapping where mapping->begin >= begin, @@ -2047,96 +2022,89 @@ static mapping_t* insert_mapping(BDRVVVFATState* s, * - else: adjust * - replace name */ - int index = find_mapping_for_cluster_aux(s, begin, 0, s->mapping.next); + int index = find_mapping_for_cluster_aux( s, begin, 0, s->mapping.next ); mapping_t* mapping = NULL; - mapping_t* first_mapping = array_get(&(s->mapping), 0); + mapping_t* first_mapping = array_get( &( s->mapping ), 0 ); - if (index < s->mapping.next && (mapping = array_get(&(s->mapping), index)) - && mapping->begin < begin) { - mapping->end = begin; - index++; - mapping = array_get(&(s->mapping), index); + if ( index < s->mapping.next && ( mapping = array_get( &( s->mapping ), index ) ) && mapping->begin < begin ) { + mapping->end = begin; + index++; + mapping = array_get( &( s->mapping ), index ); } - if (index >= s->mapping.next || mapping->begin > begin) { - mapping = array_insert(&(s->mapping), index, 1); - mapping->path = NULL; - adjust_mapping_indices(s, index, +1); + if ( index >= s->mapping.next || mapping->begin > begin ) { + mapping = array_insert( &( s->mapping ), index, 1 ); + mapping->path = NULL; + adjust_mapping_indices( s, index, +1 ); } mapping->begin = begin; mapping->end = end; -DLOG(mapping_t* next_mapping; -assert(index + 1 >= s->mapping.next || -((next_mapping = array_get(&(s->mapping), index + 1)) && - next_mapping->begin >= end))); + DLOG( mapping_t * next_mapping; assert( index + 1 >= s->mapping.next || ( ( next_mapping = array_get( &( s->mapping ), index + 1 ) ) && + next_mapping->begin >= end ) ) ); - DLOG(fprintf(stderr, "insert mapping %u:\n", index); print_mapping(mapping)); + DLOG( fprintf( stderr, "insert mapping %u:\n", index ); print_mapping( mapping ) ); - if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer) - s->current_mapping = array_get(&(s->mapping), - s->current_mapping - first_mapping); + if ( s->current_mapping && first_mapping != ( mapping_t* )s->mapping.pointer ) + s->current_mapping = array_get( &( s->mapping ), s->current_mapping - first_mapping ); return mapping; } -static int remove_mapping(BDRVVVFATState* s, int mapping_index) +static int remove_mapping( BDRVVVFATState* s, int mapping_index ) { - mapping_t* mapping = array_get(&(s->mapping), mapping_index); - mapping_t* first_mapping = array_get(&(s->mapping), 0); + mapping_t* mapping = array_get( &( s->mapping ), mapping_index ); + mapping_t* first_mapping = array_get( &( s->mapping ), 0 ); - DLOG(fprintf(stderr, "remove mapping %u:\n", mapping_index); print_mapping(mapping)); + DLOG( fprintf( stderr, "remove mapping %u:\n", mapping_index ); print_mapping( mapping ) ); /* free mapping */ - if (mapping->first_mapping_index < 0) - free(mapping->path); + if ( mapping->first_mapping_index < 0 ) + free( mapping->path ); /* remove from s->mapping */ - array_remove(&(s->mapping), mapping_index); + array_remove( &( s->mapping ), mapping_index ); /* adjust all references to mappings */ - adjust_mapping_indices(s, mapping_index, -1); + adjust_mapping_indices( s, mapping_index, -1 ); - if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer) - s->current_mapping = array_get(&(s->mapping), - s->current_mapping - first_mapping); + if ( s->current_mapping && first_mapping != ( mapping_t* )s->mapping.pointer ) + s->current_mapping = array_get( &( s->mapping ), s->current_mapping - first_mapping ); return 0; } -static void adjust_dirindices(BDRVVVFATState* s, int offset, int adjust) +static void adjust_dirindices( BDRVVVFATState* s, int offset, int adjust ) { int i; - for (i = 0; i < s->mapping.next; i++) { - mapping_t* mapping = array_get(&(s->mapping), i); - if (mapping->dir_index >= offset) - mapping->dir_index += adjust; - if ((mapping->mode & MODE_DIRECTORY) && - mapping->info.dir.first_dir_index >= offset) - mapping->info.dir.first_dir_index += adjust; + for ( i = 0; i < s->mapping.next; i++ ) { + mapping_t* mapping = array_get( &( s->mapping ), i ); + if ( mapping->dir_index >= offset ) + mapping->dir_index += adjust; + if ( ( mapping->mode & MODE_DIRECTORY ) && mapping->info.dir.first_dir_index >= offset ) + mapping->info.dir.first_dir_index += adjust; } } -static direntry_t* insert_direntries(BDRVVVFATState* s, - int dir_index, int count) +static direntry_t* insert_direntries( BDRVVVFATState* s, int dir_index, int count ) { /* * make room in s->directory, * adjust_dirindices */ - direntry_t* result = array_insert(&(s->directory), dir_index, count); - if (result == NULL) - return NULL; - adjust_dirindices(s, dir_index, count); + direntry_t* result = array_insert( &( s->directory ), dir_index, count ); + if ( result == NULL ) + return NULL; + adjust_dirindices( s, dir_index, count ); return result; } -static int remove_direntries(BDRVVVFATState* s, int dir_index, int count) +static int remove_direntries( BDRVVVFATState* s, int dir_index, int count ) { - int ret = array_remove_slice(&(s->directory), dir_index, count); - if (ret) - return ret; - adjust_dirindices(s, dir_index, -count); + int ret = array_remove_slice( &( s->directory ), dir_index, count ); + if ( ret ) + return ret; + adjust_dirindices( s, dir_index, -count ); return 0; } @@ -2146,89 +2114,79 @@ static int remove_direntries(BDRVVVFATState* s, int dir_index, int count) * to the modified fat, and the corresponding entries in s->mapping are * adjusted) */ -static int commit_mappings(BDRVVVFATState* s, - uint32_t first_cluster, int dir_index) +static int commit_mappings( BDRVVVFATState* s, uint32_t first_cluster, int dir_index ) { - mapping_t* mapping = find_mapping_for_cluster(s, first_cluster); - direntry_t* direntry = array_get(&(s->directory), dir_index); + mapping_t* mapping = find_mapping_for_cluster( s, first_cluster ); + direntry_t* direntry = array_get( &( s->directory ), dir_index ); uint32_t cluster = first_cluster; - vvfat_close_current_file(s); + vvfat_close_current_file( s ); - assert(mapping); - assert(mapping->begin == first_cluster); + assert( mapping ); + assert( mapping->begin == first_cluster ); mapping->first_mapping_index = -1; mapping->dir_index = dir_index; - mapping->mode = (dir_index <= 0 || is_directory(direntry)) ? - MODE_DIRECTORY : MODE_NORMAL; + mapping->mode = ( dir_index <= 0 || is_directory( direntry ) ) ? MODE_DIRECTORY : MODE_NORMAL; - while (!fat_eof(s, cluster)) { - uint32_t c, c1; + while ( !fat_eof( s, cluster ) ) { + uint32_t c, c1; - for (c = cluster, c1 = modified_fat_get(s, c); c + 1 == c1; - c = c1, c1 = modified_fat_get(s, c1)); + for ( c = cluster, c1 = modified_fat_get( s, c ); c + 1 == c1; c = c1, c1 = modified_fat_get( s, c1 ) ) + ; - c++; - if (c > mapping->end) { - int index = array_index(&(s->mapping), mapping); - int i, max_i = s->mapping.next - index; - for (i = 1; i < max_i && mapping[i].begin < c; i++); - while (--i > 0) - remove_mapping(s, index + 1); - } - assert(mapping == array_get(&(s->mapping), s->mapping.next - 1) - || mapping[1].begin >= c); - mapping->end = c; + c++; + if ( c > mapping->end ) { + int index = array_index( &( s->mapping ), mapping ); + int i, max_i = s->mapping.next - index; + for ( i = 1; i < max_i && mapping[ i ].begin < c; i++ ) + ; + while ( --i > 0 ) + remove_mapping( s, index + 1 ); + } + assert( mapping == array_get( &( s->mapping ), s->mapping.next - 1 ) || mapping[ 1 ].begin >= c ); + mapping->end = c; - if (!fat_eof(s, c1)) { - int i = find_mapping_for_cluster_aux(s, c1, 0, s->mapping.next); - mapping_t* next_mapping = i >= s->mapping.next ? NULL : - array_get(&(s->mapping), i); + if ( !fat_eof( s, c1 ) ) { + int i = find_mapping_for_cluster_aux( s, c1, 0, s->mapping.next ); + mapping_t* next_mapping = i >= s->mapping.next ? NULL : array_get( &( s->mapping ), i ); - if (next_mapping == NULL || next_mapping->begin > c1) { - int i1 = array_index(&(s->mapping), mapping); + if ( next_mapping == NULL || next_mapping->begin > c1 ) { + int i1 = array_index( &( s->mapping ), mapping ); - next_mapping = insert_mapping(s, c1, c1+1); + next_mapping = insert_mapping( s, c1, c1 + 1 ); - if (c1 < c) - i1++; - mapping = array_get(&(s->mapping), i1); - } + if ( c1 < c ) + i1++; + mapping = array_get( &( s->mapping ), i1 ); + } - next_mapping->dir_index = mapping->dir_index; - next_mapping->first_mapping_index = - mapping->first_mapping_index < 0 ? - array_index(&(s->mapping), mapping) : - mapping->first_mapping_index; - next_mapping->path = mapping->path; - next_mapping->mode = mapping->mode; - next_mapping->read_only = mapping->read_only; - if (mapping->mode & MODE_DIRECTORY) { - next_mapping->info.dir.parent_mapping_index = - mapping->info.dir.parent_mapping_index; - next_mapping->info.dir.first_dir_index = - mapping->info.dir.first_dir_index + - 0x10 * s->sectors_per_cluster * - (mapping->end - mapping->begin); - } else - next_mapping->info.file.offset = mapping->info.file.offset + - mapping->end - mapping->begin; + next_mapping->dir_index = mapping->dir_index; + next_mapping->first_mapping_index = + mapping->first_mapping_index < 0 ? array_index( &( s->mapping ), mapping ) : mapping->first_mapping_index; + next_mapping->path = mapping->path; + next_mapping->mode = mapping->mode; + next_mapping->read_only = mapping->read_only; + if ( mapping->mode & MODE_DIRECTORY ) { + next_mapping->info.dir.parent_mapping_index = mapping->info.dir.parent_mapping_index; + next_mapping->info.dir.first_dir_index = + mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * ( mapping->end - mapping->begin ); + } else + next_mapping->info.file.offset = mapping->info.file.offset + mapping->end - mapping->begin; - mapping = next_mapping; - } - - cluster = c1; + mapping = next_mapping; + } + + cluster = c1; } return 0; } -static int commit_direntries(BDRVVVFATState* s, - int dir_index, int parent_mapping_index) +static int commit_direntries( BDRVVVFATState* s, int dir_index, int parent_mapping_index ) { - direntry_t* direntry = array_get(&(s->directory), dir_index); - uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry(direntry); - mapping_t* mapping = find_mapping_for_cluster(s, first_cluster); + direntry_t* direntry = array_get( &( s->directory ), dir_index ); + uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry( direntry ); + mapping_t* mapping = find_mapping_for_cluster( s, first_cluster ); int factor = 0x10 * s->sectors_per_cluster; int old_cluster_count, new_cluster_count; @@ -2237,65 +2195,57 @@ static int commit_direntries(BDRVVVFATState* s, int ret, i; uint32_t c; - DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n", mapping->path, parent_mapping_index)); + DLOG( fprintf( stderr, "commit_direntries for %s, parent_mapping_index %d\n", mapping->path, parent_mapping_index ) ); - assert(direntry); - assert(mapping); - assert(mapping->begin == first_cluster); - assert(mapping->info.dir.first_dir_index < s->directory.next); - assert(mapping->mode & MODE_DIRECTORY); - assert(dir_index == 0 || is_directory(direntry)); + assert( direntry ); + assert( mapping ); + assert( mapping->begin == first_cluster ); + assert( mapping->info.dir.first_dir_index < s->directory.next ); + assert( mapping->mode & MODE_DIRECTORY ); + assert( dir_index == 0 || is_directory( direntry ) ); mapping->info.dir.parent_mapping_index = parent_mapping_index; - if (first_cluster == 0) { - old_cluster_count = new_cluster_count = - s->last_cluster_of_root_directory; + if ( first_cluster == 0 ) { + old_cluster_count = new_cluster_count = s->last_cluster_of_root_directory; } else { - for (old_cluster_count = 0, c = first_cluster; !fat_eof(s, c); - c = fat_get(s, c)) - old_cluster_count++; + for ( old_cluster_count = 0, c = first_cluster; !fat_eof( s, c ); c = fat_get( s, c ) ) + old_cluster_count++; - for (new_cluster_count = 0, c = first_cluster; !fat_eof(s, c); - c = modified_fat_get(s, c)) - new_cluster_count++; + for ( new_cluster_count = 0, c = first_cluster; !fat_eof( s, c ); c = modified_fat_get( s, c ) ) + new_cluster_count++; } - if (new_cluster_count > old_cluster_count) { - if (insert_direntries(s, - current_dir_index + factor * old_cluster_count, - factor * (new_cluster_count - old_cluster_count)) == NULL) - return -1; - } else if (new_cluster_count < old_cluster_count) - remove_direntries(s, - current_dir_index + factor * new_cluster_count, - factor * (old_cluster_count - new_cluster_count)); + if ( new_cluster_count > old_cluster_count ) { + if ( insert_direntries( s, current_dir_index + factor * old_cluster_count, factor * ( new_cluster_count - old_cluster_count ) ) == + NULL ) + return -1; + } else if ( new_cluster_count < old_cluster_count ) + remove_direntries( s, current_dir_index + factor * new_cluster_count, factor * ( old_cluster_count - new_cluster_count ) ); - for (c = first_cluster; !fat_eof(s, c); c = modified_fat_get(s, c)) { - void* direntry = array_get(&(s->directory), current_dir_index); - int ret = vvfat_read(s->bs, cluster2sector(s, c), direntry, - s->sectors_per_cluster); - if (ret) - return ret; - assert(!strncmp((char *) s->directory.pointer, VOLUME_LABEL, strlen(VOLUME_LABEL))); - current_dir_index += factor; + for ( c = first_cluster; !fat_eof( s, c ); c = modified_fat_get( s, c ) ) { + void* direntry = array_get( &( s->directory ), current_dir_index ); + int ret = vvfat_read( s->bs, cluster2sector( s, c ), direntry, s->sectors_per_cluster ); + if ( ret ) + return ret; + assert( !strncmp( ( char* )s->directory.pointer, VOLUME_LABEL, strlen( VOLUME_LABEL ) ) ); + current_dir_index += factor; } - ret = commit_mappings(s, first_cluster, dir_index); - if (ret) - return ret; + ret = commit_mappings( s, first_cluster, dir_index ); + if ( ret ) + return ret; /* recurse */ - for (i = 0; i < factor * new_cluster_count; i++) { - direntry = array_get(&(s->directory), first_dir_index + i); - if (is_directory(direntry) && !is_dot(direntry)) { - mapping = find_mapping_for_cluster(s, first_cluster); - assert(mapping->mode & MODE_DIRECTORY); - ret = commit_direntries(s, first_dir_index + i, - array_index(&(s->mapping), mapping)); - if (ret) - return ret; - } + for ( i = 0; i < factor * new_cluster_count; i++ ) { + direntry = array_get( &( s->directory ), first_dir_index + i ); + if ( is_directory( direntry ) && !is_dot( direntry ) ) { + mapping = find_mapping_for_cluster( s, first_cluster ); + assert( mapping->mode & MODE_DIRECTORY ); + ret = commit_direntries( s, first_dir_index + i, array_index( &( s->mapping ), mapping ) ); + if ( ret ) + return ret; + } } return 0; @@ -2303,241 +2253,230 @@ static int commit_direntries(BDRVVVFATState* s, /* commit one file (adjust contents, adjust mapping), return first_mapping_index */ -static int commit_one_file(BDRVVVFATState* s, - int dir_index, uint32_t offset) +static int commit_one_file( BDRVVVFATState* s, int dir_index, uint32_t offset ) { - direntry_t* direntry = array_get(&(s->directory), dir_index); - uint32_t c = begin_of_direntry(direntry); + direntry_t* direntry = array_get( &( s->directory ), dir_index ); + uint32_t c = begin_of_direntry( direntry ); uint32_t first_cluster = c; - mapping_t* mapping = find_mapping_for_cluster(s, c); - uint32_t size = filesize_of_direntry(direntry); - unsigned char* cluster = malloc(s->cluster_size); + mapping_t* mapping = find_mapping_for_cluster( s, c ); + uint32_t size = filesize_of_direntry( direntry ); + unsigned char* cluster = malloc( s->cluster_size ); uint32_t i; int fd = 0; - assert(offset < size); - assert((offset % s->cluster_size) == 0); + assert( offset < size ); + assert( ( offset % s->cluster_size ) == 0 ); - for (i = s->cluster_size; i < offset; i += s->cluster_size) - c = modified_fat_get(s, c); + for ( i = s->cluster_size; i < offset; i += s->cluster_size ) + c = modified_fat_get( s, c ); - fd = open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666); - if (fd < 0) { - fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path, - strerror(errno), errno); - return fd; + fd = open( mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666 ); + if ( fd < 0 ) { + fprintf( stderr, "Could not open %s... (%s, %d)\n", mapping->path, strerror( errno ), errno ); + return fd; } - if (offset > 0) - if (lseek(fd, offset, SEEK_SET) != offset) - return -3; + if ( offset > 0 ) + if ( lseek( fd, offset, SEEK_SET ) != offset ) + return -3; - while (offset < size) { - uint32_t c1; - int rest_size = (size - offset > s->cluster_size ? - s->cluster_size : size - offset); - int ret; + while ( offset < size ) { + uint32_t c1; + int rest_size = ( size - offset > s->cluster_size ? s->cluster_size : size - offset ); + int ret; - c1 = modified_fat_get(s, c); + c1 = modified_fat_get( s, c ); - assert((size - offset == 0 && fat_eof(s, c)) || - (size > offset && c >=2 && !fat_eof(s, c))); - assert(size >= 0); + assert( ( size - offset == 0 && fat_eof( s, c ) ) || ( size > offset && c >= 2 && !fat_eof( s, c ) ) ); + assert( size >= 0 ); - ret = vvfat_read(s->bs, cluster2sector(s, c), - cluster, (rest_size + 0x1ff) / 0x200); + ret = vvfat_read( s->bs, cluster2sector( s, c ), cluster, ( rest_size + 0x1ff ) / 0x200 ); - if (ret < 0) - return ret; + if ( ret < 0 ) + return ret; - if (write(fd, cluster, rest_size) < 0) - return -2; + if ( write( fd, cluster, rest_size ) < 0 ) + return -2; - offset += rest_size; - c = c1; + offset += rest_size; + c = c1; } - if (ftruncate(fd, size)) { - perror("ftruncate()"); - close(fd); + if ( ftruncate( fd, size ) ) { + perror( "ftruncate()" ); + close( fd ); return -4; } - return commit_mappings(s, first_cluster, dir_index); + return commit_mappings( s, first_cluster, dir_index ); } #ifdef DEBUG /* test, if all mappings point to valid direntries */ -static void check1(BDRVVVFATState* s, const char *where) +static void check1( BDRVVVFATState* s, const char* where ) { int i; - for (i = 0; i < s->mapping.next; i++) { - mapping_t* mapping = array_get(&(s->mapping), i); - if (mapping->mode & MODE_DELETED) { - DLOG(fprintf(stderr, "%s: deleted\n", where); print_mapping(mapping)); - continue; - } - assert(mapping->dir_index >= 0); - assert(mapping->dir_index < s->directory.next); - direntry_t* direntry = array_get(&(s->directory), mapping->dir_index); - assert(mapping->begin == begin_of_direntry(direntry) || mapping->first_mapping_index >= 0); - if (mapping->mode & MODE_DIRECTORY) { - assert(mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * (mapping->end - mapping->begin) <= s->directory.next); - assert((mapping->info.dir.first_dir_index % (0x10 * s->sectors_per_cluster)) == 0); - } + for ( i = 0; i < s->mapping.next; i++ ) { + mapping_t* mapping = array_get( &( s->mapping ), i ); + if ( mapping->mode & MODE_DELETED ) { + DLOG( fprintf( stderr, "%s: deleted\n", where ); print_mapping( mapping ) ); + continue; + } + assert( mapping->dir_index >= 0 ); + assert( mapping->dir_index < s->directory.next ); + direntry_t* direntry = array_get( &( s->directory ), mapping->dir_index ); + assert( mapping->begin == begin_of_direntry( direntry ) || mapping->first_mapping_index >= 0 ); + if ( mapping->mode & MODE_DIRECTORY ) { + assert( mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * ( mapping->end - mapping->begin ) <= + s->directory.next ); + assert( ( mapping->info.dir.first_dir_index % ( 0x10 * s->sectors_per_cluster ) ) == 0 ); + } } } /* test, if all direntries have mappings */ -static void check2(BDRVVVFATState* s, const char *where) +static void check2( BDRVVVFATState* s, const char* where ) { int i; int first_mapping = -1; - for (i = 0; i < s->directory.next; i++) { - direntry_t* direntry = array_get(&(s->directory), i); + for ( i = 0; i < s->directory.next; i++ ) { + direntry_t* direntry = array_get( &( s->directory ), i ); - if (is_short_name(direntry) && begin_of_direntry(direntry)) { - mapping_t* mapping = find_mapping_for_cluster(s, begin_of_direntry(direntry)); - assert(mapping); - assert(mapping->dir_index == i || is_dot(direntry)); - assert(mapping->begin == begin_of_direntry(direntry) || is_dot(direntry)); - } + if ( is_short_name( direntry ) && begin_of_direntry( direntry ) ) { + mapping_t* mapping = find_mapping_for_cluster( s, begin_of_direntry( direntry ) ); + assert( mapping ); + assert( mapping->dir_index == i || is_dot( direntry ) ); + assert( mapping->begin == begin_of_direntry( direntry ) || is_dot( direntry ) ); + } - if ((i % (0x10 * s->sectors_per_cluster)) == 0) { - /* cluster start */ - int j, count = 0; + if ( ( i % ( 0x10 * s->sectors_per_cluster ) ) == 0 ) { + /* cluster start */ + int j, count = 0; - for (j = 0; j < s->mapping.next; j++) { - mapping_t* mapping = array_get(&(s->mapping), j); - if (mapping->mode & MODE_DELETED) - continue; - if (mapping->mode & MODE_DIRECTORY) { - if (mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i) { - assert(++count == 1); - if (mapping->first_mapping_index == -1) - first_mapping = array_index(&(s->mapping), mapping); - else - assert(first_mapping == mapping->first_mapping_index); - if (mapping->info.dir.parent_mapping_index < 0) - assert(j == 0); - else { - mapping_t* parent = array_get(&(s->mapping), mapping->info.dir.parent_mapping_index); - assert(parent->mode & MODE_DIRECTORY); - assert(parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index); - } - } - } - } - if (count == 0) - first_mapping = -1; - } + for ( j = 0; j < s->mapping.next; j++ ) { + mapping_t* mapping = array_get( &( s->mapping ), j ); + if ( mapping->mode & MODE_DELETED ) + continue; + if ( mapping->mode & MODE_DIRECTORY ) { + if ( mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i ) { + assert( ++count == 1 ); + if ( mapping->first_mapping_index == -1 ) + first_mapping = array_index( &( s->mapping ), mapping ); + else + assert( first_mapping == mapping->first_mapping_index ); + if ( mapping->info.dir.parent_mapping_index < 0 ) + assert( j == 0 ); + else { + mapping_t* parent = array_get( &( s->mapping ), mapping->info.dir.parent_mapping_index ); + assert( parent->mode & MODE_DIRECTORY ); + assert( parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index ); + } + } + } + } + if ( count == 0 ) + first_mapping = -1; + } } } #endif -static int handle_renames_and_mkdirs(BDRVVVFATState* s) +static int handle_renames_and_mkdirs( BDRVVVFATState* s ) { int i; #ifdef DEBUG - fprintf(stderr, "handle_renames\n"); - for (i = 0; i < s->commits.next; i++) { - commit_t* commit = array_get(&(s->commits), i); - fprintf(stderr, "%d, %s (%d, %d)\n", i, commit->path ? commit->path : "(null)", commit->param.rename.cluster, commit->action); + fprintf( stderr, "handle_renames\n" ); + for ( i = 0; i < s->commits.next; i++ ) { + commit_t* commit = array_get( &( s->commits ), i ); + fprintf( stderr, "%d, %s (%d, %d)\n", i, commit->path ? commit->path : "(null)", commit->param.rename.cluster, commit->action ); } #endif - for (i = 0; i < s->commits.next;) { - commit_t* commit = array_get(&(s->commits), i); - if (commit->action == ACTION_RENAME) { - mapping_t* mapping = find_mapping_for_cluster(s, - commit->param.rename.cluster); - char* old_path = mapping->path; + for ( i = 0; i < s->commits.next; ) { + commit_t* commit = array_get( &( s->commits ), i ); + if ( commit->action == ACTION_RENAME ) { + mapping_t* mapping = find_mapping_for_cluster( s, commit->param.rename.cluster ); + char* old_path = mapping->path; - assert(commit->path); - mapping->path = commit->path; - if (rename(old_path, mapping->path)) - return -2; + assert( commit->path ); + mapping->path = commit->path; + if ( rename( old_path, mapping->path ) ) + return -2; - if (mapping->mode & MODE_DIRECTORY) { - int l1 = strlen(mapping->path); - int l2 = strlen(old_path); - int diff = l1 - l2; - direntry_t* direntry = array_get(&(s->directory), - mapping->info.dir.first_dir_index); - uint32_t c = mapping->begin; - int i = 0; + if ( mapping->mode & MODE_DIRECTORY ) { + int l1 = strlen( mapping->path ); + int l2 = strlen( old_path ); + int diff = l1 - l2; + direntry_t* direntry = array_get( &( s->directory ), mapping->info.dir.first_dir_index ); + uint32_t c = mapping->begin; + int i = 0; - /* recurse */ - while (!fat_eof(s, c)) { - do { - direntry_t* d = direntry + i; + /* recurse */ + while ( !fat_eof( s, c ) ) { + do { + direntry_t* d = direntry + i; - if (is_file(d) || (is_directory(d) && !is_dot(d))) { - mapping_t* m = find_mapping_for_cluster(s, - begin_of_direntry(d)); - int l = strlen(m->path); - char* new_path = malloc(l + diff + 1); + if ( is_file( d ) || ( is_directory( d ) && !is_dot( d ) ) ) { + mapping_t* m = find_mapping_for_cluster( s, begin_of_direntry( d ) ); + int l = strlen( m->path ); + char* new_path = malloc( l + diff + 1 ); - assert(!strncmp(m->path, mapping->path, l2)); + assert( !strncmp( m->path, mapping->path, l2 ) ); - strcpy(new_path, mapping->path); - strcpy(new_path + l1, m->path + l2); + strcpy( new_path, mapping->path ); + strcpy( new_path + l1, m->path + l2 ); - schedule_rename(s, m->begin, new_path); - } - i++; - } while((i % (0x10 * s->sectors_per_cluster)) != 0); - c = fat_get(s, c); - } - } + schedule_rename( s, m->begin, new_path ); + } + i++; + } while ( ( i % ( 0x10 * s->sectors_per_cluster ) ) != 0 ); + c = fat_get( s, c ); + } + } - free(old_path); - array_remove(&(s->commits), i); - continue; - } else if (commit->action == ACTION_MKDIR) { - mapping_t* mapping; - int j, parent_path_len; + free( old_path ); + array_remove( &( s->commits ), i ); + continue; + } else if ( commit->action == ACTION_MKDIR ) { + mapping_t* mapping; + int j, parent_path_len; #ifdef __MINGW32__ - if (mkdir(commit->path)) + if ( mkdir( commit->path ) ) return -5; #else - if (mkdir(commit->path, 0755)) + if ( mkdir( commit->path, 0755 ) ) return -5; #endif - mapping = insert_mapping(s, commit->param.mkdir.cluster, - commit->param.mkdir.cluster + 1); - if (mapping == NULL) - return -6; + mapping = insert_mapping( s, commit->param.mkdir.cluster, commit->param.mkdir.cluster + 1 ); + if ( mapping == NULL ) + return -6; - mapping->mode = MODE_DIRECTORY; - mapping->read_only = 0; - mapping->path = commit->path; - j = s->directory.next; - assert(j); - insert_direntries(s, s->directory.next, - 0x10 * s->sectors_per_cluster); - mapping->info.dir.first_dir_index = j; + mapping->mode = MODE_DIRECTORY; + mapping->read_only = 0; + mapping->path = commit->path; + j = s->directory.next; + assert( j ); + insert_direntries( s, s->directory.next, 0x10 * s->sectors_per_cluster ); + mapping->info.dir.first_dir_index = j; - parent_path_len = strlen(commit->path) - - strlen(get_basename(commit->path)) - 1; - for (j = 0; j < s->mapping.next; j++) { - mapping_t* m = array_get(&(s->mapping), j); - if (m->first_mapping_index < 0 && m != mapping && - !strncmp(m->path, mapping->path, parent_path_len) && - strlen(m->path) == parent_path_len) - break; - } - assert(j < s->mapping.next); - mapping->info.dir.parent_mapping_index = j; + parent_path_len = strlen( commit->path ) - strlen( get_basename( commit->path ) ) - 1; + for ( j = 0; j < s->mapping.next; j++ ) { + mapping_t* m = array_get( &( s->mapping ), j ); + if ( m->first_mapping_index < 0 && m != mapping && !strncmp( m->path, mapping->path, parent_path_len ) && + strlen( m->path ) == parent_path_len ) + break; + } + assert( j < s->mapping.next ); + mapping->info.dir.parent_mapping_index = j; - array_remove(&(s->commits), i); - continue; - } + array_remove( &( s->commits ), i ); + continue; + } - i++; + i++; } return 0; } @@ -2545,153 +2484,147 @@ static int handle_renames_and_mkdirs(BDRVVVFATState* s) /* * TODO: make sure that the short name is not matching *another* file */ -static int handle_commits(BDRVVVFATState* s) +static int handle_commits( BDRVVVFATState* s ) { int i, fail = 0; - vvfat_close_current_file(s); + vvfat_close_current_file( s ); - for (i = 0; !fail && i < s->commits.next; i++) { - commit_t* commit = array_get(&(s->commits), i); - switch(commit->action) { - case ACTION_RENAME: case ACTION_MKDIR: - assert(0); - fail = -2; - break; - case ACTION_WRITEOUT: { - direntry_t* entry = array_get(&(s->directory), - commit->param.writeout.dir_index); - uint32_t begin = begin_of_direntry(entry); - mapping_t* mapping = find_mapping_for_cluster(s, begin); + for ( i = 0; !fail && i < s->commits.next; i++ ) { + commit_t* commit = array_get( &( s->commits ), i ); + switch ( commit->action ) { + case ACTION_RENAME: + case ACTION_MKDIR: + assert( 0 ); + fail = -2; + break; + case ACTION_WRITEOUT: + { + direntry_t* entry = array_get( &( s->directory ), commit->param.writeout.dir_index ); + uint32_t begin = begin_of_direntry( entry ); + mapping_t* mapping = find_mapping_for_cluster( s, begin ); - assert(mapping); - assert(mapping->begin == begin); - assert(commit->path == NULL); + assert( mapping ); + assert( mapping->begin == begin ); + assert( commit->path == NULL ); - if (commit_one_file(s, commit->param.writeout.dir_index, - commit->param.writeout.modified_offset)) - fail = -3; + if ( commit_one_file( s, commit->param.writeout.dir_index, commit->param.writeout.modified_offset ) ) + fail = -3; - break; - } - case ACTION_NEW_FILE: { - int begin = commit->param.new_file.first_cluster; - mapping_t* mapping = find_mapping_for_cluster(s, begin); - direntry_t* entry; - int i; + break; + } + case ACTION_NEW_FILE: + { + int begin = commit->param.new_file.first_cluster; + mapping_t* mapping = find_mapping_for_cluster( s, begin ); + direntry_t* entry; + int i; - /* find direntry */ - for (i = 0; i < s->directory.next; i++) { - entry = array_get(&(s->directory), i); - if (is_file(entry) && begin_of_direntry(entry) == begin) - break; - } + /* find direntry */ + for ( i = 0; i < s->directory.next; i++ ) { + entry = array_get( &( s->directory ), i ); + if ( is_file( entry ) && begin_of_direntry( entry ) == begin ) + break; + } - if (i >= s->directory.next) { - fail = -6; - continue; - } + if ( i >= s->directory.next ) { + fail = -6; + continue; + } - /* make sure there exists an initial mapping */ - if (mapping && mapping->begin != begin) { - mapping->end = begin; - mapping = NULL; - } - if (mapping == NULL) { - mapping = insert_mapping(s, begin, begin+1); - } - /* most members will be fixed in commit_mappings() */ - assert(commit->path); - mapping->path = commit->path; - mapping->read_only = 0; - mapping->mode = MODE_NORMAL; - mapping->info.file.offset = 0; + /* make sure there exists an initial mapping */ + if ( mapping && mapping->begin != begin ) { + mapping->end = begin; + mapping = NULL; + } + if ( mapping == NULL ) { + mapping = insert_mapping( s, begin, begin + 1 ); + } + /* most members will be fixed in commit_mappings() */ + assert( commit->path ); + mapping->path = commit->path; + mapping->read_only = 0; + mapping->mode = MODE_NORMAL; + mapping->info.file.offset = 0; - if (commit_one_file(s, i, 0)) - fail = -7; + if ( commit_one_file( s, i, 0 ) ) + fail = -7; - break; - } - default: - assert(0); - } + break; + } + default: + assert( 0 ); + } } - if (i > 0 && array_remove_slice(&(s->commits), 0, i)) - return -1; + if ( i > 0 && array_remove_slice( &( s->commits ), 0, i ) ) + return -1; return fail; } -static int handle_deletes(BDRVVVFATState* s) +static int handle_deletes( BDRVVVFATState* s ) { int i, deferred = 1, deleted = 1; /* delete files corresponding to mappings marked as deleted */ /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */ - while (deferred && deleted) { - deferred = 0; - deleted = 0; + while ( deferred && deleted ) { + deferred = 0; + deleted = 0; - for (i = 1; i < s->mapping.next; i++) { - mapping_t* mapping = array_get(&(s->mapping), i); - if (mapping->mode & MODE_DELETED) { - direntry_t* entry = array_get(&(s->directory), - mapping->dir_index); + for ( i = 1; i < s->mapping.next; i++ ) { + mapping_t* mapping = array_get( &( s->mapping ), i ); + if ( mapping->mode & MODE_DELETED ) { + direntry_t* entry = array_get( &( s->directory ), mapping->dir_index ); - DLOG(fprintf(stderr, "%s:%u: ", __FUNCTION__, __LINE__); print_mapping(mapping); print_direntry(entry)); + DLOG( fprintf( stderr, "%s:%u: ", __FUNCTION__, __LINE__ ); print_mapping( mapping ); print_direntry( entry ) ); - if (is_free(entry)) { - /* remove file/directory */ - if (mapping->mode & MODE_DIRECTORY) { - int j, next_dir_index = s->directory.next, - first_dir_index = mapping->info.dir.first_dir_index; + if ( is_free( entry ) ) { + /* remove file/directory */ + if ( mapping->mode & MODE_DIRECTORY ) { + int j, next_dir_index = s->directory.next, first_dir_index = mapping->info.dir.first_dir_index; - if (rmdir(mapping->path) < 0) { - if (errno == ENOTEMPTY) { - deferred++; - continue; - } else - return -5; - } + if ( rmdir( mapping->path ) < 0 ) { + if ( errno == ENOTEMPTY ) { + deferred++; + continue; + } else + return -5; + } - for (j = 1; j < s->mapping.next; j++) { - mapping_t* m = array_get(&(s->mapping), j); - if (m->mode & MODE_DIRECTORY && - m->info.dir.first_dir_index > - first_dir_index && - m->info.dir.first_dir_index < - next_dir_index) - next_dir_index = - m->info.dir.first_dir_index; - } - remove_direntries(s, first_dir_index, - next_dir_index - first_dir_index); + for ( j = 1; j < s->mapping.next; j++ ) { + mapping_t* m = array_get( &( s->mapping ), j ); + if ( m->mode & MODE_DIRECTORY && m->info.dir.first_dir_index > first_dir_index && + m->info.dir.first_dir_index < next_dir_index ) + next_dir_index = m->info.dir.first_dir_index; + } + remove_direntries( s, first_dir_index, next_dir_index - first_dir_index ); - deleted++; - } else { - if (unlink(mapping->path)) - return -4; - deleted++; - } + deleted++; + } else { + if ( unlink( mapping->path ) ) + return -4; + deleted++; + } - DLOG(fprintf(stderr, "DELETE (%d)\n", i); print_mapping(mapping); print_direntry(entry)); - remove_mapping(s, i); - } - } - } + DLOG( fprintf( stderr, "DELETE (%d)\n", i ); print_mapping( mapping ); print_direntry( entry ) ); + remove_mapping( s, i ); + } + } + } } return 0; } -static int have_deletes(BDRVVVFATState* s) +static int have_deletes( BDRVVVFATState* s ) { int i; - for (i = 0; i < s->mapping.next; i++) { - mapping_t* mapping = array_get(&(s->mapping), i); - if (mapping->mode & MODE_DELETED) { - return 1; - } + for ( i = 0; i < s->mapping.next; i++ ) { + mapping_t* mapping = array_get( &( s->mapping ), i ); + if ( mapping->mode & MODE_DELETED ) { + return 1; + } } return 0; @@ -2705,88 +2638,86 @@ static int have_deletes(BDRVVVFATState* s) * - recurse direntries from root (using bs->bdrv_read) * - delete files corresponding to mappings marked as deleted */ -static int do_commit(BDRVVVFATState* s) +static int do_commit( BDRVVVFATState* s ) { int ret = 0; /* the real meat are the commits. Nothing to do? Move along! */ - if ((0 == s->commits.next) && (0 == have_deletes(s))) { - DLOG(fprintf(stderr, "%s:%u: nothing to do\n", __FUNCTION__, __LINE__)); - return 0; + if ( ( 0 == s->commits.next ) && ( 0 == have_deletes( s ) ) ) { + DLOG( fprintf( stderr, "%s:%u: nothing to do\n", __FUNCTION__, __LINE__ ) ); + return 0; } - vvfat_close_current_file(s); + vvfat_close_current_file( s ); - ret = handle_renames_and_mkdirs(s); - if (ret) { - fprintf(stderr, "Error handling renames (%d)\n", ret); - assert(0); - return ret; + ret = handle_renames_and_mkdirs( s ); + if ( ret ) { + fprintf( stderr, "Error handling renames (%d)\n", ret ); + assert( 0 ); + return ret; } - /* copy FAT (with bdrv_read) */ - memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat); + /* copy FAT (with bdrv_read) */ + memcpy( s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat ); /* recurse direntries from root (using bs->bdrv_read) */ - ret = commit_direntries(s, 0, -1); - if (ret) { - fprintf(stderr, "Fatal: error while committing (%d)\n", ret); - assert(0); - return ret; + ret = commit_direntries( s, 0, -1 ); + if ( ret ) { + fprintf( stderr, "Fatal: error while committing (%d)\n", ret ); + assert( 0 ); + return ret; } - ret = handle_commits(s); - if (ret) { - fprintf(stderr, "Error handling commits (%d)\n", ret); - assert(0); - return ret; + ret = handle_commits( s ); + if ( ret ) { + fprintf( stderr, "Error handling commits (%d)\n", ret ); + assert( 0 ); + return ret; } - ret = handle_deletes(s); - if (ret) { - fprintf(stderr, "Error deleting\n"); - assert(0); - return ret; + ret = handle_deletes( s ); + if ( ret ) { + fprintf( stderr, "Error deleting\n" ); + assert( 0 ); + return ret; } - s->qcow->drv->bdrv_make_empty(s->qcow); + s->qcow->drv->bdrv_make_empty( s->qcow ); - memset(s->used_clusters, 0, sector2cluster(s, s->sector_count)); + memset( s->used_clusters, 0, sector2cluster( s, s->sector_count ) ); -DLOG(checkpoint(__FUNCTION__)); + DLOG( checkpoint( __FUNCTION__ ) ); return 0; } -static int try_commit(BDRVVVFATState* s) +static int try_commit( BDRVVVFATState* s ) { - vvfat_close_current_file(s); + vvfat_close_current_file( s ); -DLOG(checkpoint(__FUNCTION__)); + DLOG( checkpoint( __FUNCTION__ ) ); - if(!is_consistent(s)) - return -1; + if ( !is_consistent( s ) ) + return -1; - return do_commit(s); + return do_commit( s ); } -static void -vvfat_write_timer(void *opaque) +static void vvfat_write_timer( void* opaque ) { - BDRVVVFATState *s = opaque; + BDRVVVFATState* s = opaque; - DLOG(fprintf(stderr, "%s:%u:\n", __FUNCTION__, __LINE__)); + DLOG( fprintf( stderr, "%s:%u:\n", __FUNCTION__, __LINE__ ) ); -DLOG(checkpoint("vvfat_write_timer: before try_commits")); + DLOG( checkpoint( "vvfat_write_timer: before try_commits" ) ); - try_commit(s); + try_commit( s ); -DLOG(checkpoint("vvfat_write_timer: after try_commits")); + DLOG( checkpoint( "vvfat_write_timer: after try_commits" ) ); } -static int vvfat_write(BlockDriverState *bs, int64_t sector_num, - const uint8_t *buf, int nb_sectors) +static int vvfat_write( BlockDriverState* bs, int64_t sector_num, const uint8_t* buf, int nb_sectors ) { - BDRVVVFATState *s = bs->opaque; + BDRVVVFATState* s = bs->opaque; mapping_t* mapping; #ifdef DEBUG_SECTORS int32_t orig_sector = sector_num; @@ -2794,9 +2725,9 @@ static int vvfat_write(BlockDriverState *bs, int64_t sector_num, int64_t start_cluster, end_cluster; int i, ret; -DLOG(checkpoint(__FUNCTION__)); + DLOG( checkpoint( __FUNCTION__ ) ); - vvfat_close_current_file(s); + vvfat_close_current_file( s ); /* * Some sanity checks: @@ -2804,200 +2735,196 @@ DLOG(checkpoint(__FUNCTION__)); * - do not allow to write non-ASCII filenames */ - if (sector_num < s->first_sectors_number) { - DLOG(fprintf(stderr, "%s:%u: sector: %u nb %u (first: %u)\n", __FUNCTION__, __LINE__, (int) sector_num, nb_sectors, s->first_sectors_number)); - return -1; + if ( sector_num < s->first_sectors_number ) { + DLOG( fprintf( stderr, "%s:%u: sector: %u nb %u (first: %u)\n", __FUNCTION__, __LINE__, ( int )sector_num, nb_sectors, + s->first_sectors_number ) ); + return -1; } #ifdef DEBUG_SECTORS - for (i = 0; i < nb_sectors; i++) { - fprintf(stderr, "%s:%u: sector %u:\n", __FUNCTION__, __LINE__, orig_sector + i); - hexdump(buf + i * 0x200, 0x200); + for ( i = 0; i < nb_sectors; i++ ) { + fprintf( stderr, "%s:%u: sector %u:\n", __FUNCTION__, __LINE__, orig_sector + i ); + hexdump( buf + i * 0x200, 0x200 ); } #endif - DLOG(fprintf(stderr, "%s:%u: check mappings, sector: %u nb %u (faked: %u)\n", __FUNCTION__, __LINE__, (int) sector_num, nb_sectors, s->faked_sectors)); + DLOG( fprintf( stderr, "%s:%u: check mappings, sector: %u nb %u (faked: %u)\n", __FUNCTION__, __LINE__, ( int )sector_num, nb_sectors, + s->faked_sectors ) ); - if (sector_num >= s->faked_sectors) { - start_cluster = sector2cluster(s, sector_num); + if ( sector_num >= s->faked_sectors ) { + start_cluster = sector2cluster( s, sector_num ); } else { start_cluster = 0; } - if (sector_num + nb_sectors - 1 >= s->faked_sectors) { - end_cluster = sector2cluster(s, sector_num + nb_sectors - 1); + if ( sector_num + nb_sectors - 1 >= s->faked_sectors ) { + end_cluster = sector2cluster( s, sector_num + nb_sectors - 1 ); } else { end_cluster = -1; } - for (i = start_cluster; i <= end_cluster; ) { - DLOG(fprintf(stderr, "%s:%u: cluster %u\n", __FUNCTION__, __LINE__, i)); - mapping = find_mapping_for_cluster(s, i); - DLOG(fprintf(stderr, "%s:%u: mapping %p\n", __FUNCTION__, __LINE__, mapping)); - if (mapping) { - if (mapping->read_only) { - fprintf(stderr, "Tried to write to write-protected file %s\n", - mapping->path); - return -1; - } + for ( i = start_cluster; i <= end_cluster; ) { + DLOG( fprintf( stderr, "%s:%u: cluster %u\n", __FUNCTION__, __LINE__, i ) ); + mapping = find_mapping_for_cluster( s, i ); + DLOG( fprintf( stderr, "%s:%u: mapping %p\n", __FUNCTION__, __LINE__, mapping ) ); + if ( mapping ) { + if ( mapping->read_only ) { + fprintf( stderr, "Tried to write to write-protected file %s\n", mapping->path ); + return -1; + } - if (mapping->mode & MODE_DIRECTORY) { - int begin = cluster2sector(s, i); - int end = begin + s->sectors_per_cluster, k; - int dir_index; - const direntry_t* direntries; - long_file_name lfn; + if ( mapping->mode & MODE_DIRECTORY ) { + int begin = cluster2sector( s, i ); + int end = begin + s->sectors_per_cluster, k; + int dir_index; + const direntry_t* direntries; + long_file_name lfn; - lfn_init(&lfn); + lfn_init( &lfn ); - if (begin < sector_num) - begin = sector_num; - if (end > sector_num + nb_sectors) - end = sector_num + nb_sectors; - dir_index = mapping->dir_index + - 0x10 * (begin - mapping->begin * s->sectors_per_cluster); - direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num)); + if ( begin < sector_num ) + begin = sector_num; + if ( end > sector_num + nb_sectors ) + end = sector_num + nb_sectors; + dir_index = mapping->dir_index + 0x10 * ( begin - mapping->begin * s->sectors_per_cluster ); + direntries = ( direntry_t* )( buf + 0x200 * ( begin - sector_num ) ); - for (k = 0; k < (end - begin) * 0x10; k++) { - /* do not allow non-ASCII filenames */ - if (parse_long_name(&lfn, direntries + k) < 0) { - fprintf(stderr, "Warning: non-ASCII filename\n"); - return -1; - } - /* no access to the direntry of a read-only file */ - else if (is_short_name(direntries+k) && - (direntries[k].attributes & 1)) { - if (memcmp(direntries + k, - array_get(&(s->directory), dir_index + k), - sizeof(direntry_t))) { - fprintf(stderr, "Warning: tried to write to write-protected file\n"); - return -1; - } - } - } - } - i = mapping->end; - } else { - i++; - } + for ( k = 0; k < ( end - begin ) * 0x10; k++ ) { + /* do not allow non-ASCII filenames */ + if ( parse_long_name( &lfn, direntries + k ) < 0 ) { + fprintf( stderr, "Warning: non-ASCII filename\n" ); + return -1; + } + /* no access to the direntry of a read-only file */ + else if ( is_short_name( direntries + k ) && ( direntries[ k ].attributes & 1 ) ) { + if ( memcmp( direntries + k, array_get( &( s->directory ), dir_index + k ), sizeof( direntry_t ) ) ) { + fprintf( stderr, "Warning: tried to write to write-protected file\n" ); + return -1; + } + } + } + } + i = mapping->end; + } else { + i++; + } } /* * Use qcow backend. Commit later. */ - DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors)); - ret = s->qcow->drv->bdrv_write(s->qcow, sector_num, buf, nb_sectors); - if (ret < 0) { - fprintf(stderr, "Error writing to qcow backend\n"); - return ret; + DLOG( fprintf( stderr, "Write to qcow backend: %d + %d\n", ( int )sector_num, nb_sectors ) ); + ret = s->qcow->drv->bdrv_write( s->qcow, sector_num, buf, nb_sectors ); + if ( ret < 0 ) { + fprintf( stderr, "Error writing to qcow backend\n" ); + return ret; } - for (i = start_cluster; i <= end_cluster; i++) - s->used_clusters[i] |= USED_ALLOCATED; + for ( i = start_cluster; i <= end_cluster; i++ ) + s->used_clusters[ i ] |= USED_ALLOCATED; - qemu_mod_timer(s->write_timer, qemu_get_clock(rt_clock) + ticks_per_sec); + qemu_mod_timer( s->write_timer, qemu_get_clock( rt_clock ) + ticks_per_sec ); return 0; } -static int vvfat_is_allocated(BlockDriverState *bs, - int64_t sector_num, int nb_sectors, int* n) +static int vvfat_is_allocated( BlockDriverState* bs, int64_t sector_num, int nb_sectors, int* n ) { BDRVVVFATState* s = bs->opaque; *n = s->sector_count - sector_num; - if (*n > nb_sectors) - *n = nb_sectors; - else if (*n < 0) - return 0; - return 1; + if ( *n > nb_sectors ) + *n = nb_sectors; + else if ( *n < 0 ) + return 0; + return 1; } -static int enable_write(BDRVVVFATState *s) +static int enable_write( BDRVVVFATState* s ) { int error; int size; - - DLOG(fprintf(stderr, "%s:%u:\n", __FUNCTION__, __LINE__)); - size = sector2cluster(s, s->sector_count); - s->used_clusters = calloc(size, 1); + DLOG( fprintf( stderr, "%s:%u:\n", __FUNCTION__, __LINE__ ) ); - array_init(&(s->commits), sizeof(commit_t)); + size = sector2cluster( s, s->sector_count ); + s->used_clusters = calloc( size, 1 ); - s->qcow_filename = malloc(1024); - get_tmp_filename(s->qcow_filename, 1024); - error = bdrv_create(&bdrv_qcow, s->qcow_filename, s->sector_count, "", 0); - if (error < 0) { - DLOG(fprintf(stderr, "%s:%u: bdrv_create '%s': %d\n", __FUNCTION__, __LINE__, s->qcow_filename, error)); - return -1; + array_init( &( s->commits ), sizeof( commit_t ) ); + + s->qcow_filename = malloc( 1024 ); + get_tmp_filename( s->qcow_filename, 1024 ); + error = bdrv_create( &bdrv_qcow, s->qcow_filename, s->sector_count, "", 0 ); + if ( error < 0 ) { + DLOG( fprintf( stderr, "%s:%u: bdrv_create '%s': %d\n", __FUNCTION__, __LINE__, s->qcow_filename, error ) ); + return -1; } - s->qcow = bdrv_new(""); - if (s->qcow == NULL) { - DLOG(fprintf(stderr, "%s:%u: bdrv_new: Out of memory\n", __FUNCTION__, __LINE__)); + s->qcow = bdrv_new( "" ); + if ( s->qcow == NULL ) { + DLOG( fprintf( stderr, "%s:%u: bdrv_new: Out of memory\n", __FUNCTION__, __LINE__ ) ); #ifndef _WIN32 - unlink(s->qcow_filename); + unlink( s->qcow_filename ); #endif - return -1; + return -1; } - error = bdrv_open(s->qcow, s->qcow_filename, 0); - if (error < 0) { - DLOG(fprintf(stderr, "%s:%u: bdrv_open '%s': %d\n", __FUNCTION__, __LINE__, s->qcow_filename, error)); + error = bdrv_open( s->qcow, s->qcow_filename, 0 ); + if ( error < 0 ) { + DLOG( fprintf( stderr, "%s:%u: bdrv_open '%s': %d\n", __FUNCTION__, __LINE__, s->qcow_filename, error ) ); #ifndef _WIN32 - unlink(s->qcow_filename); + unlink( s->qcow_filename ); #endif - bdrv_delete(s->qcow); - return -1; + bdrv_delete( s->qcow ); + return -1; } - s->write_timer = qemu_new_timer(rt_clock, vvfat_write_timer, s); - if (NULL == s->write_timer) { - DLOG(fprintf(stderr, "%s:%u: write_timer: Out of memory\n", __FUNCTION__, __LINE__)); + s->write_timer = qemu_new_timer( rt_clock, vvfat_write_timer, s ); + if ( NULL == s->write_timer ) { + DLOG( fprintf( stderr, "%s:%u: write_timer: Out of memory\n", __FUNCTION__, __LINE__ ) ); #ifndef _WIN32 - unlink(s->qcow_filename); + unlink( s->qcow_filename ); #endif - bdrv_delete(s->qcow); - return -1; + bdrv_delete( s->qcow ); + return -1; } s->qcow->is_temporary = 1; #ifndef _WIN32 - unlink(s->qcow_filename); + unlink( s->qcow_filename ); #endif - DLOG(fprintf(stderr, "%s:%u: write enabled\n", __FUNCTION__, __LINE__)); + DLOG( fprintf( stderr, "%s:%u: write enabled\n", __FUNCTION__, __LINE__ ) ); return 0; } -static void vvfat_close(BlockDriverState *bs) +static void vvfat_close( BlockDriverState* bs ) { - BDRVVVFATState *s = bs->opaque; + BDRVVVFATState* s = bs->opaque; - if (qemu_timer_pending(s->write_timer)) { - qemu_del_timer(s->write_timer); - vvfat_write_timer(s); + if ( qemu_timer_pending( s->write_timer ) ) { + qemu_del_timer( s->write_timer ); + vvfat_write_timer( s ); } - vvfat_close_current_file(s); + vvfat_close_current_file( s ); - if (s->qcow) { - qemu_free(s->qcow_filename); + if ( s->qcow ) { + qemu_free( s->qcow_filename ); - bdrv_delete(s->qcow); - s->qcow = NULL; + bdrv_delete( s->qcow ); + s->qcow = NULL; } - array_free(&(s->fat)); - array_free(&(s->directory)); - array_free(&(s->mapping)); + array_free( &( s->fat ) ); + array_free( &( s->directory ) ); + array_free( &( s->mapping ) ); - if(s->cluster_buffer) - free(s->cluster_buffer); + if ( s->cluster_buffer ) + free( s->cluster_buffer ); } BlockDriver bdrv_vvfat = { "vvfat", - sizeof(BDRVVVFATState), + sizeof( BDRVVVFATState ), NULL, /* no probe for protocols */ vvfat_open, vvfat_read, @@ -3010,18 +2937,17 @@ BlockDriver bdrv_vvfat = { }; #ifdef DEBUG -static void -checkpoint(const char *where) +static void checkpoint( const char* where ) { - DLOG(fprintf(stderr, "%s:%u: checkpoint(%s)\n", __FUNCTION__, __LINE__, where)); + DLOG( fprintf( stderr, "%s:%u: checkpoint(%s)\n", __FUNCTION__, __LINE__, where ) ); - assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2); - DLOG(fprintf(stderr, "checkpoint(%s): call check1()\n", where)); - check1(vvv, where); - DLOG(fprintf(stderr, "checkpoint(%s): call check2()\n", where)); - check2(vvv, where); - assert(!vvv->current_mapping || vvv->current_fd || (vvv->current_mapping->mode & MODE_DIRECTORY)); -#if 0 + assert( ( ( mapping_t* )array_get( &( vvv->mapping ), 0 ) )->end == 2 ); + DLOG( fprintf( stderr, "checkpoint(%s): call check1()\n", where ) ); + check1( vvv, where ); + DLOG( fprintf( stderr, "checkpoint(%s): call check2()\n", where ) ); + check2( vvv, where ); + assert( !vvv->current_mapping || vvv->current_fd || ( vvv->current_mapping->mode & MODE_DIRECTORY ) ); +# if 0 if (((direntry_t*)vvv->directory.pointer)[1].attributes != 0xf) fprintf(stderr, "Nonono!\n"); mapping_t* mapping; @@ -3034,14 +2960,13 @@ checkpoint(const char *where) assert(mapping->dir_index < vvv->directory.next); direntry = array_get(&(vvv->directory), mapping->dir_index); assert(!memcmp(direntry->name, "USB H ", 11) || direntry->name[0]==0); -#endif - DLOG(fprintf(stderr, "checkpoint(%s): done\n", where)); +# endif + DLOG( fprintf( stderr, "checkpoint(%s): done\n", where ) ); return; /* avoid compiler warnings: */ - hexdump(NULL, 100); - remove_mapping(vvv, 0); - print_mapping(NULL); - print_direntry(NULL); + hexdump( NULL, 100 ); + remove_mapping( vvv, 0 ); + print_mapping( NULL ); + print_direntry( NULL ); } #endif - diff --git a/src/block.c b/src/block.c index d4b865d..ed7cd53 100644 --- a/src/block.c +++ b/src/block.c @@ -12,666 +12,617 @@ #include "block.h" #include "block_int.h" -#define SECTOR_BITS 9 -#define SECTOR_SIZE (1 << SECTOR_BITS) +#define SECTOR_BITS 9 +#define SECTOR_SIZE ( 1 << SECTOR_BITS ) -static BlockDriverState *bdrv_first; -static BlockDriver *first_drv; +static BlockDriverState* bdrv_first; +static BlockDriver* first_drv; #ifndef ENOMEDIUM -#define ENOMEDIUM ENODEV +# define ENOMEDIUM ENODEV #endif -static void bdrv_close(BlockDriverState *bs); +static void bdrv_close( BlockDriverState* bs ); -static int -path_is_absolute(const char *path) +static int path_is_absolute( const char* path ) { - const char *p; + const char* p; #ifdef _WIN32 - /* specific case for names like: "\\.\d:" */ - if (*path == '/' || *path == '\\') - return 1; + /* specific case for names like: "\\.\d:" */ + if ( *path == '/' || *path == '\\' ) + return 1; #endif - p = strchr(path, ':'); - if (p) - p++; - else - p = path; + p = strchr( path, ':' ); + if ( p ) + p++; + else + p = path; #ifdef _WIN32 - return (*p == '/' || *p == '\\'); + return ( *p == '/' || *p == '\\' ); #else - return (*p == '/'); + return ( *p == '/' ); #endif } /* if filename is absolute, just copy it to dest. Otherwise, build a path to it by considering it is relative to base_path. URL are supported. */ -static void -path_combine(char *dest, int dest_size, - const char *base_path, - const char *filename) +static void path_combine( char* dest, int dest_size, const char* base_path, const char* filename ) { - const char *p, *p1; - int len; + const char *p, *p1; + int len; - if (dest_size <= 0) - return; - if (path_is_absolute(filename)) { - pstrcpy(dest, dest_size, filename); - } else { - p = strchr(base_path, ':'); - if (p) - p++; - else - p = base_path; - p1 = strrchr(base_path, '/'); + if ( dest_size <= 0 ) + return; + if ( path_is_absolute( filename ) ) { + pstrcpy( dest, dest_size, filename ); + } else { + p = strchr( base_path, ':' ); + if ( p ) + p++; + else + p = base_path; + p1 = strrchr( base_path, '/' ); #ifdef _WIN32 - { - const char *p2; - p2 = strrchr(base_path, '\\'); - if (!p1 || p2 > p1) - p1 = p2; - } + { + const char* p2; + p2 = strrchr( base_path, '\\' ); + if ( !p1 || p2 > p1 ) + p1 = p2; + } #endif - if (p1) - p1++; - else - p1 = base_path; - if (p1 > p) - p = p1; - len = p - base_path; - if (len > dest_size - 1) - len = dest_size - 1; - memcpy(dest, base_path, len); - dest[len] = '\0'; - pstrcat(dest, dest_size, filename); - } + if ( p1 ) + p1++; + else + p1 = base_path; + if ( p1 > p ) + p = p1; + len = p - base_path; + if ( len > dest_size - 1 ) + len = dest_size - 1; + memcpy( dest, base_path, len ); + dest[ len ] = '\0'; + pstrcat( dest, dest_size, filename ); + } } #ifdef _WIN32 -void get_tmp_filename(char *filename, int size) +void get_tmp_filename( char* filename, int size ) { - char temp_dir[MAX_PATH]; + char temp_dir[ MAX_PATH ]; - GetTempPath(MAX_PATH, temp_dir); - GetTempFileName(temp_dir, "x49", 0, filename); + GetTempPath( MAX_PATH, temp_dir ); + GetTempFileName( temp_dir, "x49", 0, filename ); } #else -void get_tmp_filename(char *filename, int size) +void get_tmp_filename( char* filename, int size ) { - int fd; + int fd; - /* XXX: race condition possible */ - pstrcpy(filename, size, "/tmp/x49gp.XXXXXX"); - fd = mkstemp(filename); - close(fd); + /* XXX: race condition possible */ + pstrcpy( filename, size, "/tmp/x49gp.XXXXXX" ); + fd = mkstemp( filename ); + close( fd ); } #endif #ifdef _WIN32 -static int is_windows_drive_prefix(const char *filename) +static int is_windows_drive_prefix( const char* filename ) { - return (((filename[0] >= 'a' && filename[0] <= 'z') || - (filename[0] >= 'A' && filename[0] <= 'Z')) && - filename[1] == ':'); + return ( ( ( filename[ 0 ] >= 'a' && filename[ 0 ] <= 'z' ) || ( filename[ 0 ] >= 'A' && filename[ 0 ] <= 'Z' ) ) && + filename[ 1 ] == ':' ); } - -static int is_windows_drive(const char *filename) + +static int is_windows_drive( const char* filename ) { - if (is_windows_drive_prefix(filename) && - filename[2] == '\0') - return 1; - if (strstart(filename, "\\\\.\\", NULL) || - strstart(filename, "//./", NULL)) - return 1; - return 0; + if ( is_windows_drive_prefix( filename ) && filename[ 2 ] == '\0' ) + return 1; + if ( strstart( filename, "\\\\.\\", NULL ) || strstart( filename, "//./", NULL ) ) + return 1; + return 0; } #endif -static BlockDriver * -find_protocol(const char *filename) +static BlockDriver* find_protocol( const char* filename ) { - BlockDriver *drv1; - char protocol[128]; - int len; - const char *p; + BlockDriver* drv1; + char protocol[ 128 ]; + int len; + const char* p; #ifdef _WIN32 - if (is_windows_drive(filename) || - is_windows_drive_prefix(filename)) - return &bdrv_raw; + if ( is_windows_drive( filename ) || is_windows_drive_prefix( filename ) ) + return &bdrv_raw; #endif - p = strchr(filename, ':'); - if (!p) - return &bdrv_raw; - len = p - filename; - if (len > sizeof(protocol) - 1) - len = sizeof(protocol) - 1; - memcpy(protocol, filename, len); - protocol[len] = '\0'; - for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { + p = strchr( filename, ':' ); + if ( !p ) + return &bdrv_raw; + len = p - filename; + if ( len > sizeof( protocol ) - 1 ) + len = sizeof( protocol ) - 1; + memcpy( protocol, filename, len ); + protocol[ len ] = '\0'; + for ( drv1 = first_drv; drv1 != NULL; drv1 = drv1->next ) { #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, - "%s:%u: protocol '%s', drv->protocol_name '%s'\n", - __FUNCTION__, __LINE__, protocol, drv1->protocol_name); + fprintf( stderr, "%s:%u: protocol '%s', drv->protocol_name '%s'\n", __FUNCTION__, __LINE__, protocol, drv1->protocol_name ); #endif - if (drv1->protocol_name && - !strcmp(drv1->protocol_name, protocol)) { + if ( drv1->protocol_name && !strcmp( drv1->protocol_name, protocol ) ) { #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: protocol '%s', drv %p\n", - __FUNCTION__, __LINE__, protocol, drv1); + fprintf( stderr, "%s:%u: protocol '%s', drv %p\n", __FUNCTION__, __LINE__, protocol, drv1 ); #endif - return drv1; - } - } + return drv1; + } + } #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: protocol '%s', NULL\n", - __FUNCTION__, __LINE__, protocol); + fprintf( stderr, "%s:%u: protocol '%s', NULL\n", __FUNCTION__, __LINE__, protocol ); #endif - return NULL; + return NULL; } /* XXX: force raw format if block or character device ? It would simplify the BSD case */ -static BlockDriver * -find_image_format(const char *filename) +static BlockDriver* find_image_format( const char* filename ) { - int ret, score, score_max; - BlockDriver *drv1, *drv; - uint8_t buf[2048]; - BlockDriverState *bs; - - drv = find_protocol(filename); - /* no need to test disk image formats for vvfat */ - if (drv == &bdrv_vvfat) - return drv; + int ret, score, score_max; + BlockDriver *drv1, *drv; + uint8_t buf[ 2048 ]; + BlockDriverState* bs; - ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); - if (ret < 0) - return NULL; - ret = bdrv_pread(bs, 0, buf, sizeof(buf)); - bdrv_delete(bs); - if (ret < 0) { - return NULL; - } + drv = find_protocol( filename ); + /* no need to test disk image formats for vvfat */ + if ( drv == &bdrv_vvfat ) + return drv; - score_max = 0; - for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { - if (drv1->bdrv_probe) { - score = drv1->bdrv_probe(buf, ret, filename); - if (score > score_max) { - score_max = score; - drv = drv1; - } - } - } - return drv; + ret = bdrv_file_open( &bs, filename, BDRV_O_RDONLY ); + if ( ret < 0 ) + return NULL; + ret = bdrv_pread( bs, 0, buf, sizeof( buf ) ); + bdrv_delete( bs ); + if ( ret < 0 ) { + return NULL; + } + + score_max = 0; + for ( drv1 = first_drv; drv1 != NULL; drv1 = drv1->next ) { + if ( drv1->bdrv_probe ) { + score = drv1->bdrv_probe( buf, ret, filename ); + if ( score > score_max ) { + score_max = score; + drv = drv1; + } + } + } + return drv; } -int -bdrv_create(BlockDriver *drv, - const char *filename, int64_t size_in_sectors, - const char *backing_file, int flags) +int bdrv_create( BlockDriver* drv, const char* filename, int64_t size_in_sectors, const char* backing_file, int flags ) { - if (!drv->bdrv_create) - return -ENOTSUP; - return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); + if ( !drv->bdrv_create ) + return -ENOTSUP; + return drv->bdrv_create( filename, size_in_sectors, backing_file, flags ); } -static void -bdrv_register(BlockDriver *bdrv) +static void bdrv_register( BlockDriver* bdrv ) { - bdrv->next = first_drv; - first_drv = bdrv; + bdrv->next = first_drv; + first_drv = bdrv; } - + /* create a new block device (by default it is empty) */ -BlockDriverState * -bdrv_new(const char *device_name) +BlockDriverState* bdrv_new( const char* device_name ) { - BlockDriverState **pbs, *bs; + BlockDriverState **pbs, *bs; - bs = qemu_mallocz(sizeof(BlockDriverState)); - if(!bs) - return NULL; - pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); - if (device_name[0] != '\0') { - /* insert at the end */ - pbs = &bdrv_first; - while (*pbs != NULL) - pbs = &(*pbs)->next; - *pbs = bs; - } - return bs; + bs = qemu_mallocz( sizeof( BlockDriverState ) ); + if ( !bs ) + return NULL; + pstrcpy( bs->device_name, sizeof( bs->device_name ), device_name ); + if ( device_name[ 0 ] != '\0' ) { + /* insert at the end */ + pbs = &bdrv_first; + while ( *pbs != NULL ) + pbs = &( *pbs )->next; + *pbs = bs; + } + return bs; } -/** +/** * Truncate file to 'offset' bytes (needed only for file protocols) */ -int -bdrv_truncate(BlockDriverState *bs, int64_t offset) -{ - BlockDriver *drv = bs->drv; - if (!drv) - return -ENOMEDIUM; - if (!drv->bdrv_truncate) - return -ENOTSUP; - return drv->bdrv_truncate(bs, offset); +int bdrv_truncate( BlockDriverState* bs, int64_t offset ) +{ + BlockDriver* drv = bs->drv; + if ( !drv ) + return -ENOMEDIUM; + if ( !drv->bdrv_truncate ) + return -ENOTSUP; + return drv->bdrv_truncate( bs, offset ); } /** * Length of a file in bytes. Return < 0 if error or unknown. */ -int64_t -bdrv_getlength(BlockDriverState *bs) +int64_t bdrv_getlength( BlockDriverState* bs ) { - BlockDriver *drv = bs->drv; - if (!drv) - return -ENOMEDIUM; - if (!drv->bdrv_getlength) { - /* legacy mode */ - return bs->total_sectors * SECTOR_SIZE; - } - return drv->bdrv_getlength(bs); + BlockDriver* drv = bs->drv; + if ( !drv ) + return -ENOMEDIUM; + if ( !drv->bdrv_getlength ) { + /* legacy mode */ + return bs->total_sectors * SECTOR_SIZE; + } + return drv->bdrv_getlength( bs ); } -int -bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) +int bdrv_file_open( BlockDriverState** pbs, const char* filename, int flags ) { - BlockDriverState *bs; - int ret; + BlockDriverState* bs; + int ret; #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: filename '%s'\n", - __FUNCTION__, __LINE__, filename); + fprintf( stderr, "%s:%u: filename '%s'\n", __FUNCTION__, __LINE__, filename ); #endif - bs = bdrv_new(""); - if (!bs) - return -ENOMEM; - ret = bdrv_open(bs, filename, flags | BDRV_O_FILE); - if (ret < 0) { - bdrv_delete(bs); + bs = bdrv_new( "" ); + if ( !bs ) + return -ENOMEM; + ret = bdrv_open( bs, filename, flags | BDRV_O_FILE ); + if ( ret < 0 ) { + bdrv_delete( bs ); #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: '%s': %d\n", - __FUNCTION__, __LINE__, filename, ret); + fprintf( stderr, "%s:%u: '%s': %d\n", __FUNCTION__, __LINE__, filename, ret ); #endif - return ret; - } - *pbs = bs; + return ret; + } + *pbs = bs; #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: return 0\n", __FUNCTION__, __LINE__); + fprintf( stderr, "%s:%u: return 0\n", __FUNCTION__, __LINE__ ); #endif - return 0; + return 0; } -int -bdrv_open(BlockDriverState *bs, const char *filename, int flags) +int bdrv_open( BlockDriverState* bs, const char* filename, int flags ) { - int ret, open_flags; - char backing_filename[1024]; - BlockDriver *drv = NULL; + int ret, open_flags; + char backing_filename[ 1024 ]; + BlockDriver* drv = NULL; #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: filename '%s'\n", - __FUNCTION__, __LINE__, filename); + fprintf( stderr, "%s:%u: filename '%s'\n", __FUNCTION__, __LINE__, filename ); #endif - bs->read_only = 0; - bs->is_temporary = 0; - bs->encrypted = 0; + bs->read_only = 0; + bs->is_temporary = 0; + bs->encrypted = 0; - pstrcpy(bs->filename, sizeof(bs->filename), filename); - if (flags & BDRV_O_FILE) { - drv = find_protocol(filename); - if (!drv) { + pstrcpy( bs->filename, sizeof( bs->filename ), filename ); + if ( flags & BDRV_O_FILE ) { + drv = find_protocol( filename ); + if ( !drv ) { #ifdef DEBUG_X49GP_BLOCK -f printf(stderr, "%s:%u: drv: %p\n", - __FUNCTION__, __LINE__, drv); + f printf( stderr, "%s:%u: drv: %p\n", __FUNCTION__, __LINE__, drv ); #endif - return -ENOENT; - } - } else { - if (!drv) { - drv = find_image_format(filename); - if (!drv) { + return -ENOENT; + } + } else { + if ( !drv ) { + drv = find_image_format( filename ); + if ( !drv ) { #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: drv: %p\n", - __FUNCTION__, __LINE__, drv); + fprintf( stderr, "%s:%u: drv: %p\n", __FUNCTION__, __LINE__, drv ); #endif - return -1; - } - } - } + return -1; + } + } + } #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: drv: %p\n", __FUNCTION__, __LINE__, drv); + fprintf( stderr, "%s:%u: drv: %p\n", __FUNCTION__, __LINE__, drv ); #endif - bs->drv = drv; - bs->opaque = qemu_mallocz(drv->instance_size); - if (bs->opaque == NULL && drv->instance_size > 0) { + bs->drv = drv; + bs->opaque = qemu_mallocz( drv->instance_size ); + if ( bs->opaque == NULL && drv->instance_size > 0 ) { #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: no opaque\n", __FUNCTION__, __LINE__); + fprintf( stderr, "%s:%u: no opaque\n", __FUNCTION__, __LINE__ ); #endif - return -1; - } - /* Note: for compatibility, we open disk image files as RDWR, and - RDONLY as fallback */ - if (!(flags & BDRV_O_FILE)) - open_flags = BDRV_O_RDWR; - else - open_flags = flags & ~(BDRV_O_FILE); - ret = drv->bdrv_open(bs, filename, open_flags); + return -1; + } + /* Note: for compatibility, we open disk image files as RDWR, and + RDONLY as fallback */ + if ( !( flags & BDRV_O_FILE ) ) + open_flags = BDRV_O_RDWR; + else + open_flags = flags & ~( BDRV_O_FILE ); + ret = drv->bdrv_open( bs, filename, open_flags ); #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: drv->bdrv_open: %d\n", - __FUNCTION__, __LINE__, ret); + fprintf( stderr, "%s:%u: drv->bdrv_open: %d\n", __FUNCTION__, __LINE__, ret ); #endif - if (ret == -EACCES && !(flags & BDRV_O_FILE)) { - ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY); - bs->read_only = 1; - } - if (ret < 0) { - qemu_free(bs->opaque); - bs->opaque = NULL; - bs->drv = NULL; + if ( ret == -EACCES && !( flags & BDRV_O_FILE ) ) { + ret = drv->bdrv_open( bs, filename, BDRV_O_RDONLY ); + bs->read_only = 1; + } + if ( ret < 0 ) { + qemu_free( bs->opaque ); + bs->opaque = NULL; + bs->drv = NULL; #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: return %d\n", - __FUNCTION__, __LINE__, ret); + fprintf( stderr, "%s:%u: return %d\n", __FUNCTION__, __LINE__, ret ); #endif - return ret; - } - if (drv->bdrv_getlength) { - bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; - } + return ret; + } + if ( drv->bdrv_getlength ) { + bs->total_sectors = bdrv_getlength( bs ) >> SECTOR_BITS; + } #ifndef _WIN32 - if (bs->is_temporary) { - unlink(filename); - } + if ( bs->is_temporary ) { + unlink( filename ); + } #endif - if (bs->backing_file[0] != '\0') { - /* if there is a backing file, use it */ - bs->backing_hd = bdrv_new(""); - if (!bs->backing_hd) { - fail: - bdrv_close(bs); + if ( bs->backing_file[ 0 ] != '\0' ) { + /* if there is a backing file, use it */ + bs->backing_hd = bdrv_new( "" ); + if ( !bs->backing_hd ) { +fail: + bdrv_close( bs ); #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: return -ENOMEM\n", - __FUNCTION__, __LINE__); + fprintf( stderr, "%s:%u: return -ENOMEM\n", __FUNCTION__, __LINE__ ); #endif - return -ENOMEM; - } + return -ENOMEM; + } #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: combine '%s' '%s'\n", - __FUNCTION__, __LINE__, filename, bs->backing_file); + fprintf( stderr, "%s:%u: combine '%s' '%s'\n", __FUNCTION__, __LINE__, filename, bs->backing_file ); #endif - path_combine(backing_filename, sizeof(backing_filename), - filename, bs->backing_file); + path_combine( backing_filename, sizeof( backing_filename ), filename, bs->backing_file ); #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: combine: '%s'\n", - __FUNCTION__, __LINE__, backing_filename); + fprintf( stderr, "%s:%u: combine: '%s'\n", __FUNCTION__, __LINE__, backing_filename ); #endif - if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0) { + if ( bdrv_open( bs->backing_hd, backing_filename, 0 ) < 0 ) { #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: backing fail\n", - __FUNCTION__, __LINE__); + fprintf( stderr, "%s:%u: backing fail\n", __FUNCTION__, __LINE__ ); #endif - goto fail; - } - } + goto fail; + } + } - /* call the change callback */ - bs->media_changed = 1; - if (bs->change_cb) - bs->change_cb(bs->change_opaque); + /* call the change callback */ + bs->media_changed = 1; + if ( bs->change_cb ) + bs->change_cb( bs->change_opaque ); #ifdef DEBUG_X49GP_BLOCK - fprintf(stderr, "%s:%u: return 0\n", __FUNCTION__, __LINE__); + fprintf( stderr, "%s:%u: return 0\n", __FUNCTION__, __LINE__ ); #endif - return 0; + return 0; } -static void -bdrv_close(BlockDriverState *bs) +static void bdrv_close( BlockDriverState* bs ) { - if (NULL == bs->drv) - return; + if ( NULL == bs->drv ) + return; - /* call the change callback */ - bs->media_changed = 1; - if (bs->change_cb) - bs->change_cb(bs->change_opaque); + /* call the change callback */ + bs->media_changed = 1; + if ( bs->change_cb ) + bs->change_cb( bs->change_opaque ); - if (bs->backing_hd) - bdrv_delete(bs->backing_hd); + if ( bs->backing_hd ) + bdrv_delete( bs->backing_hd ); - bs->drv->bdrv_close(bs); + bs->drv->bdrv_close( bs ); #ifdef _WIN32 - if (bs->is_temporary) { - unlink(bs->filename); - } + if ( bs->is_temporary ) { + unlink( bs->filename ); + } #endif - qemu_free(bs->opaque); - bs->opaque = NULL; - bs->drv = NULL; + qemu_free( bs->opaque ); + bs->opaque = NULL; + bs->drv = NULL; } -void -bdrv_delete(BlockDriverState *bs) +void bdrv_delete( BlockDriverState* bs ) { - /* XXX: remove the driver list */ - bdrv_close(bs); - qemu_free(bs); + /* XXX: remove the driver list */ + bdrv_close( bs ); + qemu_free( bs ); } /* return < 0 if error. See bdrv_write() for the return codes */ -int -bdrv_read(BlockDriverState * bs, int64_t sector_num, - uint8_t * buf, int nb_sectors) +int bdrv_read( BlockDriverState* bs, int64_t sector_num, uint8_t* buf, int nb_sectors ) { - BlockDriver *drv = bs->drv; + BlockDriver* drv = bs->drv; - if (!drv) - return -ENOMEDIUM; + if ( !drv ) + return -ENOMEDIUM; - if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { - memcpy(buf, bs->boot_sector_data, 512); - sector_num++; - nb_sectors--; - buf += 512; - if (nb_sectors == 0) - return 0; - } - if (drv->bdrv_pread) { - int ret, len; + if ( sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0 ) { + memcpy( buf, bs->boot_sector_data, 512 ); + sector_num++; + nb_sectors--; + buf += 512; + if ( nb_sectors == 0 ) + return 0; + } + if ( drv->bdrv_pread ) { + int ret, len; - len = nb_sectors * 512; - ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); - if (ret < 0) - return ret; - else if (ret != len) - return -EINVAL; - else - return 0; - } else { - return drv->bdrv_read(bs, sector_num, buf, nb_sectors); - } + len = nb_sectors * 512; + ret = drv->bdrv_pread( bs, sector_num * 512, buf, len ); + if ( ret < 0 ) + return ret; + else if ( ret != len ) + return -EINVAL; + else + return 0; + } else { + return drv->bdrv_read( bs, sector_num, buf, nb_sectors ); + } } -/* Return < 0 if error. Important errors are: +/* Return < 0 if error. Important errors are: -EIO generic I/O error (may happen for all errors) -ENOMEDIUM No media inserted. -EINVAL Invalid sector number or nb_sectors -EACCES Trying to write a read-only device */ -static int -bdrv_write(BlockDriverState * bs, int64_t sector_num, - const uint8_t * buf, int nb_sectors) +static int bdrv_write( BlockDriverState* bs, int64_t sector_num, const uint8_t* buf, int nb_sectors ) { - BlockDriver *drv = bs->drv; + BlockDriver* drv = bs->drv; - if (!bs->drv) - return -ENOMEDIUM; - if (bs->read_only) - return -EACCES; - if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { - memcpy(bs->boot_sector_data, buf, 512); - } - if (drv->bdrv_pwrite) { - int ret, len; + if ( !bs->drv ) + return -ENOMEDIUM; + if ( bs->read_only ) + return -EACCES; + if ( sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0 ) { + memcpy( bs->boot_sector_data, buf, 512 ); + } + if ( drv->bdrv_pwrite ) { + int ret, len; - len = nb_sectors * 512; - ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len); - if (ret < 0) - return ret; - else if (ret != len) - return -EIO; - else - return 0; - } else { - return drv->bdrv_write(bs, sector_num, buf, nb_sectors); - } + len = nb_sectors * 512; + ret = drv->bdrv_pwrite( bs, sector_num * 512, buf, len ); + if ( ret < 0 ) + return ret; + else if ( ret != len ) + return -EIO; + else + return 0; + } else { + return drv->bdrv_write( bs, sector_num, buf, nb_sectors ); + } } -static int -bdrv_pread_em(BlockDriverState * bs, int64_t offset, uint8_t * buf, int count1) +static int bdrv_pread_em( BlockDriverState* bs, int64_t offset, uint8_t* buf, int count1 ) { - uint8_t tmp_buf[SECTOR_SIZE]; - int len, nb_sectors, count; - int64_t sector_num; + uint8_t tmp_buf[ SECTOR_SIZE ]; + int len, nb_sectors, count; + int64_t sector_num; - count = count1; - /* first read to align to sector start */ - len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); - if (len > count) - len = count; - sector_num = offset >> SECTOR_BITS; - if (len > 0) { - if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) - return -EIO; - memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); - count -= len; - if (count == 0) - return count1; - sector_num++; - buf += len; - } + count = count1; + /* first read to align to sector start */ + len = ( SECTOR_SIZE - offset ) & ( SECTOR_SIZE - 1 ); + if ( len > count ) + len = count; + sector_num = offset >> SECTOR_BITS; + if ( len > 0 ) { + if ( bdrv_read( bs, sector_num, tmp_buf, 1 ) < 0 ) + return -EIO; + memcpy( buf, tmp_buf + ( offset & ( SECTOR_SIZE - 1 ) ), len ); + count -= len; + if ( count == 0 ) + return count1; + sector_num++; + buf += len; + } - /* read the sectors "in place" */ - nb_sectors = count >> SECTOR_BITS; - if (nb_sectors > 0) { - if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) - return -EIO; - sector_num += nb_sectors; - len = nb_sectors << SECTOR_BITS; - buf += len; - count -= len; - } + /* read the sectors "in place" */ + nb_sectors = count >> SECTOR_BITS; + if ( nb_sectors > 0 ) { + if ( bdrv_read( bs, sector_num, buf, nb_sectors ) < 0 ) + return -EIO; + sector_num += nb_sectors; + len = nb_sectors << SECTOR_BITS; + buf += len; + count -= len; + } - /* add data from the last sector */ - if (count > 0) { - if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) - return -EIO; - memcpy(buf, tmp_buf, count); - } - return count1; + /* add data from the last sector */ + if ( count > 0 ) { + if ( bdrv_read( bs, sector_num, tmp_buf, 1 ) < 0 ) + return -EIO; + memcpy( buf, tmp_buf, count ); + } + return count1; } -static int -bdrv_pwrite_em(BlockDriverState * bs, int64_t offset, - const uint8_t * buf, int count1) +static int bdrv_pwrite_em( BlockDriverState* bs, int64_t offset, const uint8_t* buf, int count1 ) { - uint8_t tmp_buf[SECTOR_SIZE]; - int len, nb_sectors, count; - int64_t sector_num; + uint8_t tmp_buf[ SECTOR_SIZE ]; + int len, nb_sectors, count; + int64_t sector_num; - count = count1; - /* first write to align to sector start */ - len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); - if (len > count) - len = count; - sector_num = offset >> SECTOR_BITS; - if (len > 0) { - if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) - return -EIO; - memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); - if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) - return -EIO; - count -= len; - if (count == 0) - return count1; - sector_num++; - buf += len; - } + count = count1; + /* first write to align to sector start */ + len = ( SECTOR_SIZE - offset ) & ( SECTOR_SIZE - 1 ); + if ( len > count ) + len = count; + sector_num = offset >> SECTOR_BITS; + if ( len > 0 ) { + if ( bdrv_read( bs, sector_num, tmp_buf, 1 ) < 0 ) + return -EIO; + memcpy( tmp_buf + ( offset & ( SECTOR_SIZE - 1 ) ), buf, len ); + if ( bdrv_write( bs, sector_num, tmp_buf, 1 ) < 0 ) + return -EIO; + count -= len; + if ( count == 0 ) + return count1; + sector_num++; + buf += len; + } - /* write the sectors "in place" */ - nb_sectors = count >> SECTOR_BITS; - if (nb_sectors > 0) { - if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) - return -EIO; - sector_num += nb_sectors; - len = nb_sectors << SECTOR_BITS; - buf += len; - count -= len; - } + /* write the sectors "in place" */ + nb_sectors = count >> SECTOR_BITS; + if ( nb_sectors > 0 ) { + if ( bdrv_write( bs, sector_num, buf, nb_sectors ) < 0 ) + return -EIO; + sector_num += nb_sectors; + len = nb_sectors << SECTOR_BITS; + buf += len; + count -= len; + } - /* add data from the last sector */ - if (count > 0) { - if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) - return -EIO; - memcpy(tmp_buf, buf, count); - if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) - return -EIO; - } - return count1; + /* add data from the last sector */ + if ( count > 0 ) { + if ( bdrv_read( bs, sector_num, tmp_buf, 1 ) < 0 ) + return -EIO; + memcpy( tmp_buf, buf, count ); + if ( bdrv_write( bs, sector_num, tmp_buf, 1 ) < 0 ) + return -EIO; + } + return count1; } /** - * Read with byte offsets (needed only for file protocols) + * Read with byte offsets (needed only for file protocols) */ -int -bdrv_pread(BlockDriverState * bs, int64_t offset, void *buf1, int count1) +int bdrv_pread( BlockDriverState* bs, int64_t offset, void* buf1, int count1 ) { - BlockDriver *drv = bs->drv; + BlockDriver* drv = bs->drv; - if (!drv) - return -ENOMEDIUM; - if (!drv->bdrv_pread) - return bdrv_pread_em(bs, offset, buf1, count1); - return drv->bdrv_pread(bs, offset, buf1, count1); + if ( !drv ) + return -ENOMEDIUM; + if ( !drv->bdrv_pread ) + return bdrv_pread_em( bs, offset, buf1, count1 ); + return drv->bdrv_pread( bs, offset, buf1, count1 ); } -/** - * Write with byte offsets (needed only for file protocols) +/** + * Write with byte offsets (needed only for file protocols) */ -int -bdrv_pwrite(BlockDriverState * bs, int64_t offset, const void *buf1, int count1) +int bdrv_pwrite( BlockDriverState* bs, int64_t offset, const void* buf1, int count1 ) { - BlockDriver *drv = bs->drv; + BlockDriver* drv = bs->drv; - if (!drv) - return -ENOMEDIUM; - if (!drv->bdrv_pwrite) - return bdrv_pwrite_em(bs, offset, buf1, count1); - return drv->bdrv_pwrite(bs, offset, buf1, count1); + if ( !drv ) + return -ENOMEDIUM; + if ( !drv->bdrv_pwrite ) + return bdrv_pwrite_em( bs, offset, buf1, count1 ); + return drv->bdrv_pwrite( bs, offset, buf1, count1 ); } -void -bdrv_flush(BlockDriverState *bs) +void bdrv_flush( BlockDriverState* bs ) { - if (bs->drv->bdrv_flush) - bs->drv->bdrv_flush(bs); - if (bs->backing_hd) - bdrv_flush(bs->backing_hd); + if ( bs->drv->bdrv_flush ) + bs->drv->bdrv_flush( bs ); + if ( bs->backing_hd ) + bdrv_flush( bs->backing_hd ); } -void -bdrv_init(void) +void bdrv_init( void ) { - /* bdrv_register(&bdrv_raw); */ - /* bdrv_register(&bdrv_host_device); */ - bdrv_register(&bdrv_qcow); - bdrv_register(&bdrv_vvfat); + /* bdrv_register(&bdrv_raw); */ + /* bdrv_register(&bdrv_host_device); */ + bdrv_register( &bdrv_qcow ); + bdrv_register( &bdrv_vvfat ); } diff --git a/src/block.h b/src/block.h index 177d65d..7ad4b1e 100644 --- a/src/block.h +++ b/src/block.h @@ -6,65 +6,62 @@ #include -#define BDRV_O_RDONLY 0x0000 -#define BDRV_O_RDWR 0x0002 -#define BDRV_O_ACCESS 0x0003 -#define BDRV_O_CREAT 0x0004 /* create an empty file */ -#define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save - writes in a snapshot */ -#define BDRV_O_FILE 0x0010 /* open as a raw file (do not try to - use a disk image format on top of - it (default for - bdrv_file_open()) */ +#define BDRV_O_RDONLY 0x0000 +#define BDRV_O_RDWR 0x0002 +#define BDRV_O_ACCESS 0x0003 +#define BDRV_O_CREAT 0x0004 /* create an empty file */ +#define BDRV_O_SNAPSHOT \ + 0x0008 /* open the file read only and save \ + writes in a snapshot */ +#define BDRV_O_FILE \ + 0x0010 /* open as a raw file (do not try to \ + use a disk image format on top of \ + it (default for \ + bdrv_file_open()) */ typedef struct BlockDriver BlockDriver; typedef struct SnapshotInfo QEMUSnapshotInfo; typedef struct BlockDriverInfo BlockDriverInfo; typedef struct BlockDriverAIOCB BlockDriverAIOCB; -typedef void BlockDriverCompletionFunc(void *opaque, int ret); +typedef void BlockDriverCompletionFunc( void* opaque, int ret ); extern BlockDriver bdrv_raw; extern BlockDriver bdrv_host_device; extern BlockDriver bdrv_qcow; extern BlockDriver bdrv_vvfat; -void bdrv_init(void); -int bdrv_create(BlockDriver *drv, - const char *filename, int64_t size_in_sectors, - const char *backing_file, int flags); -BlockDriverState *bdrv_new(const char *device_name); -void bdrv_delete(BlockDriverState *bs); -int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags); -int bdrv_open(BlockDriverState *bs, const char *filename, int flags); +void bdrv_init( void ); +int bdrv_create( BlockDriver* drv, const char* filename, int64_t size_in_sectors, const char* backing_file, int flags ); +BlockDriverState* bdrv_new( const char* device_name ); +void bdrv_delete( BlockDriverState* bs ); +int bdrv_file_open( BlockDriverState** pbs, const char* filename, int flags ); +int bdrv_open( BlockDriverState* bs, const char* filename, int flags ); -int bdrv_read(BlockDriverState *bs, int64_t sector_num, - uint8_t *buf, int nb_sectors); -int bdrv_pread(BlockDriverState *bs, int64_t offset, - void *buf, int count); -int bdrv_pwrite(BlockDriverState *bs, int64_t offset, - const void *buf, int count); +int bdrv_read( BlockDriverState* bs, int64_t sector_num, uint8_t* buf, int nb_sectors ); +int bdrv_pread( BlockDriverState* bs, int64_t offset, void* buf, int count ); +int bdrv_pwrite( BlockDriverState* bs, int64_t offset, const void* buf, int count ); -int bdrv_truncate(BlockDriverState *bs, int64_t offset); -int64_t bdrv_getlength(BlockDriverState *bs); -void bdrv_flush(BlockDriverState *bs); +int bdrv_truncate( BlockDriverState* bs, int64_t offset ); +int64_t bdrv_getlength( BlockDriverState* bs ); +void bdrv_flush( BlockDriverState* bs ); /* timers */ typedef struct QEMUClock QEMUClock; -typedef void QEMUTimerCB(void *opaque); +typedef void QEMUTimerCB( void* opaque ); /* The real time clock should be used only for stuff which does not change the virtual machine state, as it is run even if the virtual machine is stopped. The real time clock has a frequency of 1000 Hz. */ -extern QEMUClock *rt_clock; +extern QEMUClock* rt_clock; -int64_t qemu_get_clock(QEMUClock *clock); +int64_t qemu_get_clock( QEMUClock* clock ); -QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque); -void qemu_del_timer(QEMUTimer *ts); -void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time); -int qemu_timer_pending(QEMUTimer *ts); +QEMUTimer* qemu_new_timer( QEMUClock* clock, QEMUTimerCB* cb, void* opaque ); +void qemu_del_timer( QEMUTimer* ts ); +void qemu_mod_timer( QEMUTimer* ts, int64_t expire_time ); +int qemu_timer_pending( QEMUTimer* ts ); extern int64_t ticks_per_sec; diff --git a/src/block_int.h b/src/block_int.h index abeec9c..8f7d3c1 100644 --- a/src/block_int.h +++ b/src/block_int.h @@ -24,84 +24,77 @@ #ifndef BLOCK_INT_H #define BLOCK_INT_H -# include "qemu-git/qemu-common.h" -# include "block.h" +#include "qemu-git/qemu-common.h" +#include "block.h" struct BlockDriver { - const char *format_name; + const char* format_name; int instance_size; - int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename); - int (*bdrv_open)(BlockDriverState *bs, const char *filename, int flags); - int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num, - uint8_t *buf, int nb_sectors); - int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num, - const uint8_t *buf, int nb_sectors); - void (*bdrv_close)(BlockDriverState *bs); - int (*bdrv_create)(const char *filename, int64_t total_sectors, - const char *backing_file, int flags); - void (*bdrv_flush)(BlockDriverState *bs); - int (*bdrv_is_allocated)(BlockDriverState *bs, int64_t sector_num, - int nb_sectors, int *pnum); - int (*bdrv_set_key)(BlockDriverState *bs, const char *key); - int (*bdrv_make_empty)(BlockDriverState *bs); + int ( *bdrv_probe )( const uint8_t* buf, int buf_size, const char* filename ); + int ( *bdrv_open )( BlockDriverState* bs, const char* filename, int flags ); + int ( *bdrv_read )( BlockDriverState* bs, int64_t sector_num, uint8_t* buf, int nb_sectors ); + int ( *bdrv_write )( BlockDriverState* bs, int64_t sector_num, const uint8_t* buf, int nb_sectors ); + void ( *bdrv_close )( BlockDriverState* bs ); + int ( *bdrv_create )( const char* filename, int64_t total_sectors, const char* backing_file, int flags ); + void ( *bdrv_flush )( BlockDriverState* bs ); + int ( *bdrv_is_allocated )( BlockDriverState* bs, int64_t sector_num, int nb_sectors, int* pnum ); + int ( *bdrv_set_key )( BlockDriverState* bs, const char* key ); + int ( *bdrv_make_empty )( BlockDriverState* bs ); - const char *protocol_name; - int (*bdrv_pread)(BlockDriverState *bs, int64_t offset, - uint8_t *buf, int count); - int (*bdrv_pwrite)(BlockDriverState *bs, int64_t offset, - const uint8_t *buf, int count); - int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset); - int64_t (*bdrv_getlength)(BlockDriverState *bs); - int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num, - const uint8_t *buf, int nb_sectors); + const char* protocol_name; + int ( *bdrv_pread )( BlockDriverState* bs, int64_t offset, uint8_t* buf, int count ); + int ( *bdrv_pwrite )( BlockDriverState* bs, int64_t offset, const uint8_t* buf, int count ); + int ( *bdrv_truncate )( BlockDriverState* bs, int64_t offset ); + int64_t ( *bdrv_getlength )( BlockDriverState* bs ); + int ( *bdrv_write_compressed )( BlockDriverState* bs, int64_t sector_num, const uint8_t* buf, int nb_sectors ); - int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi); + int ( *bdrv_get_info )( BlockDriverState* bs, BlockDriverInfo* bdi ); /* removable device specific */ - int (*bdrv_is_inserted)(BlockDriverState *bs); - int (*bdrv_media_changed)(BlockDriverState *bs); - int (*bdrv_eject)(BlockDriverState *bs, int eject_flag); - int (*bdrv_set_locked)(BlockDriverState *bs, int locked); + int ( *bdrv_is_inserted )( BlockDriverState* bs ); + int ( *bdrv_media_changed )( BlockDriverState* bs ); + int ( *bdrv_eject )( BlockDriverState* bs, int eject_flag ); + int ( *bdrv_set_locked )( BlockDriverState* bs, int locked ); - struct BlockDriver *next; + struct BlockDriver* next; }; struct BlockDriverState { int64_t total_sectors; /* if we are reading a disk image, give its size in sectors */ - int read_only; /* if true, the media is read only */ - int removable; /* if true, the media can be removed */ - int locked; /* if true, the media cannot temporarily be ejected */ - int encrypted; /* if true, the media is encrypted */ + int read_only; /* if true, the media is read only */ + int removable; /* if true, the media can be removed */ + int locked; /* if true, the media cannot temporarily be ejected */ + int encrypted; /* if true, the media is encrypted */ /* event callback when inserting/removing */ - void (*change_cb)(void *opaque); - void *change_opaque; + void ( *change_cb )( void* opaque ); + void* change_opaque; - BlockDriver *drv; /* NULL means no media */ - void *opaque; + BlockDriver* drv; /* NULL means no media */ + void* opaque; int boot_sector_enabled; - uint8_t boot_sector_data[512]; + uint8_t boot_sector_data[ 512 ]; - char filename[1024]; - char backing_file[1024]; /* if non zero, the image is a diff of - this file image */ + char filename[ 1024 ]; + char backing_file[ 1024 ]; /* if non zero, the image is a diff of + this file image */ int is_temporary; int media_changed; - BlockDriverState *backing_hd; + BlockDriverState* backing_hd; /* async read/write emulation */ - void *sync_aiocb; + void* sync_aiocb; /* NOTE: the following infos are only hints for real hardware drivers. They are not used by the block driver */ int cyls, heads, secs, translation; int type; - char device_name[32]; - BlockDriverState *next; + char device_name[ 32 ]; + BlockDriverState* next; }; -void get_tmp_filename(char *filename, int size); +void get_tmp_filename( char* filename, int size ); #endif /* BLOCK_INT_H */ diff --git a/src/byteorder.h b/src/byteorder.h index 72fb035..c6ed2e5 100644 --- a/src/byteorder.h +++ b/src/byteorder.h @@ -6,86 +6,75 @@ #include -static __inline__ uint16_t swab16(uint16_t x) -{ - return ((x & 0xff00) >> 8) | - ((x & 0x00ff) << 8); -} +static __inline__ uint16_t swab16( uint16_t x ) { return ( ( x & 0xff00 ) >> 8 ) | ( ( x & 0x00ff ) << 8 ); } -static __inline__ uint32_t swab32(uint32_t x) +static __inline__ uint32_t swab32( uint32_t x ) { - return ((x & 0xff000000) >> 24) | - ((x & 0x00ff0000) >> 8) | - ((x & 0x0000ff00) << 8) | - ((x & 0x000000ff) << 24); + return ( ( x & 0xff000000 ) >> 24 ) | ( ( x & 0x00ff0000 ) >> 8 ) | ( ( x & 0x0000ff00 ) << 8 ) | ( ( x & 0x000000ff ) << 24 ); } #ifdef __sparc__ -#define ASI_PL 0x88 /* Primary, implicit, little endian. */ +# define ASI_PL 0x88 /* Primary, implicit, little endian. */ -static __inline__ uint16_t __load_le16(const uint16_t *p) +static __inline__ uint16_t __load_le16( const uint16_t* p ) { - uint16_t x; + uint16_t x; - __asm__ __volatile__ ("lduha [%1] %2, %0" - : "=r" (x) - : "r" (p), "i" (ASI_PL)); - return x; + __asm__ __volatile__( "lduha [%1] %2, %0" : "=r"( x ) : "r"( p ), "i"( ASI_PL ) ); + return x; } -static __inline__ uint32_t __load_le32(const uint32_t *p) +static __inline__ uint32_t __load_le32( const uint32_t* p ) { - uint32_t x; + uint32_t x; - __asm__ __volatile__ ("lduwa [%1] %2, %0" - : "=r" (x) - : "r" (p), "i" (ASI_PL)); - return x; + __asm__ __volatile__( "lduwa [%1] %2, %0" : "=r"( x ) : "r"( p ), "i"( ASI_PL ) ); + return x; } -static __inline__ void __store_le16(uint16_t *p, uint16_t x) +static __inline__ void __store_le16( uint16_t* p, uint16_t x ) { - __asm__ __volatile__ ("stha %0, [%1] %2" - : /* no outputs */ - : "r" (x), "r" (p), "i" (ASI_PL)); + __asm__ __volatile__( "stha %0, [%1] %2" + : /* no outputs */ + : "r"( x ), "r"( p ), "i"( ASI_PL ) ); } -static __inline__ void __store_le32(uint32_t *p, uint32_t x) +static __inline__ void __store_le32( uint32_t* p, uint32_t x ) { - __asm__ __volatile__ ("stwa %0, [%1] %2" - : /* no outputs */ - : "r" (x), "r" (p), "i" (ASI_PL)); + __asm__ __volatile__( "stwa %0, [%1] %2" + : /* no outputs */ + : "r"( x ), "r"( p ), "i"( ASI_PL ) ); } #endif /* __sparc__ */ #if __BYTE_ORDER == __LITTLE_ENDIAN -#define le16_to_cpu(x) (x) -#define cpu_to_le16(x) (x) -#define le32_to_cpu(x) (x) -#define cpu_to_le32(x) (x) +# define le16_to_cpu( x ) ( x ) +# define cpu_to_le16( x ) ( x ) +# define le32_to_cpu( x ) ( x ) +# define cpu_to_le32( x ) ( x ) -#define load_le16(p) (*(p)) -#define store_le16(p, x) (*(p) = (x)) -#define load_le32(p) (*(p)) -#define store_le32(p, x) (*(p) = (x)) +# define load_le16( p ) ( *( p ) ) +# define store_le16( p, x ) ( *( p ) = ( x ) ) +# define load_le32( p ) ( *( p ) ) +# define store_le32( p, x ) ( *( p ) = ( x ) ) #elif __BYTE_ORDER == __BIG_ENDIAN -#define le16_to_cpu(x) swab16(x) -#define cpu_to_le16(x) swab16(x) -#define le32_to_cpu(x) swab32(x) -#define cpu_to_le32(x) swab32(x) +# define le16_to_cpu( x ) swab16( x ) +# define cpu_to_le16( x ) swab16( x ) +# define le32_to_cpu( x ) swab32( x ) +# define cpu_to_le32( x ) swab32( x ) -#define load_le16(p) __load_le16(p) -#define store_le16(p, x) __store_le16(p, x) -#define load_le32(p) __load_le32(p) -#define store_le32(p, x) __store_le32(p, x) +# define load_le16( p ) __load_le16( p ) +# define store_le16( p, x ) __store_le16( p, x ) +# define load_le32( p ) __load_le32( p ) +# define store_le32( p, x ) __store_le32( p, x ) #else -#error "Cannot determine host byteorder" +# error "Cannot determine host byteorder" #endif #endif /* !(_X49GP_BYTEORDER_H) */ diff --git a/src/flash.c b/src/flash.c index 2e3d766..ab8d850 100644 --- a/src/flash.c +++ b/src/flash.c @@ -19,820 +19,693 @@ #include "x49gp_ui.h" #include "byteorder.h" -#define FLASH_STATE_NORMAL 0 +#define FLASH_STATE_NORMAL 0 -#define FLASH_STATE_UNLOCK1 1 -#define FLASH_STATE_UNLOCK2 2 +#define FLASH_STATE_UNLOCK1 1 +#define FLASH_STATE_UNLOCK2 2 -#define FLASH_STATE_ERASE1 3 -#define FLASH_STATE_ERASE2 4 -#define FLASH_STATE_ERASE3 5 +#define FLASH_STATE_ERASE1 3 +#define FLASH_STATE_ERASE2 4 +#define FLASH_STATE_ERASE3 5 -#define FLASH_STATE_SOFTWARE_EXIT1 6 -#define FLASH_STATE_SOFTWARE_EXIT2 7 -#define FLASH_STATE_CFI_QUERY_EXIT1 8 -#define FLASH_STATE_CFI_QUERY_EXIT2 9 +#define FLASH_STATE_SOFTWARE_EXIT1 6 +#define FLASH_STATE_SOFTWARE_EXIT2 7 +#define FLASH_STATE_CFI_QUERY_EXIT1 8 +#define FLASH_STATE_CFI_QUERY_EXIT2 9 -#define FLASH_STATE_SOFTWARE_ID 10 -#define FLASH_STATE_CFI_QUERY 11 -#define FLASH_STATE_WORD_PROG 12 +#define FLASH_STATE_SOFTWARE_ID 10 +#define FLASH_STATE_CFI_QUERY 11 +#define FLASH_STATE_WORD_PROG 12 typedef struct { - void *data; - int state; - unsigned short vendor_ID; - unsigned short device_ID; - const unsigned short *cfi_data; - uint32_t cfi_size; - uint32_t sector_size; - uint32_t block_size; - char *filename; - int fd; - size_t size; + void* data; + int state; + unsigned short vendor_ID; + unsigned short device_ID; + const unsigned short* cfi_data; + uint32_t cfi_size; + uint32_t sector_size; + uint32_t block_size; + char* filename; + int fd; + size_t size; - uint32_t iotype; - uint32_t offset; + uint32_t iotype; + uint32_t offset; } x49gp_flash_t; -#define SST29VF160_VENDOR_ID 0x00bf -#define SST29VF160_DEVICE_ID 0x2782 +#define SST29VF160_VENDOR_ID 0x00bf +#define SST29VF160_DEVICE_ID 0x2782 -#define SST29VF160_SECTOR_SIZE 0x00001000 -#define SST29VF160_BLOCK_SIZE 0x00010000 -#define SST29VF160_SIZE 0x00200000 +#define SST29VF160_SECTOR_SIZE 0x00001000 +#define SST29VF160_BLOCK_SIZE 0x00010000 +#define SST29VF160_SIZE 0x00200000 -#define BOOT_SIZE 0x00004000 +#define BOOT_SIZE 0x00004000 -static const unsigned short sst29vf160_cfi_data[] = +static const unsigned short sst29vf160_cfi_data[] = { + [0x10] = 0x0051, [0x11] = 0x0052, [0x12] = 0x0059, [0x13] = 0x0001, [0x14] = 0x0007, [0x15] = 0x0000, [0x16] = 0x0000, + [0x17] = 0x0000, [0x18] = 0x0000, [0x19] = 0x0000, [0x1a] = 0x0000, + + [0x1b] = 0x0027, [0x1c] = 0x0036, [0x1d] = 0x0000, [0x1e] = 0x0000, [0x1f] = 0x0004, [0x20] = 0x0000, [0x21] = 0x0004, + [0x22] = 0x0006, [0x23] = 0x0001, [0x24] = 0x0000, [0x25] = 0x0001, [0x26] = 0x0001, + + [0x27] = 0x0015, [0x28] = 0x0001, [0x29] = 0x0000, [0x2a] = 0x0000, [0x2b] = 0x0000, [0x2c] = 0x0002, [0x2d] = 0x00ff, + [0x2e] = 0x0001, [0x2f] = 0x0010, [0x30] = 0x0000, [0x31] = 0x003f, [0x32] = 0x0000, [0x33] = 0x0000, [0x34] = 0x0001 }; +#define SST29VF160_CFI_SIZE ( sizeof( sst29vf160_cfi_data ) / sizeof( sst29vf160_cfi_data[ 0 ] ) ) + +static void flash_state_reset( x49gp_flash_t* flash ) { - [0x10] = 0x0051, - [0x11] = 0x0052, - [0x12] = 0x0059, - [0x13] = 0x0001, - [0x14] = 0x0007, - [0x15] = 0x0000, - [0x16] = 0x0000, - [0x17] = 0x0000, - [0x18] = 0x0000, - [0x19] = 0x0000, - [0x1a] = 0x0000, - - [0x1b] = 0x0027, - [0x1c] = 0x0036, - [0x1d] = 0x0000, - [0x1e] = 0x0000, - [0x1f] = 0x0004, - [0x20] = 0x0000, - [0x21] = 0x0004, - [0x22] = 0x0006, - [0x23] = 0x0001, - [0x24] = 0x0000, - [0x25] = 0x0001, - [0x26] = 0x0001, - - [0x27] = 0x0015, - [0x28] = 0x0001, - [0x29] = 0x0000, - [0x2a] = 0x0000, - [0x2b] = 0x0000, - [0x2c] = 0x0002, - [0x2d] = 0x00ff, - [0x2e] = 0x0001, - [0x2f] = 0x0010, - [0x30] = 0x0000, - [0x31] = 0x003f, - [0x32] = 0x0000, - [0x33] = 0x0000, - [0x34] = 0x0001 -}; -#define SST29VF160_CFI_SIZE (sizeof(sst29vf160_cfi_data) / sizeof(sst29vf160_cfi_data[0])) - -static void -flash_state_reset(x49gp_flash_t *flash) -{ - if (flash->state != FLASH_STATE_NORMAL) { - cpu_register_physical_memory(0x00000000, SST29VF160_SIZE, - flash->offset | flash->iotype | IO_MEM_ROMD); - flash->state = FLASH_STATE_NORMAL; - } + if ( flash->state != FLASH_STATE_NORMAL ) { + cpu_register_physical_memory( 0x00000000, SST29VF160_SIZE, flash->offset | flash->iotype | IO_MEM_ROMD ); + flash->state = FLASH_STATE_NORMAL; + } } -static uint32_t -flash_get_halfword(x49gp_flash_t *flash, uint32_t offset) +static uint32_t flash_get_halfword( x49gp_flash_t* flash, uint32_t offset ) { - uint8_t *datap = flash->data; - uint16_t data; + uint8_t* datap = flash->data; + uint16_t data; - switch (flash->state) { - default: - flash_state_reset(flash); - /* fall through */ + switch ( flash->state ) { + default: + flash_state_reset( flash ); + /* fall through */ - case FLASH_STATE_NORMAL: - data = lduw_p(datap + offset); - break; + case FLASH_STATE_NORMAL: + data = lduw_p( datap + offset ); + break; - case FLASH_STATE_SOFTWARE_ID: - if (offset & 2) { - data = flash->device_ID; - } else { - data = flash->vendor_ID; - } - break; + case FLASH_STATE_SOFTWARE_ID: + if ( offset & 2 ) { + data = flash->device_ID; + } else { + data = flash->vendor_ID; + } + break; - case FLASH_STATE_CFI_QUERY: - if ( (offset>>1) < flash->cfi_size) { - data = flash->cfi_data[offset >> 1]; - } else { - data = 0x0000; - } - break; - } + case FLASH_STATE_CFI_QUERY: + if ( ( offset >> 1 ) < flash->cfi_size ) { + data = flash->cfi_data[ offset >> 1 ]; + } else { + data = 0x0000; + } + break; + } - return data; + return data; } -static void -flash_put_halfword(x49gp_flash_t *flash, uint32_t offset, uint32_t data) +static void flash_put_halfword( x49gp_flash_t* flash, uint32_t offset, uint32_t data ) { - uint8_t *datap = flash->data; - uint16_t temp; + uint8_t* datap = flash->data; + uint16_t temp; - data &= 0xffff; + data &= 0xffff; - switch (flash->state) { - default: - flash_state_reset(flash); - /* fall through */ + switch ( flash->state ) { + default: + flash_state_reset( flash ); + /* fall through */ - case FLASH_STATE_NORMAL: - if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) { - flash->state = FLASH_STATE_UNLOCK1; - cpu_register_physical_memory(0x00000000, SST29VF160_SIZE, flash->iotype); - } - break; + case FLASH_STATE_NORMAL: + if ( ( ( offset >> 1 ) == 0x5555 ) && ( ( data & 0xff ) == 0xaa ) ) { + flash->state = FLASH_STATE_UNLOCK1; + cpu_register_physical_memory( 0x00000000, SST29VF160_SIZE, flash->iotype ); + } + break; - case FLASH_STATE_UNLOCK1: - if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) { - flash->state = FLASH_STATE_UNLOCK2; - } else { - flash_state_reset(flash); - } - break; + case FLASH_STATE_UNLOCK1: + if ( ( ( offset >> 1 ) == 0x2aaa ) && ( ( data & 0xff ) == 0x55 ) ) { + flash->state = FLASH_STATE_UNLOCK2; + } else { + flash_state_reset( flash ); + } + break; - case FLASH_STATE_UNLOCK2: - if ((offset >> 1) == 0x5555) { - switch (data & 0xff) { - case 0xa0: - flash->state = FLASH_STATE_WORD_PROG; - break; - case 0x80: - flash->state = FLASH_STATE_ERASE1; - break; - case 0x90: - flash->state = FLASH_STATE_SOFTWARE_ID; - break; - case 0x98: - flash->state = FLASH_STATE_CFI_QUERY; - break; - default: - flash_state_reset(flash); - break; - } - } else { - flash_state_reset(flash); - } - break; + case FLASH_STATE_UNLOCK2: + if ( ( offset >> 1 ) == 0x5555 ) { + switch ( data & 0xff ) { + case 0xa0: + flash->state = FLASH_STATE_WORD_PROG; + break; + case 0x80: + flash->state = FLASH_STATE_ERASE1; + break; + case 0x90: + flash->state = FLASH_STATE_SOFTWARE_ID; + break; + case 0x98: + flash->state = FLASH_STATE_CFI_QUERY; + break; + default: + flash_state_reset( flash ); + break; + } + } else { + flash_state_reset( flash ); + } + break; - case FLASH_STATE_ERASE1: - if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) { - flash->state = FLASH_STATE_ERASE2; - } else { - flash_state_reset(flash); - } - break; + case FLASH_STATE_ERASE1: + if ( ( ( offset >> 1 ) == 0x5555 ) && ( ( data & 0xff ) == 0xaa ) ) { + flash->state = FLASH_STATE_ERASE2; + } else { + flash_state_reset( flash ); + } + break; - case FLASH_STATE_ERASE2: - if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) { - flash->state = FLASH_STATE_ERASE3; - } else { - flash_state_reset(flash); - } - break; + case FLASH_STATE_ERASE2: + if ( ( ( offset >> 1 ) == 0x2aaa ) && ( ( data & 0xff ) == 0x55 ) ) { + flash->state = FLASH_STATE_ERASE3; + } else { + flash_state_reset( flash ); + } + break; - case FLASH_STATE_SOFTWARE_EXIT1: - if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) { - flash->state = FLASH_STATE_SOFTWARE_EXIT2; - } else { - flash->state = FLASH_STATE_SOFTWARE_ID; - } - break; + case FLASH_STATE_SOFTWARE_EXIT1: + if ( ( ( offset >> 1 ) == 0x2aaa ) && ( ( data & 0xff ) == 0x55 ) ) { + flash->state = FLASH_STATE_SOFTWARE_EXIT2; + } else { + flash->state = FLASH_STATE_SOFTWARE_ID; + } + break; - case FLASH_STATE_SOFTWARE_EXIT2: - if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xf0)) { - flash_state_reset(flash); - } else { - flash->state = FLASH_STATE_SOFTWARE_ID; - } - break; + case FLASH_STATE_SOFTWARE_EXIT2: + if ( ( ( offset >> 1 ) == 0x5555 ) && ( ( data & 0xff ) == 0xf0 ) ) { + flash_state_reset( flash ); + } else { + flash->state = FLASH_STATE_SOFTWARE_ID; + } + break; - case FLASH_STATE_CFI_QUERY_EXIT1: - if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) { - flash->state = FLASH_STATE_CFI_QUERY_EXIT2; - } else { - flash->state = FLASH_STATE_CFI_QUERY; - } - break; + case FLASH_STATE_CFI_QUERY_EXIT1: + if ( ( ( offset >> 1 ) == 0x2aaa ) && ( ( data & 0xff ) == 0x55 ) ) { + flash->state = FLASH_STATE_CFI_QUERY_EXIT2; + } else { + flash->state = FLASH_STATE_CFI_QUERY; + } + break; - case FLASH_STATE_CFI_QUERY_EXIT2: - if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xf0)) { - flash_state_reset(flash); - } else { - flash->state = FLASH_STATE_CFI_QUERY; - } - break; + case FLASH_STATE_CFI_QUERY_EXIT2: + if ( ( ( offset >> 1 ) == 0x5555 ) && ( ( data & 0xff ) == 0xf0 ) ) { + flash_state_reset( flash ); + } else { + flash->state = FLASH_STATE_CFI_QUERY; + } + break; - case FLASH_STATE_SOFTWARE_ID: - if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) { - flash->state = FLASH_STATE_SOFTWARE_EXIT1; - } else if ((data & 0xff) == 0xf0) { - flash_state_reset(flash); - } - break; + case FLASH_STATE_SOFTWARE_ID: + if ( ( ( offset >> 1 ) == 0x5555 ) && ( ( data & 0xff ) == 0xaa ) ) { + flash->state = FLASH_STATE_SOFTWARE_EXIT1; + } else if ( ( data & 0xff ) == 0xf0 ) { + flash_state_reset( flash ); + } + break; - case FLASH_STATE_CFI_QUERY: - if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) { - flash->state = FLASH_STATE_CFI_QUERY_EXIT1; - } else if ((data & 0xff) == 0xf0) { - flash_state_reset(flash); - } - break; + case FLASH_STATE_CFI_QUERY: + if ( ( ( offset >> 1 ) == 0x5555 ) && ( ( data & 0xff ) == 0xaa ) ) { + flash->state = FLASH_STATE_CFI_QUERY_EXIT1; + } else if ( ( data & 0xff ) == 0xf0 ) { + flash_state_reset( flash ); + } + break; - case FLASH_STATE_WORD_PROG: - temp = lduw_p(datap + offset); - stw_p(datap + offset, data & temp); + case FLASH_STATE_WORD_PROG: + temp = lduw_p( datap + offset ); + stw_p( datap + offset, data & temp ); #ifdef DEBUG_X49GP_FLASH_WRITE - printf("write FLASH 2 (state %u) at offset %08x: %04x, result: %04x\n", - flash->state, offset, data, lduw_p(datap + offset)); + printf( "write FLASH 2 (state %u) at offset %08x: %04x, result: %04x\n", flash->state, offset, data, lduw_p( datap + offset ) ); #endif - flash_state_reset(flash); - break; + flash_state_reset( flash ); + break; - case FLASH_STATE_ERASE3: - switch (data & 0xff) { - case 0x10: /* Chip Erase */ + case FLASH_STATE_ERASE3: + switch ( data & 0xff ) { + case 0x10: /* Chip Erase */ #ifdef DEBUG_X49GP_FLASH_WRITE - printf("erase FLASH %08x %08x\n", 0, SST29VF160_SIZE); + printf( "erase FLASH %08x %08x\n", 0, SST29VF160_SIZE ); #endif - memset(datap, 0xff, SST29VF160_SIZE); - break; + memset( datap, 0xff, SST29VF160_SIZE ); + break; - case 0x30: /* Sector Erase */ + case 0x30: /* Sector Erase */ #ifdef DEBUG_X49GP_FLASH_WRITE - printf("erase FLASH %08x %08x\n", - (offset & ~(flash->sector_size - 1)), - flash->sector_size); + printf( "erase FLASH %08x %08x\n", ( offset & ~( flash->sector_size - 1 ) ), flash->sector_size ); #endif - memset(datap + (offset & ~(flash->sector_size - 1)), 0xff, flash->sector_size); + memset( datap + ( offset & ~( flash->sector_size - 1 ) ), 0xff, flash->sector_size ); #ifdef DEBUG_X49GP_FLASH_WRITE - printf("erase FLASH %08x: %04x, %08x: %04x, %08x: %04x\n", - offset, lduw_p(datap + offset), - offset + 0x800, lduw_p(datap + offset + 0x800), - offset + 0xffc, lduw_p(datap + offset + 0xffc)); + printf( "erase FLASH %08x: %04x, %08x: %04x, %08x: %04x\n", offset, lduw_p( datap + offset ), offset + 0x800, + lduw_p( datap + offset + 0x800 ), offset + 0xffc, lduw_p( datap + offset + 0xffc ) ); #endif - break; + break; - case 0x50: /* Block Erase */ + case 0x50: /* Block Erase */ #ifdef DEBUG_X49GP_FLASH_WRITE - printf("erase FLASH %08x %08x\n", - (offset & ~(flash->block_size - 1)), - flash->block_size); + printf( "erase FLASH %08x %08x\n", ( offset & ~( flash->block_size - 1 ) ), flash->block_size ); #endif - memset(datap + (offset & ~(flash->block_size - 1)), 0xff, flash->block_size); - break; - default: - break; - } + memset( datap + ( offset & ~( flash->block_size - 1 ) ), 0xff, flash->block_size ); + break; + default: + break; + } - flash_state_reset(flash); - break; - } + flash_state_reset( flash ); + break; + } } -static uint32_t -flash_readb(void *opaque, target_phys_addr_t offset) +static uint32_t flash_readb( void* opaque, target_phys_addr_t offset ) { - x49gp_flash_t *flash = opaque; - uint8_t *datap = flash->data; - unsigned short temp; - uint32_t shift; - unsigned char data; + x49gp_flash_t* flash = opaque; + uint8_t* datap = flash->data; + unsigned short temp; + uint32_t shift; + unsigned char data; - if (flash->state == FLASH_STATE_NORMAL) { - data = *(datap + offset); - } else { - temp = flash_get_halfword(flash, offset & ~(1)); - shift = (offset & 1) << 3; - data = (temp >> shift) & 0xff; - } + if ( flash->state == FLASH_STATE_NORMAL ) { + data = *( datap + offset ); + } else { + temp = flash_get_halfword( flash, offset & ~( 1 ) ); + shift = ( offset & 1 ) << 3; + data = ( temp >> shift ) & 0xff; + } #ifdef DEBUG_X49GP_FLASH_READ - printf("read FLASH 1 (state %u) at offset %08lx: %02x\n", - flash->state, (unsigned long) offset, data); + printf( "read FLASH 1 (state %u) at offset %08lx: %02x\n", flash->state, ( unsigned long )offset, data ); #endif - return data; + return data; } -static uint32_t -flash_readw(void *opaque, target_phys_addr_t offset) +static uint32_t flash_readw( void* opaque, target_phys_addr_t offset ) { - x49gp_flash_t *flash = opaque; - uint8_t *datap = flash->data; - uint32_t data; + x49gp_flash_t* flash = opaque; + uint8_t* datap = flash->data; + uint32_t data; - if (flash->state == FLASH_STATE_NORMAL) { - data = lduw_p(datap + offset); - } else { - data = flash_get_halfword(flash, offset); - } + if ( flash->state == FLASH_STATE_NORMAL ) { + data = lduw_p( datap + offset ); + } else { + data = flash_get_halfword( flash, offset ); + } #ifdef DEBUG_X49GP_FLASH_READ - printf("read FLASH 2 (state %u) at offset %08lx: %04x\n", - flash->state, (unsigned long) offset, data); + printf( "read FLASH 2 (state %u) at offset %08lx: %04x\n", flash->state, ( unsigned long )offset, data ); #endif - return data; + return data; } -static uint32_t -flash_readl(void *opaque, target_phys_addr_t offset) +static uint32_t flash_readl( void* opaque, target_phys_addr_t offset ) { - x49gp_flash_t *flash = opaque; - uint8_t *datap = flash->data; - uint32_t data; + x49gp_flash_t* flash = opaque; + uint8_t* datap = flash->data; + uint32_t data; - if (flash->state == FLASH_STATE_NORMAL) { - data = ldl_p(datap + offset); - } else { - data = (flash_get_halfword(flash, offset + 2) << 16) | - (flash_get_halfword(flash, offset + 0) << 0); - } + if ( flash->state == FLASH_STATE_NORMAL ) { + data = ldl_p( datap + offset ); + } else { + data = ( flash_get_halfword( flash, offset + 2 ) << 16 ) | ( flash_get_halfword( flash, offset + 0 ) << 0 ); + } #ifdef DEBUG_X49GP_FLASH_READ - printf("read FLASH 4 (state %u) at offset %08lx: %08x\n", - flash->state, (unsigned long) offset, data); + printf( "read FLASH 4 (state %u) at offset %08lx: %08x\n", flash->state, ( unsigned long )offset, data ); #endif - return data; + return data; } -static void -flash_writeb(void *opaque, target_phys_addr_t offset, uint32_t data) +static void flash_writeb( void* opaque, target_phys_addr_t offset, uint32_t data ) { - x49gp_flash_t *flash = opaque; - uint32_t shift; + x49gp_flash_t* flash = opaque; + uint32_t shift; - data &= 0xff; + data &= 0xff; #ifdef DEBUG_X49GP_FLASH_WRITE - printf("write FLASH 1 (state %u) at offset %08lx: %02x\n", - flash->state, offset, data); + printf( "write FLASH 1 (state %u) at offset %08lx: %02x\n", flash->state, offset, data ); #endif - /* - * This does not issue read-modify-write, i.e. you will get - * broken data in FLASH memory. This would be the case with - * real hardware, too. - */ - shift = (offset & 1) << 3; - flash_put_halfword(flash, offset & ~(1), data << shift); + /* + * This does not issue read-modify-write, i.e. you will get + * broken data in FLASH memory. This would be the case with + * real hardware, too. + */ + shift = ( offset & 1 ) << 3; + flash_put_halfword( flash, offset & ~( 1 ), data << shift ); } -static void -flash_writew(void *opaque, target_phys_addr_t offset, uint32_t data) +static void flash_writew( void* opaque, target_phys_addr_t offset, uint32_t data ) { - x49gp_flash_t *flash = opaque; + x49gp_flash_t* flash = opaque; - data &= 0xffff; + data &= 0xffff; #ifdef DEBUG_X49GP_FLASH_WRITE - printf("write FLASH 2 (state %u) at offset %08lx: %04x\n", - flash->state, offset, data); + printf( "write FLASH 2 (state %u) at offset %08lx: %04x\n", flash->state, offset, data ); #endif - flash_put_halfword(flash, offset, data); + flash_put_halfword( flash, offset, data ); } -static void -flash_writel(void *opaque, target_phys_addr_t offset, uint32_t data) +static void flash_writel( void* opaque, target_phys_addr_t offset, uint32_t data ) { - x49gp_flash_t *flash = opaque; + x49gp_flash_t* flash = opaque; #ifdef DEBUG_X49GP_FLASH_WRITE - printf("write FLASH 4 (state %u) at offset %08lx: %08x\n", - flash->state, offset, data); + printf( "write FLASH 4 (state %u) at offset %08lx: %08x\n", flash->state, offset, data ); #endif - flash_put_halfword(flash, offset + 2, (data >> 16) & 0xffff); - flash_put_halfword(flash, offset + 0, (data >> 0) & 0xffff); + flash_put_halfword( flash, offset + 2, ( data >> 16 ) & 0xffff ); + flash_put_halfword( flash, offset + 0, ( data >> 0 ) & 0xffff ); } -static int -flash_load(x49gp_module_t *module, GKeyFile *key) +static int flash_load( x49gp_module_t* module, GKeyFile* key ) { - x49gp_flash_t *flash = module->user_data; - x49gp_t *x49gp = module->x49gp; - x49gp_ui_t *ui = x49gp->ui; - int calc = ui->calculator; - char *filename; - struct stat st; - char *bootfile; - int bootfd, fwfd; - int error; - int i; - char bank_marker[5] = {0xf0, 0x02, 0x00, 0x00, 0x00}; - int bytes_read; + x49gp_flash_t* flash = module->user_data; + x49gp_t* x49gp = module->x49gp; + x49gp_ui_t* ui = x49gp->ui; + int calc = ui->calculator; + char* filename; + struct stat st; + char* bootfile; + int bootfd, fwfd; + int error; + int i; + char bank_marker[ 5 ] = { 0xf0, 0x02, 0x00, 0x00, 0x00 }; + int bytes_read; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - error = x49gp_module_get_filename(module, key, "filename", "flash", - &(flash->filename), &filename); + error = x49gp_module_get_filename( module, key, "filename", "flash", &( flash->filename ), &filename ); - flash->fd = open(filename, O_RDWR | O_CREAT, 0644); - if (flash->fd < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: open %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - return error; - } + flash->fd = open( filename, O_RDWR | O_CREAT, 0644 ); + if ( flash->fd < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: open %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + return error; + } - flash->size = SST29VF160_SIZE; - if (fstat(flash->fd, &st) < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: fstat %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(flash->fd); - flash->fd = -1; - return error; - } + flash->size = SST29VF160_SIZE; + if ( fstat( flash->fd, &st ) < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: fstat %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( flash->fd ); + flash->fd = -1; + return error; + } - if (ftruncate(flash->fd, flash->size) < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: ftruncate %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(flash->fd); - flash->fd = -1; - return error; - } + if ( ftruncate( flash->fd, flash->size ) < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: ftruncate %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( flash->fd ); + flash->fd = -1; + return error; + } - flash->data = mmap(phys_ram_base + flash->offset, flash->size, - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, - flash->fd, 0); - if (flash->data == (void *) -1) { - error = -errno; - fprintf(stderr, "%s: %s:%u: mmap %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(flash->fd); - flash->fd = -1; - return error; - } + flash->data = mmap( phys_ram_base + flash->offset, flash->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, flash->fd, 0 ); + if ( flash->data == ( void* )-1 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: mmap %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( flash->fd ); + flash->fd = -1; + return error; + } + if ( flash->size > st.st_size ) { + fprintf( stderr, "Flash too small, rebuilding\n" ); + x49gp->startup_reinit = X49GP_REINIT_FLASH_FULL; + } + if ( x49gp->startup_reinit >= X49GP_REINIT_FLASH ) { - if (flash->size > st.st_size) { - fprintf(stderr, "Flash too small, rebuilding\n"); - x49gp->startup_reinit = X49GP_REINIT_FLASH_FULL; - } - if (x49gp->startup_reinit >= X49GP_REINIT_FLASH) { + if ( x49gp->startup_reinit == X49GP_REINIT_FLASH_FULL ) + memset( phys_ram_base + flash->offset, 0xff, flash->size - st.st_size ); - if (x49gp->startup_reinit == X49GP_REINIT_FLASH_FULL) - memset(phys_ram_base + flash->offset, 0xff, - flash->size - st.st_size); + bootfd = x49gp_module_open_rodata( module, + calc == UI_CALCULATOR_HP49GP || calc == UI_CALCULATOR_HP49GP_NEWRPL ? "firmware/boot-49g+.bin" + : "firmware/boot-50g.bin", + &bootfile ); - bootfd = x49gp_module_open_rodata(module, - calc == UI_CALCULATOR_HP49GP || - calc == UI_CALCULATOR_HP49GP_NEWRPL ? - "firmware/boot-49g+.bin" : - "firmware/boot-50g.bin", - &bootfile); + if ( bootfd < 0 ) { + g_free( filename ); + close( flash->fd ); + flash->fd = -1; + return bootfd; + } - if (bootfd < 0) { - g_free(filename); - close(flash->fd); - flash->fd = -1; - return bootfd; - } + if ( read( bootfd, phys_ram_base + flash->offset, BOOT_SIZE ) < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: read %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + g_free( bootfile ); + close( bootfd ); + close( flash->fd ); + flash->fd = -1; + return error; + } - if (read(bootfd, phys_ram_base + flash->offset, - BOOT_SIZE) < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: read %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - g_free(bootfile); - close(bootfd); - close(flash->fd); - flash->fd = -1; - return error; - } + g_free( filename ); + close( bootfd ); + g_free( bootfile ); - g_free(filename); - close(bootfd); - g_free(bootfile); - - if (x49gp->startup_reinit == X49GP_REINIT_FLASH_FULL) { - /* The stock firmware expects special markers in certain - spots across the flash. Without these, the user banks - act up and are not usable, and PINIT apparently won't - fix it. Let's help it out; custom firmware will have - to deal with remnants of the user banks on real - calculators anyway, so if they break here, they will - too on actual hardware because that always comes with - the stock firmware and its user banks marked - properly. */ - for (i=2;i<14;i++) { - bank_marker[1] = i; - memcpy(phys_ram_base + flash->offset + 0x40100 + - 0x20000 * i, bank_marker, 5); - } - } + if ( x49gp->startup_reinit == X49GP_REINIT_FLASH_FULL ) { + /* The stock firmware expects special markers in certain + spots across the flash. Without these, the user banks + act up and are not usable, and PINIT apparently won't + fix it. Let's help it out; custom firmware will have + to deal with remnants of the user banks on real + calculators anyway, so if they break here, they will + too on actual hardware because that always comes with + the stock firmware and its user banks marked + properly. */ + for ( i = 2; i < 14; i++ ) { + bank_marker[ 1 ] = i; + memcpy( phys_ram_base + flash->offset + 0x40100 + 0x20000 * i, bank_marker, 5 ); + } + } retry: - filename = NULL; - if (x49gp->firmware != NULL) { - filename = g_strdup(x49gp->firmware); - } else { - x49gp_ui_open_firmware(x49gp, &filename); - } - if (filename != NULL) { - fwfd = open(filename, O_RDONLY); - if (fwfd < 0) { - fprintf(stderr, "%s: %s:%u: open %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - /* Mark firmware as invalid if there is one */ - memset(phys_ram_base + flash->offset + - BOOT_SIZE, 0, 16); - if (x49gp->firmware != NULL) { - fprintf(stderr, "Warning: Could not " - "open selected firmware, " - "falling back to bootloader " - "recovery tools\n"); - } else { - x49gp_ui_show_error(x49gp, - "Could not open " - "selected " - "firmware!"); - goto retry; - } - } else { - bytes_read = read(fwfd, phys_ram_base + - flash->offset + BOOT_SIZE, - 16); - if (bytes_read < 0) { - fprintf(stderr, "%s: %s:%u: read %s: %s\n", - module->name, __FUNCTION__, - __LINE__, filename, - strerror(errno)); - /* Mark firmware as invalid - if there is one */ - memset(phys_ram_base + flash->offset + - BOOT_SIZE, 0, 16); - if (x49gp->firmware != NULL) { - fprintf(stderr, "Warning: " - "Could not read " - "selected firmware, " - "falling back to " - "bootloader recovery " - "tools\n"); - } else { - x49gp_ui_show_error(x49gp, - "Could not " - "read " - "selected " - "firmware!"); - goto retry; - } - } else if (bytes_read < 16 || - memcmp(phys_ram_base + - flash->offset + BOOT_SIZE, - "KINPOUPDATEIMAGE", 16) - != 0) { - /* Mark firmware as invalid */ - memset(phys_ram_base + flash->offset + - BOOT_SIZE, 0, 16); - if (x49gp->firmware != NULL) { - fprintf(stderr, "Warning: " - "Firmware is invalid, " - "falling back to " - "bootloader recovery " - "tools\n"); - } else { - x49gp_ui_show_error(x49gp, - "Selected " - "firmware " - "is " - "invalid!"); - goto retry; - } - /* The firmware may be shorter than - SST29VF160_SIZE - BOOT_SIZE, but if so, - read will just give us what it sees. - The space after that will remain empty. */ - } else if (read(fwfd, phys_ram_base + - flash->offset + BOOT_SIZE + 16, - SST29VF160_SIZE - - (BOOT_SIZE + 16)) - < 0) { - fprintf(stderr, "%s: %s:%u: read %s: %s\n", - module->name, __FUNCTION__, - __LINE__, filename, - strerror(errno)); - /* Mark firmware as invalid - if there is one */ - memset(phys_ram_base + flash->offset + - BOOT_SIZE, 0, 16); - if (x49gp->firmware != NULL) { - fprintf(stderr, "Warning: " - "Could not read " - "selected firmware, " - "falling back to " - "bootloader recovery " - "tools\n"); - } else { - x49gp_ui_show_error(x49gp, - "Could not " - "read " - "selected " - "firmware!"); - goto retry; - } - } else { - /* Mark firmware as valid in the same - way the bootloader does */ - memcpy(phys_ram_base + flash->offset + - BOOT_SIZE, "Kinposhcopyright", - 16); - } - close(fwfd); - } - g_free(filename); - } - } else { - g_free(filename); - } + filename = NULL; + if ( x49gp->firmware != NULL ) { + filename = g_strdup( x49gp->firmware ); + } else { + x49gp_ui_open_firmware( x49gp, &filename ); + } + if ( filename != NULL ) { + fwfd = open( filename, O_RDONLY ); + if ( fwfd < 0 ) { + fprintf( stderr, "%s: %s:%u: open %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + /* Mark firmware as invalid if there is one */ + memset( phys_ram_base + flash->offset + BOOT_SIZE, 0, 16 ); + if ( x49gp->firmware != NULL ) { + fprintf( stderr, "Warning: Could not " + "open selected firmware, " + "falling back to bootloader " + "recovery tools\n" ); + } else { + x49gp_ui_show_error( x49gp, "Could not open " + "selected " + "firmware!" ); + goto retry; + } + } else { + bytes_read = read( fwfd, phys_ram_base + flash->offset + BOOT_SIZE, 16 ); + if ( bytes_read < 0 ) { + fprintf( stderr, "%s: %s:%u: read %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + /* Mark firmware as invalid + if there is one */ + memset( phys_ram_base + flash->offset + BOOT_SIZE, 0, 16 ); + if ( x49gp->firmware != NULL ) { + fprintf( stderr, "Warning: " + "Could not read " + "selected firmware, " + "falling back to " + "bootloader recovery " + "tools\n" ); + } else { + x49gp_ui_show_error( x49gp, "Could not " + "read " + "selected " + "firmware!" ); + goto retry; + } + } else if ( bytes_read < 16 || memcmp( phys_ram_base + flash->offset + BOOT_SIZE, "KINPOUPDATEIMAGE", 16 ) != 0 ) { + /* Mark firmware as invalid */ + memset( phys_ram_base + flash->offset + BOOT_SIZE, 0, 16 ); + if ( x49gp->firmware != NULL ) { + fprintf( stderr, "Warning: " + "Firmware is invalid, " + "falling back to " + "bootloader recovery " + "tools\n" ); + } else { + x49gp_ui_show_error( x49gp, "Selected " + "firmware " + "is " + "invalid!" ); + goto retry; + } + /* The firmware may be shorter than + SST29VF160_SIZE - BOOT_SIZE, but if so, + read will just give us what it sees. + The space after that will remain empty. */ + } else if ( read( fwfd, phys_ram_base + flash->offset + BOOT_SIZE + 16, SST29VF160_SIZE - ( BOOT_SIZE + 16 ) ) < 0 ) { + fprintf( stderr, "%s: %s:%u: read %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + /* Mark firmware as invalid + if there is one */ + memset( phys_ram_base + flash->offset + BOOT_SIZE, 0, 16 ); + if ( x49gp->firmware != NULL ) { + fprintf( stderr, "Warning: " + "Could not read " + "selected firmware, " + "falling back to " + "bootloader recovery " + "tools\n" ); + } else { + x49gp_ui_show_error( x49gp, "Could not " + "read " + "selected " + "firmware!" ); + goto retry; + } + } else { + /* Mark firmware as valid in the same + way the bootloader does */ + memcpy( phys_ram_base + flash->offset + BOOT_SIZE, "Kinposhcopyright", 16 ); + } + close( fwfd ); + } + g_free( filename ); + } + } else { + g_free( filename ); + } - return error; + return error; } -static int -flash_save(x49gp_module_t *module, GKeyFile *key) +static int flash_save( x49gp_module_t* module, GKeyFile* key ) { - x49gp_flash_t *flash = module->user_data; - int error; + x49gp_flash_t* flash = module->user_data; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - x49gp_module_set_filename(module, key, "filename", flash->filename); + x49gp_module_set_filename( module, key, "filename", flash->filename ); - error = msync(flash->data, flash->size, MS_ASYNC); - if (error) { - fprintf(stderr, "%s:%u: msync: %s\n", - __FUNCTION__, __LINE__, strerror(errno)); - return error; - } + error = msync( flash->data, flash->size, MS_ASYNC ); + if ( error ) { + fprintf( stderr, "%s:%u: msync: %s\n", __FUNCTION__, __LINE__, strerror( errno ) ); + return error; + } - error = fsync(flash->fd); - if (error) { - fprintf(stderr, "%s:%u: fsync: %s\n", - __FUNCTION__, __LINE__, strerror(errno)); - return error; - } + error = fsync( flash->fd ); + if ( error ) { + fprintf( stderr, "%s:%u: fsync: %s\n", __FUNCTION__, __LINE__, strerror( errno ) ); + return error; + } - return 0; + return 0; } -static int -flash_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int flash_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - x49gp_flash_t *flash = module->user_data; + x49gp_flash_t* flash = module->user_data; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - flash_state_reset(flash); - return 0; + flash_state_reset( flash ); + return 0; } -static CPUReadMemoryFunc *flash_readfn[] = -{ - flash_readb, - flash_readw, - flash_readl -}; +static CPUReadMemoryFunc* flash_readfn[] = { flash_readb, flash_readw, flash_readl }; -static CPUWriteMemoryFunc *flash_writefn[] = -{ - flash_writeb, - flash_writew, - flash_writel -}; +static CPUWriteMemoryFunc* flash_writefn[] = { flash_writeb, flash_writew, flash_writel }; -static int -flash_init(x49gp_module_t *module) +static int flash_init( x49gp_module_t* module ) { - x49gp_flash_t *flash; + x49gp_flash_t* flash; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - flash = malloc(sizeof(x49gp_flash_t)); - if (NULL == flash) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->name, __FUNCTION__, __LINE__); - return -1; - } - memset(flash, 0, sizeof(x49gp_flash_t)); + flash = malloc( sizeof( x49gp_flash_t ) ); + if ( NULL == flash ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->name, __FUNCTION__, __LINE__ ); + return -1; + } + memset( flash, 0, sizeof( x49gp_flash_t ) ); - flash->vendor_ID = SST29VF160_VENDOR_ID; - flash->device_ID = SST29VF160_DEVICE_ID; - flash->cfi_data = sst29vf160_cfi_data; - flash->cfi_size = SST29VF160_CFI_SIZE; - flash->sector_size = SST29VF160_SECTOR_SIZE; - flash->block_size = SST29VF160_BLOCK_SIZE; - flash->fd = -1; + flash->vendor_ID = SST29VF160_VENDOR_ID; + flash->device_ID = SST29VF160_DEVICE_ID; + flash->cfi_data = sst29vf160_cfi_data; + flash->cfi_size = SST29VF160_CFI_SIZE; + flash->sector_size = SST29VF160_SECTOR_SIZE; + flash->block_size = SST29VF160_BLOCK_SIZE; + flash->fd = -1; - module->user_data = flash; + module->user_data = flash; - flash->iotype = cpu_register_io_memory(flash_readfn, - flash_writefn, flash); + flash->iotype = cpu_register_io_memory( flash_readfn, flash_writefn, flash ); - flash->data = (void *) -1; - flash->offset = phys_ram_size; - phys_ram_size += SST29VF160_SIZE; + flash->data = ( void* )-1; + flash->offset = phys_ram_size; + phys_ram_size += SST29VF160_SIZE; - cpu_register_physical_memory(0x00000000, SST29VF160_SIZE, - flash->offset | flash->iotype | IO_MEM_ROMD); + cpu_register_physical_memory( 0x00000000, SST29VF160_SIZE, flash->offset | flash->iotype | IO_MEM_ROMD ); - return 0; + return 0; } -static int -flash_exit(x49gp_module_t *module) +static int flash_exit( x49gp_module_t* module ) { - x49gp_flash_t *flash; + x49gp_flash_t* flash; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - flash = module->user_data; + if ( module->user_data ) { + flash = module->user_data; - if (flash->data != (void *) -1) { - munmap(flash->data, flash->size); - } - if (flash->fd) { - close(flash->fd); - } + if ( flash->data != ( void* )-1 ) { + munmap( flash->data, flash->size ); + } + if ( flash->fd ) { + close( flash->fd ); + } - free(flash); - } + free( flash ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_flash_init(x49gp_t *x49gp) +int x49gp_flash_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "flash", flash_init, flash_exit, - flash_reset, flash_load, flash_save, NULL, - &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "flash", flash_init, flash_exit, flash_reset, flash_load, flash_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/gdbstub.c b/src/gdbstub.c index cb39e0f..534c4e7 100644 --- a/src/gdbstub.c +++ b/src/gdbstub.c @@ -34,57 +34,41 @@ #define MAX_PACKET_LENGTH 4096 - -enum { - GDB_SIGNAL_0 = 0, - GDB_SIGNAL_INT = 2, - GDB_SIGNAL_TRAP = 5, - GDB_SIGNAL_UNKNOWN = 143 -}; +enum { GDB_SIGNAL_0 = 0, GDB_SIGNAL_INT = 2, GDB_SIGNAL_TRAP = 5, GDB_SIGNAL_UNKNOWN = 143 }; /* In system mode we only need SIGINT and SIGTRAP; other signals are not yet supported. */ -enum { - TARGET_SIGINT = 2, - TARGET_SIGTRAP = 5 -}; +enum { TARGET_SIGINT = 2, TARGET_SIGTRAP = 5 }; -static int gdb_signal_table[] = { - -1, - -1, - TARGET_SIGINT, - -1, - -1, - TARGET_SIGTRAP -}; +static int gdb_signal_table[] = { -1, -1, TARGET_SIGINT, -1, -1, TARGET_SIGTRAP }; -static int target_signal_to_gdb (int sig) +static int target_signal_to_gdb( int sig ) { int i; - for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) - if (gdb_signal_table[i] == sig) + for ( i = 0; i < ARRAY_SIZE( gdb_signal_table ); i++ ) + if ( gdb_signal_table[ i ] == sig ) return i; return GDB_SIGNAL_UNKNOWN; } -static int gdb_signal_to_target (int sig) +static int gdb_signal_to_target( int sig ) { - if (sig < ARRAY_SIZE (gdb_signal_table)) - return gdb_signal_table[sig]; + if ( sig < ARRAY_SIZE( gdb_signal_table ) ) + return gdb_signal_table[ sig ]; else return -1; } -//#define DEBUG_GDB +// #define DEBUG_GDB typedef struct GDBRegisterState { int base_reg; int num_regs; gdb_reg_cb get_reg; gdb_reg_cb set_reg; - const char *xml; - struct GDBRegisterState *next; + const char* xml; + struct GDBRegisterState* next; } GDBRegisterState; enum RSState { @@ -96,14 +80,14 @@ enum RSState { RS_SYSCALL, }; typedef struct GDBState { - CPUState *c_cpu; /* current CPU for step/continue ops */ - CPUState *g_cpu; /* current CPU for other ops */ - CPUState *query_cpu; /* for q{f|s}ThreadInfo */ - enum RSState state; /* parsing state */ - char line_buf[MAX_PACKET_LENGTH]; + CPUState* c_cpu; /* current CPU for step/continue ops */ + CPUState* g_cpu; /* current CPU for other ops */ + CPUState* query_cpu; /* for q{f|s}ThreadInfo */ + enum RSState state; /* parsing state */ + char line_buf[ MAX_PACKET_LENGTH ]; int line_buf_index; int line_csum; - uint8_t last_packet[MAX_PACKET_LENGTH + 4]; + uint8_t last_packet[ MAX_PACKET_LENGTH + 4 ]; int last_packet_len; int signal; int fd; @@ -113,9 +97,9 @@ typedef struct GDBState { /* By default use no IRQs and no timers while single stepping so as to * make single stepping like an ICE HW step. */ -static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; +static int sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; -static GDBState *gdbserver_state; +static GDBState* gdbserver_state; /* This is an ugly hack to cope with both new and old gdb. If gdb sends qXfer:features:read then assume we're talking to a newish @@ -125,20 +109,20 @@ static int gdb_has_xml; /* XXX: This is not thread safe. Do we care? */ static int gdbserver_fd = -1; -static int get_char(GDBState *s) +static int get_char( GDBState* s ) { uint8_t ch; int ret; - for(;;) { - ret = recv(s->fd, &ch, 1, 0); - if (ret < 0) { - if (errno == ECONNRESET) + for ( ;; ) { + ret = recv( s->fd, &ch, 1, 0 ); + if ( ret < 0 ) { + if ( errno == ECONNRESET ) s->fd = -1; - if (errno != EINTR && errno != EAGAIN) + if ( errno != EINTR && errno != EAGAIN ) return -1; - } else if (ret == 0) { - close(s->fd); + } else if ( ret == 0 ) { + close( s->fd ); s->fd = -1; return -1; } else { @@ -158,29 +142,25 @@ static enum { /* If gdb is connected when the first semihosting syscall occurs then use remote gdb syscalls. Otherwise use native file IO. */ -int use_gdb_syscalls(void) +int use_gdb_syscalls( void ) { - if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { - gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED - : GDB_SYS_DISABLED); + if ( gdb_syscall_mode == GDB_SYS_UNKNOWN ) { + gdb_syscall_mode = ( gdbserver_state ? GDB_SYS_ENABLED : GDB_SYS_DISABLED ); } return gdb_syscall_mode == GDB_SYS_ENABLED; } /* Resume execution. */ -static inline void gdb_continue(GDBState *s) -{ - s->running_state = 1; -} +static inline void gdb_continue( GDBState* s ) { s->running_state = 1; } -static void put_buffer(GDBState *s, const uint8_t *buf, int len) +static void put_buffer( GDBState* s, const uint8_t* buf, int len ) { int ret; - while (len > 0) { - ret = send(s->fd, buf, len, 0); - if (ret < 0) { - if (errno != EINTR && errno != EAGAIN) + while ( len > 0 ) { + ret = send( s->fd, buf, len, 0 ); + if ( ret < 0 ) { + if ( errno != EINTR && errno != EAGAIN ) return; } else { buf += ret; @@ -189,117 +169,121 @@ static void put_buffer(GDBState *s, const uint8_t *buf, int len) } } -static inline int fromhex(int v) +static inline int fromhex( int v ) { - if (v >= '0' && v <= '9') + if ( v >= '0' && v <= '9' ) return v - '0'; - else if (v >= 'A' && v <= 'F') + else if ( v >= 'A' && v <= 'F' ) return v - 'A' + 10; - else if (v >= 'a' && v <= 'f') + else if ( v >= 'a' && v <= 'f' ) return v - 'a' + 10; else return 0; } -static inline int tohex(int v) +static inline int tohex( int v ) { - if (v < 10) + if ( v < 10 ) return v + '0'; else return v - 10 + 'a'; } -static void memtohex(char *buf, const uint8_t *mem, int len) +static void memtohex( char* buf, const uint8_t* mem, int len ) { int i, c; - char *q; + char* q; q = buf; - for(i = 0; i < len; i++) { - c = mem[i]; - *q++ = tohex(c >> 4); - *q++ = tohex(c & 0xf); + for ( i = 0; i < len; i++ ) { + c = mem[ i ]; + *q++ = tohex( c >> 4 ); + *q++ = tohex( c & 0xf ); } *q = '\0'; } -static void hextomem(uint8_t *mem, const char *buf, int len) +static void hextomem( uint8_t* mem, const char* buf, int len ) { int i; - for(i = 0; i < len; i++) { - mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); + for ( i = 0; i < len; i++ ) { + mem[ i ] = ( fromhex( buf[ 0 ] ) << 4 ) | fromhex( buf[ 1 ] ); buf += 2; } } /* return -1 if error, 0 if OK */ -static int put_packet_binary(GDBState *s, const char *buf, int len) +static int put_packet_binary( GDBState* s, const char* buf, int len ) { int csum, i; - uint8_t *p; + uint8_t* p; - for(;;) { + for ( ;; ) { p = s->last_packet; - *(p++) = '$'; - memcpy(p, buf, len); + *( p++ ) = '$'; + memcpy( p, buf, len ); p += len; csum = 0; - for(i = 0; i < len; i++) { - csum += buf[i]; + for ( i = 0; i < len; i++ ) { + csum += buf[ i ]; } - *(p++) = '#'; - *(p++) = tohex((csum >> 4) & 0xf); - *(p++) = tohex((csum) & 0xf); + *( p++ ) = '#'; + *( p++ ) = tohex( ( csum >> 4 ) & 0xf ); + *( p++ ) = tohex( ( csum ) & 0xf ); s->last_packet_len = p - s->last_packet; - put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); + put_buffer( s, ( uint8_t* )s->last_packet, s->last_packet_len ); - i = get_char(s); - if (i < 0) + i = get_char( s ); + if ( i < 0 ) return -1; - if (i == '+') + if ( i == '+' ) break; } return 0; } /* return -1 if error, 0 if OK */ -static int put_packet(GDBState *s, const char *buf) +static int put_packet( GDBState* s, const char* buf ) { #ifdef DEBUG_GDB - printf("reply='%s'\n", buf); + printf( "reply='%s'\n", buf ); #endif - return put_packet_binary(s, buf, strlen(buf)); + return put_packet_binary( s, buf, strlen( buf ) ); } /* The GDB remote protocol transfers values in target byte order. This means we can use the raw memory access routines to access the value buffer. Conveniently, these also handle the case where the buffer is mis-aligned. */ -#define GET_REG8(val) do { \ - stb_p(mem_buf, val); \ - return 1; \ - } while(0) -#define GET_REG16(val) do { \ - stw_p(mem_buf, val); \ - return 2; \ - } while(0) -#define GET_REG32(val) do { \ - stl_p(mem_buf, val); \ - return 4; \ - } while(0) -#define GET_REG64(val) do { \ - stq_p(mem_buf, val); \ - return 8; \ - } while(0) +#define GET_REG8( val ) \ + do { \ + stb_p( mem_buf, val ); \ + return 1; \ + } while ( 0 ) +#define GET_REG16( val ) \ + do { \ + stw_p( mem_buf, val ); \ + return 2; \ + } while ( 0 ) +#define GET_REG32( val ) \ + do { \ + stl_p( mem_buf, val ); \ + return 4; \ + } while ( 0 ) +#define GET_REG64( val ) \ + do { \ + stq_p( mem_buf, val ); \ + return 8; \ + } while ( 0 ) #if TARGET_LONG_BITS == 64 -#define GET_REGL(val) GET_REG64(val) -#define ldtul_p(addr) ldq_p(addr) +# define GET_REGL( val ) GET_REG64( val ) +# define ldtul_p( addr ) ldq_p( addr ) #else -#define GET_REGL(val) GET_REG32(val) -#define ldtul_p(addr) ldl_p(addr) +# define GET_REGL( val ) GET_REG32( val ) +# define ldtul_p( addr ) ldl_p( addr ) #endif /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect @@ -308,67 +292,67 @@ static int put_packet(GDBState *s, const char *buf) We hack round this by giving the FPA regs zero size when talking to a newer gdb. */ #define NUM_CORE_REGS 26 -//#define GDB_CORE_XML "arm-core.xml" +// #define GDB_CORE_XML "arm-core.xml" -static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n) +static int cpu_gdb_read_register( CPUState* env, uint8_t* mem_buf, int n ) { - if (n < 16) { + if ( n < 16 ) { /* Core integer register. */ - GET_REG32(env->regs[n]); + GET_REG32( env->regs[ n ] ); } - if (n < 24) { + if ( n < 24 ) { /* FPA registers. */ - if (gdb_has_xml) + if ( gdb_has_xml ) return 0; - memset(mem_buf, 0, 12); + memset( mem_buf, 0, 12 ); return 12; } - switch (n) { - case 24: - /* FPA status register. */ - if (gdb_has_xml) - return 0; - GET_REG32(0); - case 25: - /* CPSR */ - GET_REG32(cpsr_read(env)); + switch ( n ) { + case 24: + /* FPA status register. */ + if ( gdb_has_xml ) + return 0; + GET_REG32( 0 ); + case 25: + /* CPSR */ + GET_REG32( cpsr_read( env ) ); } /* Unknown register. */ return 0; } -static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n) +static int cpu_gdb_write_register( CPUState* env, uint8_t* mem_buf, int n ) { uint32_t tmp; - tmp = ldl_p(mem_buf); + tmp = ldl_p( mem_buf ); /* Mask out low bit of PC to workaround gdb bugs. This will probably cause problems if we ever implement the Jazelle DBX extensions. */ - if (n == 15) + if ( n == 15 ) tmp &= ~1; - if (n < 16) { + if ( n < 16 ) { /* Core integer register. */ - env->regs[n] = tmp; + env->regs[ n ] = tmp; return 4; } - if (n < 24) { /* 16-23 */ + if ( n < 24 ) { /* 16-23 */ /* FPA registers (ignored). */ - if (gdb_has_xml) + if ( gdb_has_xml ) return 0; return 12; } - switch (n) { - case 24: - /* FPA status register (ignored). */ - if (gdb_has_xml) - return 0; - return 4; - case 25: - /* CPSR */ - cpsr_write (env, tmp, 0xffffffff); - return 4; + switch ( n ) { + case 24: + /* FPA status register (ignored). */ + if ( gdb_has_xml ) + return 0; + return 4; + case 25: + /* CPSR */ + cpsr_write( env, tmp, 0xffffffff ); + return 4; } /* Unknown register. */ return 0; @@ -378,95 +362,98 @@ static int num_g_regs = NUM_CORE_REGS; #ifdef GDB_CORE_XML /* Encode data using the encoding for 'x' packets. */ -static int memtox(char *buf, const char *mem, int len) +static int memtox( char* buf, const char* mem, int len ) { - char *p = buf; + char* p = buf; char c; - while (len--) { - c = *(mem++); - switch (c) { - case '#': case '$': case '*': case '}': - *(p++) = '}'; - *(p++) = c ^ 0x20; - break; - default: - *(p++) = c; - break; + while ( len-- ) { + c = *( mem++ ); + switch ( c ) { + case '#': + case '$': + case '*': + case '}': + *( p++ ) = '}'; + *( p++ ) = c ^ 0x20; + break; + default: + *( p++ ) = c; + break; } } return p - buf; } -static const char *get_feature_xml(const char *p, const char **newp) +static const char* get_feature_xml( const char* p, const char** newp ) { - extern const char *const xml_builtin[][2]; + extern const char* const xml_builtin[][ 2 ]; size_t len; int i; - const char *name; - static char target_xml[1024]; + const char* name; + static char target_xml[ 1024 ]; len = 0; - while (p[len] && p[len] != ':') + while ( p[ len ] && p[ len ] != ':' ) len++; *newp = p + len; name = NULL; - if (strncmp(p, "target.xml", len) == 0) { + if ( strncmp( p, "target.xml", len ) == 0 ) { /* Generate the XML description for this CPU. */ - if (!target_xml[0]) { - GDBRegisterState *r; + if ( !target_xml[ 0 ] ) { + GDBRegisterState* r; - snprintf(target_xml, sizeof(target_xml), - "" - "" - "" - "", - GDB_CORE_XML); + snprintf( target_xml, sizeof( target_xml ), + "" + "" + "" + "", + GDB_CORE_XML ); - for (r = first_cpu->gdb_regs; r; r = r->next) { - pstrcat(target_xml, sizeof(target_xml), "xml); - pstrcat(target_xml, sizeof(target_xml), "\"/>"); + for ( r = first_cpu->gdb_regs; r; r = r->next ) { + pstrcat( target_xml, sizeof( target_xml ), "xml ); + pstrcat( target_xml, sizeof( target_xml ), "\"/>" ); } - pstrcat(target_xml, sizeof(target_xml), ""); + pstrcat( target_xml, sizeof( target_xml ), "" ); } return target_xml; } - for (i = 0; ; i++) { - name = xml_builtin[i][0]; - if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) + for ( i = 0;; i++ ) { + name = xml_builtin[ i ][ 0 ]; + if ( !name || ( strncmp( name, p, len ) == 0 && strlen( name ) == len ) ) break; } - return name ? xml_builtin[i][1] : NULL; + return name ? xml_builtin[ i ][ 1 ] : NULL; } #endif -static int gdb_read_register(CPUState *env, uint8_t *mem_buf, int reg) +static int gdb_read_register( CPUState* env, uint8_t* mem_buf, int reg ) { - GDBRegisterState *r; + GDBRegisterState* r; - if (reg < NUM_CORE_REGS) - return cpu_gdb_read_register(env, mem_buf, reg); + if ( reg < NUM_CORE_REGS ) + return cpu_gdb_read_register( env, mem_buf, reg ); - for (r = env->gdb_regs; r; r = r->next) { - if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { - return r->get_reg(env, mem_buf, reg - r->base_reg); + for ( r = env->gdb_regs; r; r = r->next ) { + if ( r->base_reg <= reg && reg < r->base_reg + r->num_regs ) { + return r->get_reg( env, mem_buf, reg - r->base_reg ); } } return 0; } -static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg) +static int gdb_write_register( CPUState* env, uint8_t* mem_buf, int reg ) { - GDBRegisterState *r; + GDBRegisterState* r; - if (reg < NUM_CORE_REGS) - return cpu_gdb_write_register(env, mem_buf, reg); + if ( reg < NUM_CORE_REGS ) + return cpu_gdb_write_register( env, mem_buf, reg ); - for (r = env->gdb_regs; r; r = r->next) { - if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { - return r->set_reg(env, mem_buf, reg - r->base_reg); + for ( r = env->gdb_regs; r; r = r->next ) { + if ( r->base_reg <= reg && reg < r->base_reg + r->num_regs ) { + return r->set_reg( env, mem_buf, reg - r->base_reg ); } } return 0; @@ -478,34 +465,34 @@ static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg) gdb reading a CPU register, and set_reg is gdb modifying a CPU register. */ -void gdb_register_coprocessor(CPUState * env, - gdb_reg_cb get_reg, gdb_reg_cb set_reg, - int num_regs, const char *xml, int g_pos) +void gdb_register_coprocessor( CPUState* env, gdb_reg_cb get_reg, gdb_reg_cb set_reg, int num_regs, const char* xml, int g_pos ) { - GDBRegisterState *s; - GDBRegisterState **p; + GDBRegisterState* s; + GDBRegisterState** p; static int last_reg = NUM_CORE_REGS; - s = (GDBRegisterState *)qemu_mallocz(sizeof(GDBRegisterState)); + s = ( GDBRegisterState* )qemu_mallocz( sizeof( GDBRegisterState ) ); s->base_reg = last_reg; s->num_regs = num_regs; s->get_reg = get_reg; s->set_reg = set_reg; s->xml = xml; p = &env->gdb_regs; - while (*p) { + while ( *p ) { /* Check for duplicates. */ - if (strcmp((*p)->xml, xml) == 0) + if ( strcmp( ( *p )->xml, xml ) == 0 ) return; - p = &(*p)->next; + p = &( *p )->next; } /* Add to end of list. */ last_reg += num_regs; *p = s; - if (g_pos) { - if (g_pos != s->base_reg) { - fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n" - "Expected %d got %d\n", xml, g_pos, s->base_reg); + if ( g_pos ) { + if ( g_pos != s->base_reg ) { + fprintf( stderr, + "Error: Bad gdb register numbering for '%s'\n" + "Expected %d got %d\n", + xml, g_pos, s->base_reg ); } else { num_g_regs = last_reg; } @@ -514,105 +501,101 @@ void gdb_register_coprocessor(CPUState * env, #ifndef CONFIG_USER_ONLY static const int xlat_gdb_type[] = { - [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, - [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, + [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, + [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, }; #endif -static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type) +static int gdb_breakpoint_insert( target_ulong addr, target_ulong len, int type ) { - CPUState *env; + CPUState* env; int err = 0; - switch (type) { - case GDB_BREAKPOINT_SW: - case GDB_BREAKPOINT_HW: - for (env = first_cpu; env != NULL; env = env->next_cpu) { - err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL); - if (err) - break; - } - return err; + switch ( type ) { + case GDB_BREAKPOINT_SW: + case GDB_BREAKPOINT_HW: + for ( env = first_cpu; env != NULL; env = env->next_cpu ) { + err = cpu_breakpoint_insert( env, addr, BP_GDB, NULL ); + if ( err ) + break; + } + return err; #ifndef CONFIG_USER_ONLY - case GDB_WATCHPOINT_WRITE: - case GDB_WATCHPOINT_READ: - case GDB_WATCHPOINT_ACCESS: - for (env = first_cpu; env != NULL; env = env->next_cpu) { - err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type], - NULL); - if (err) - break; - } - return err; + case GDB_WATCHPOINT_WRITE: + case GDB_WATCHPOINT_READ: + case GDB_WATCHPOINT_ACCESS: + for ( env = first_cpu; env != NULL; env = env->next_cpu ) { + err = cpu_watchpoint_insert( env, addr, len, xlat_gdb_type[ type ], NULL ); + if ( err ) + break; + } + return err; #endif - default: - return -ENOSYS; + default: + return -ENOSYS; } } -static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type) +static int gdb_breakpoint_remove( target_ulong addr, target_ulong len, int type ) { - CPUState *env; + CPUState* env; int err = 0; - switch (type) { - case GDB_BREAKPOINT_SW: - case GDB_BREAKPOINT_HW: - for (env = first_cpu; env != NULL; env = env->next_cpu) { - err = cpu_breakpoint_remove(env, addr, BP_GDB); - if (err) - break; - } - return err; + switch ( type ) { + case GDB_BREAKPOINT_SW: + case GDB_BREAKPOINT_HW: + for ( env = first_cpu; env != NULL; env = env->next_cpu ) { + err = cpu_breakpoint_remove( env, addr, BP_GDB ); + if ( err ) + break; + } + return err; #ifndef CONFIG_USER_ONLY - case GDB_WATCHPOINT_WRITE: - case GDB_WATCHPOINT_READ: - case GDB_WATCHPOINT_ACCESS: - for (env = first_cpu; env != NULL; env = env->next_cpu) { - err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]); - if (err) - break; - } - return err; + case GDB_WATCHPOINT_WRITE: + case GDB_WATCHPOINT_READ: + case GDB_WATCHPOINT_ACCESS: + for ( env = first_cpu; env != NULL; env = env->next_cpu ) { + err = cpu_watchpoint_remove( env, addr, len, xlat_gdb_type[ type ] ); + if ( err ) + break; + } + return err; #endif - default: - return -ENOSYS; + default: + return -ENOSYS; } } -static void gdb_breakpoint_remove_all(void) +static void gdb_breakpoint_remove_all( void ) { - CPUState *env; + CPUState* env; - for (env = first_cpu; env != NULL; env = env->next_cpu) { - cpu_breakpoint_remove_all(env, BP_GDB); + for ( env = first_cpu; env != NULL; env = env->next_cpu ) { + cpu_breakpoint_remove_all( env, BP_GDB ); #ifndef CONFIG_USER_ONLY - cpu_watchpoint_remove_all(env, BP_GDB); + cpu_watchpoint_remove_all( env, BP_GDB ); #endif } } -static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) -{ - s->c_cpu->regs[15] = pc; -} +static void gdb_set_cpu_pc( GDBState* s, target_ulong pc ) { s->c_cpu->regs[ 15 ] = pc; } -static inline int gdb_id(CPUState *env) +static inline int gdb_id( CPUState* env ) { -#if defined(CONFIG_USER_ONLY) && defined(CONFIG_USE_NPTL) +#if defined( CONFIG_USER_ONLY ) && defined( CONFIG_USE_NPTL ) return env->host_tid; #else return env->cpu_index + 1; #endif } -static CPUState *find_cpu(uint32_t thread_id) +static CPUState* find_cpu( uint32_t thread_id ) { - CPUState *env; + CPUState* env; - for (env = first_cpu; env != NULL; env = env->next_cpu) { - if (gdb_id(env) == thread_id) { + for ( env = first_cpu; env != NULL; env = env->next_cpu ) { + if ( gdb_id( env ) == thread_id ) { return env; } } @@ -620,391 +603,386 @@ static CPUState *find_cpu(uint32_t thread_id) return NULL; } -static int gdb_handle_packet(GDBState *s, const char *line_buf) +static int gdb_handle_packet( GDBState* s, const char* line_buf ) { - CPUState *env; - const char *p; + CPUState* env; + const char* p; uint32_t thread; int ch, reg_size, type, res; - char buf[MAX_PACKET_LENGTH]; - uint8_t mem_buf[MAX_PACKET_LENGTH]; - uint8_t *registers; + char buf[ MAX_PACKET_LENGTH ]; + uint8_t mem_buf[ MAX_PACKET_LENGTH ]; + uint8_t* registers; target_ulong addr, len; #ifdef DEBUG_GDB - printf("command='%s'\n", line_buf); + printf( "command='%s'\n", line_buf ); #endif p = line_buf; ch = *p++; - switch(ch) { - case '?': - /* TODO: Make this return the correct value for user-mode. */ - snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP, - gdb_id(s->c_cpu)); - put_packet(s, buf); - /* Remove all the breakpoints when this query is issued, - * because gdb is doing and initial connect and the state - * should be cleaned up. - */ - gdb_breakpoint_remove_all(); - break; - case 'c': - if (*p != '\0') { - addr = strtoull(p, (char **)&p, 16); - gdb_set_cpu_pc(s, addr); - } - s->signal = 0; - gdb_continue(s); - return RS_IDLE; - case 'C': - s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16)); - if (s->signal == -1) + switch ( ch ) { + case '?': + /* TODO: Make this return the correct value for user-mode. */ + snprintf( buf, sizeof( buf ), "T%02xthread:%02x;", GDB_SIGNAL_TRAP, gdb_id( s->c_cpu ) ); + put_packet( s, buf ); + /* Remove all the breakpoints when this query is issued, + * because gdb is doing and initial connect and the state + * should be cleaned up. + */ + gdb_breakpoint_remove_all(); + break; + case 'c': + if ( *p != '\0' ) { + addr = strtoull( p, ( char** )&p, 16 ); + gdb_set_cpu_pc( s, addr ); + } s->signal = 0; - gdb_continue(s); - return RS_IDLE; - case 'v': - if (strncmp(p, "Cont", 4) == 0) { - int res_signal, res_thread; + gdb_continue( s ); + return RS_IDLE; + case 'C': + s->signal = gdb_signal_to_target( strtoul( p, ( char** )&p, 16 ) ); + if ( s->signal == -1 ) + s->signal = 0; + gdb_continue( s ); + return RS_IDLE; + case 'v': + if ( strncmp( p, "Cont", 4 ) == 0 ) { + int res_signal, res_thread; - p += 4; - if (*p == '?') { - put_packet(s, "vCont;c;C;s;S"); - break; - } - res = 0; - res_signal = 0; - res_thread = 0; - while (*p) { - int action, signal; - - if (*p++ != ';') { - res = 0; + p += 4; + if ( *p == '?' ) { + put_packet( s, "vCont;c;C;s;S" ); break; } - action = *p++; - signal = 0; - if (action == 'C' || action == 'S') { - signal = strtoul(p, (char **)&p, 16); - } else if (action != 'c' && action != 's') { - res = 0; - break; - } - thread = 0; - if (*p == ':') { - thread = strtoull(p+1, (char **)&p, 16); - } - action = tolower(action); - if (res == 0 || (res == 'c' && action == 's')) { - res = action; - res_signal = signal; - res_thread = thread; - } - } - if (res) { - if (res_thread != -1 && res_thread != 0) { - env = find_cpu(res_thread); - if (env == NULL) { - put_packet(s, "E22"); + res = 0; + res_signal = 0; + res_thread = 0; + while ( *p ) { + int action, signal; + + if ( *p++ != ';' ) { + res = 0; break; } - s->c_cpu = env; + action = *p++; + signal = 0; + if ( action == 'C' || action == 'S' ) { + signal = strtoul( p, ( char** )&p, 16 ); + } else if ( action != 'c' && action != 's' ) { + res = 0; + break; + } + thread = 0; + if ( *p == ':' ) { + thread = strtoull( p + 1, ( char** )&p, 16 ); + } + action = tolower( action ); + if ( res == 0 || ( res == 'c' && action == 's' ) ) { + res = action; + res_signal = signal; + res_thread = thread; + } } - if (res == 's') { - cpu_single_step(s->c_cpu, sstep_flags); + if ( res ) { + if ( res_thread != -1 && res_thread != 0 ) { + env = find_cpu( res_thread ); + if ( env == NULL ) { + put_packet( s, "E22" ); + break; + } + s->c_cpu = env; + } + if ( res == 's' ) { + cpu_single_step( s->c_cpu, sstep_flags ); + } + s->signal = res_signal; + gdb_continue( s ); + return RS_IDLE; } - s->signal = res_signal; - gdb_continue(s); - return RS_IDLE; + break; + } else { + goto unknown_command; } + case 'k': + /* Kill the target */ + fprintf( stderr, "\nQEMU: Terminated via GDBstub\n" ); + exit( 0 ); + case 'D': + /* Detach packet */ + gdb_breakpoint_remove_all(); + gdb_continue( s ); + put_packet( s, "OK" ); break; - } else { - goto unknown_command; - } - case 'k': - /* Kill the target */ - fprintf(stderr, "\nQEMU: Terminated via GDBstub\n"); - exit(0); - case 'D': - /* Detach packet */ - gdb_breakpoint_remove_all(); - gdb_continue(s); - put_packet(s, "OK"); - break; - case 's': - if (*p != '\0') { - addr = strtoull(p, (char **)&p, 16); - gdb_set_cpu_pc(s, addr); - } - cpu_single_step(s->c_cpu, sstep_flags); - gdb_continue(s); - return RS_IDLE; - case 'F': - { - target_ulong ret; - target_ulong err; + case 's': + if ( *p != '\0' ) { + addr = strtoull( p, ( char** )&p, 16 ); + gdb_set_cpu_pc( s, addr ); + } + cpu_single_step( s->c_cpu, sstep_flags ); + gdb_continue( s ); + return RS_IDLE; + case 'F': + { + target_ulong ret; + target_ulong err; - ret = strtoull(p, (char **)&p, 16); - if (*p == ',') { - p++; - err = strtoull(p, (char **)&p, 16); - } else { - err = 0; + ret = strtoull( p, ( char** )&p, 16 ); + if ( *p == ',' ) { + p++; + err = strtoull( p, ( char** )&p, 16 ); + } else { + err = 0; + } + if ( *p == ',' ) + p++; + type = *p; + if ( gdb_current_syscall_cb ) + gdb_current_syscall_cb( s->c_cpu, ret, err ); + if ( type == 'C' ) { + put_packet( s, "T02" ); + } else { + gdb_continue( s ); + } } - if (*p == ',') - p++; - type = *p; - if (gdb_current_syscall_cb) - gdb_current_syscall_cb(s->c_cpu, ret, err); - if (type == 'C') { - put_packet(s, "T02"); - } else { - gdb_continue(s); - } - } - break; - case 'g': - len = 0; - for (addr = 0; addr < num_g_regs; addr++) { - reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr); - len += reg_size; - } - memtohex(buf, mem_buf, len); - put_packet(s, buf); - break; - case 'G': - registers = mem_buf; - len = strlen(p) / 2; - hextomem((uint8_t *)registers, p, len); - for (addr = 0; addr < num_g_regs && len > 0; addr++) { - reg_size = gdb_write_register(s->g_cpu, registers, addr); - len -= reg_size; - registers += reg_size; - } - put_packet(s, "OK"); - break; - case 'm': - addr = strtoull(p, (char **)&p, 16); - if (*p == ',') - p++; - len = strtoull(p, NULL, 16); - if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) { - put_packet (s, "E14"); - } else { - memtohex(buf, mem_buf, len); - put_packet(s, buf); - } - break; - case 'M': - addr = strtoull(p, (char **)&p, 16); - if (*p == ',') - p++; - len = strtoull(p, (char **)&p, 16); - if (*p == ':') - p++; - hextomem(mem_buf, p, len); - if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0) - put_packet(s, "E14"); - else - put_packet(s, "OK"); - break; - case 'p': - /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. - This works, but can be very slow. Anything new enough to - understand XML also knows how to use this properly. */ - if (!gdb_has_xml) - goto unknown_command; - addr = strtoull(p, (char **)&p, 16); - reg_size = gdb_read_register(s->g_cpu, mem_buf, addr); - if (reg_size) { - memtohex(buf, mem_buf, reg_size); - put_packet(s, buf); - } else { - put_packet(s, "E14"); - } - break; - case 'P': - if (!gdb_has_xml) - goto unknown_command; - addr = strtoull(p, (char **)&p, 16); - if (*p == '=') - p++; - reg_size = strlen(p) / 2; - hextomem(mem_buf, p, reg_size); - gdb_write_register(s->g_cpu, mem_buf, addr); - put_packet(s, "OK"); - break; - case 'Z': - case 'z': - type = strtoul(p, (char **)&p, 16); - if (*p == ',') - p++; - addr = strtoull(p, (char **)&p, 16); - if (*p == ',') - p++; - len = strtoull(p, (char **)&p, 16); - if (ch == 'Z') - res = gdb_breakpoint_insert(addr, len, type); - else - res = gdb_breakpoint_remove(addr, len, type); - if (res >= 0) - put_packet(s, "OK"); - else if (res == -ENOSYS) - put_packet(s, ""); - else - put_packet(s, "E22"); - break; - case 'H': - type = *p++; - thread = strtoull(p, (char **)&p, 16); - if (thread == -1 || thread == 0) { - put_packet(s, "OK"); - break; - } - env = find_cpu(thread); - if (env == NULL) { - put_packet(s, "E22"); - break; - } - switch (type) { - case 'c': - s->c_cpu = env; - put_packet(s, "OK"); break; case 'g': - s->g_cpu = env; - put_packet(s, "OK"); - break; - default: - put_packet(s, "E22"); - break; - } - break; - case 'T': - thread = strtoull(p, (char **)&p, 16); - env = find_cpu(thread); - - if (env != NULL) { - put_packet(s, "OK"); - } else { - put_packet(s, "E22"); - } - break; - case 'q': - case 'Q': - /* parse any 'q' packets here */ - if (!strcmp(p,"qemu.sstepbits")) { - /* Query Breakpoint bit definitions */ - snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", - SSTEP_ENABLE, - SSTEP_NOIRQ, - SSTEP_NOTIMER); - put_packet(s, buf); - break; - } else if (strncmp(p,"qemu.sstep",10) == 0) { - /* Display or change the sstep_flags */ - p += 10; - if (*p != '=') { - /* Display current setting */ - snprintf(buf, sizeof(buf), "0x%x", sstep_flags); - put_packet(s, buf); - break; + len = 0; + for ( addr = 0; addr < num_g_regs; addr++ ) { + reg_size = gdb_read_register( s->g_cpu, mem_buf + len, addr ); + len += reg_size; } - p++; - type = strtoul(p, (char **)&p, 16); - sstep_flags = type; - put_packet(s, "OK"); + memtohex( buf, mem_buf, len ); + put_packet( s, buf ); break; - } else if (strcmp(p,"C") == 0) { - /* "Current thread" remains vague in the spec, so always return - * the first CPU (gdb returns the first thread). */ - put_packet(s, "QC1"); - break; - } else if (strcmp(p,"fThreadInfo") == 0) { - s->query_cpu = first_cpu; - goto report_cpuinfo; - } else if (strcmp(p,"sThreadInfo") == 0) { - report_cpuinfo: - if (s->query_cpu) { - snprintf(buf, sizeof(buf), "m%x", gdb_id(s->query_cpu)); - put_packet(s, buf); - s->query_cpu = s->query_cpu->next_cpu; - } else - put_packet(s, "l"); - break; - } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) { - thread = strtoull(p+16, (char **)&p, 16); - env = find_cpu(thread); - if (env != NULL) { - len = snprintf((char *)mem_buf, sizeof(mem_buf), - "CPU#%d [%s]", env->cpu_index, - env->halted ? "halted " : "running"); - memtohex(buf, mem_buf, len); - put_packet(s, buf); + case 'G': + registers = mem_buf; + len = strlen( p ) / 2; + hextomem( ( uint8_t* )registers, p, len ); + for ( addr = 0; addr < num_g_regs && len > 0; addr++ ) { + reg_size = gdb_write_register( s->g_cpu, registers, addr ); + len -= reg_size; + registers += reg_size; } + put_packet( s, "OK" ); break; - } - if (strncmp(p, "Supported", 9) == 0) { - snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH); -#ifdef GDB_CORE_XML - pstrcat(buf, sizeof(buf), ";qXfer:features:read+"); -#endif - put_packet(s, buf); - break; - } -#ifdef GDB_CORE_XML - if (strncmp(p, "Xfer:features:read:", 19) == 0) { - const char *xml; - target_ulong total_len; - - gdb_has_xml = 1; - p += 19; - xml = get_feature_xml(p, &p); - if (!xml) { - snprintf(buf, sizeof(buf), "E00"); - put_packet(s, buf); - break; - } - - if (*p == ':') + case 'm': + addr = strtoull( p, ( char** )&p, 16 ); + if ( *p == ',' ) p++; - addr = strtoul(p, (char **)&p, 16); - if (*p == ',') - p++; - len = strtoul(p, (char **)&p, 16); - - total_len = strlen(xml); - if (addr > total_len) { - snprintf(buf, sizeof(buf), "E00"); - put_packet(s, buf); - break; - } - if (len > (MAX_PACKET_LENGTH - 5) / 2) - len = (MAX_PACKET_LENGTH - 5) / 2; - if (len < total_len - addr) { - buf[0] = 'm'; - len = memtox(buf + 1, xml + addr, len); + len = strtoull( p, NULL, 16 ); + if ( cpu_memory_rw_debug( s->g_cpu, addr, mem_buf, len, 0 ) != 0 ) { + put_packet( s, "E14" ); } else { - buf[0] = 'l'; - len = memtox(buf + 1, xml + addr, total_len - addr); + memtohex( buf, mem_buf, len ); + put_packet( s, buf ); } - put_packet_binary(s, buf, len + 1); break; - } -#endif - /* Unrecognised 'q' command. */ - goto unknown_command; + case 'M': + addr = strtoull( p, ( char** )&p, 16 ); + if ( *p == ',' ) + p++; + len = strtoull( p, ( char** )&p, 16 ); + if ( *p == ':' ) + p++; + hextomem( mem_buf, p, len ); + if ( cpu_memory_rw_debug( s->g_cpu, addr, mem_buf, len, 1 ) != 0 ) + put_packet( s, "E14" ); + else + put_packet( s, "OK" ); + break; + case 'p': + /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. + This works, but can be very slow. Anything new enough to + understand XML also knows how to use this properly. */ + if ( !gdb_has_xml ) + goto unknown_command; + addr = strtoull( p, ( char** )&p, 16 ); + reg_size = gdb_read_register( s->g_cpu, mem_buf, addr ); + if ( reg_size ) { + memtohex( buf, mem_buf, reg_size ); + put_packet( s, buf ); + } else { + put_packet( s, "E14" ); + } + break; + case 'P': + if ( !gdb_has_xml ) + goto unknown_command; + addr = strtoull( p, ( char** )&p, 16 ); + if ( *p == '=' ) + p++; + reg_size = strlen( p ) / 2; + hextomem( mem_buf, p, reg_size ); + gdb_write_register( s->g_cpu, mem_buf, addr ); + put_packet( s, "OK" ); + break; + case 'Z': + case 'z': + type = strtoul( p, ( char** )&p, 16 ); + if ( *p == ',' ) + p++; + addr = strtoull( p, ( char** )&p, 16 ); + if ( *p == ',' ) + p++; + len = strtoull( p, ( char** )&p, 16 ); + if ( ch == 'Z' ) + res = gdb_breakpoint_insert( addr, len, type ); + else + res = gdb_breakpoint_remove( addr, len, type ); + if ( res >= 0 ) + put_packet( s, "OK" ); + else if ( res == -ENOSYS ) + put_packet( s, "" ); + else + put_packet( s, "E22" ); + break; + case 'H': + type = *p++; + thread = strtoull( p, ( char** )&p, 16 ); + if ( thread == -1 || thread == 0 ) { + put_packet( s, "OK" ); + break; + } + env = find_cpu( thread ); + if ( env == NULL ) { + put_packet( s, "E22" ); + break; + } + switch ( type ) { + case 'c': + s->c_cpu = env; + put_packet( s, "OK" ); + break; + case 'g': + s->g_cpu = env; + put_packet( s, "OK" ); + break; + default: + put_packet( s, "E22" ); + break; + } + break; + case 'T': + thread = strtoull( p, ( char** )&p, 16 ); + env = find_cpu( thread ); - default: - unknown_command: - /* put empty packet */ - buf[0] = '\0'; - put_packet(s, buf); - break; + if ( env != NULL ) { + put_packet( s, "OK" ); + } else { + put_packet( s, "E22" ); + } + break; + case 'q': + case 'Q': + /* parse any 'q' packets here */ + if ( !strcmp( p, "qemu.sstepbits" ) ) { + /* Query Breakpoint bit definitions */ + snprintf( buf, sizeof( buf ), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE, SSTEP_NOIRQ, SSTEP_NOTIMER ); + put_packet( s, buf ); + break; + } else if ( strncmp( p, "qemu.sstep", 10 ) == 0 ) { + /* Display or change the sstep_flags */ + p += 10; + if ( *p != '=' ) { + /* Display current setting */ + snprintf( buf, sizeof( buf ), "0x%x", sstep_flags ); + put_packet( s, buf ); + break; + } + p++; + type = strtoul( p, ( char** )&p, 16 ); + sstep_flags = type; + put_packet( s, "OK" ); + break; + } else if ( strcmp( p, "C" ) == 0 ) { + /* "Current thread" remains vague in the spec, so always return + * the first CPU (gdb returns the first thread). */ + put_packet( s, "QC1" ); + break; + } else if ( strcmp( p, "fThreadInfo" ) == 0 ) { + s->query_cpu = first_cpu; + goto report_cpuinfo; + } else if ( strcmp( p, "sThreadInfo" ) == 0 ) { +report_cpuinfo: + if ( s->query_cpu ) { + snprintf( buf, sizeof( buf ), "m%x", gdb_id( s->query_cpu ) ); + put_packet( s, buf ); + s->query_cpu = s->query_cpu->next_cpu; + } else + put_packet( s, "l" ); + break; + } else if ( strncmp( p, "ThreadExtraInfo,", 16 ) == 0 ) { + thread = strtoull( p + 16, ( char** )&p, 16 ); + env = find_cpu( thread ); + if ( env != NULL ) { + len = + snprintf( ( char* )mem_buf, sizeof( mem_buf ), "CPU#%d [%s]", env->cpu_index, env->halted ? "halted " : "running" ); + memtohex( buf, mem_buf, len ); + put_packet( s, buf ); + } + break; + } + if ( strncmp( p, "Supported", 9 ) == 0 ) { + snprintf( buf, sizeof( buf ), "PacketSize=%x", MAX_PACKET_LENGTH ); +#ifdef GDB_CORE_XML + pstrcat( buf, sizeof( buf ), ";qXfer:features:read+" ); +#endif + put_packet( s, buf ); + break; + } +#ifdef GDB_CORE_XML + if ( strncmp( p, "Xfer:features:read:", 19 ) == 0 ) { + const char* xml; + target_ulong total_len; + + gdb_has_xml = 1; + p += 19; + xml = get_feature_xml( p, &p ); + if ( !xml ) { + snprintf( buf, sizeof( buf ), "E00" ); + put_packet( s, buf ); + break; + } + + if ( *p == ':' ) + p++; + addr = strtoul( p, ( char** )&p, 16 ); + if ( *p == ',' ) + p++; + len = strtoul( p, ( char** )&p, 16 ); + + total_len = strlen( xml ); + if ( addr > total_len ) { + snprintf( buf, sizeof( buf ), "E00" ); + put_packet( s, buf ); + break; + } + if ( len > ( MAX_PACKET_LENGTH - 5 ) / 2 ) + len = ( MAX_PACKET_LENGTH - 5 ) / 2; + if ( len < total_len - addr ) { + buf[ 0 ] = 'm'; + len = memtox( buf + 1, xml + addr, len ); + } else { + buf[ 0 ] = 'l'; + len = memtox( buf + 1, xml + addr, total_len - addr ); + } + put_packet_binary( s, buf, len + 1 ); + break; + } +#endif + /* Unrecognised 'q' command. */ + goto unknown_command; + + default: +unknown_command: + /* put empty packet */ + buf[ 0 ] = '\0'; + put_packet( s, buf ); + break; } return RS_IDLE; } -void gdb_set_stop_cpu(CPUState *env) +void gdb_set_stop_cpu( CPUState* env ) { gdbserver_state->c_cpu = env; gdbserver_state->g_cpu = env; @@ -1015,264 +993,255 @@ void gdb_set_stop_cpu(CPUState *env) %x - target_ulong argument printed in hex. %lx - 64-bit argument printed in hex. %s - string pointer (target_ulong) and length (int) pair. */ -void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) +void gdb_do_syscall( gdb_syscall_complete_cb cb, const char* fmt, ... ) { va_list va; - char buf[256]; - char *p; + char buf[ 256 ]; + char* p; target_ulong addr; uint64_t i64; - GDBState *s; + GDBState* s; s = gdbserver_state; - if (!s) + if ( !s ) return; gdb_current_syscall_cb = cb; s->state = RS_IDLE; - va_start(va, fmt); + va_start( va, fmt ); p = buf; - *(p++) = 'F'; - while (*fmt) { - if (*fmt == '%') { + *( p++ ) = 'F'; + while ( *fmt ) { + if ( *fmt == '%' ) { fmt++; - switch (*fmt++) { - case 'x': - addr = va_arg(va, target_ulong); - p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx, addr); - break; - case 'l': - if (*(fmt++) != 'x') - goto bad_format; - i64 = va_arg(va, uint64_t); - p += snprintf(p, &buf[sizeof(buf)] - p, "%" PRIx64, i64); - break; - case 's': - addr = va_arg(va, target_ulong); - p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx "/%x", - addr, va_arg(va, int)); - break; - default: - bad_format: - fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n", - fmt - 1); - break; + switch ( *fmt++ ) { + case 'x': + addr = va_arg( va, target_ulong ); + p += snprintf( p, &buf[ sizeof( buf ) ] - p, TARGET_FMT_lx, addr ); + break; + case 'l': + if ( *( fmt++ ) != 'x' ) + goto bad_format; + i64 = va_arg( va, uint64_t ); + p += snprintf( p, &buf[ sizeof( buf ) ] - p, "%" PRIx64, i64 ); + break; + case 's': + addr = va_arg( va, target_ulong ); + p += snprintf( p, &buf[ sizeof( buf ) ] - p, TARGET_FMT_lx "/%x", addr, va_arg( va, int ) ); + break; + default: +bad_format: + fprintf( stderr, "gdbstub: Bad syscall format string '%s'\n", fmt - 1 ); + break; } } else { - *(p++) = *(fmt++); + *( p++ ) = *( fmt++ ); } } *p = 0; - va_end(va); - put_packet(s, buf); - cpu_exit(s->c_cpu); + va_end( va ); + put_packet( s, buf ); + cpu_exit( s->c_cpu ); } -static void gdb_read_byte(GDBState *s, int ch) +static void gdb_read_byte( GDBState* s, int ch ) { - char buf[256]; + char buf[ 256 ]; int i, csum; uint8_t reply; #ifdef DEBUG_GDB - printf("%s: state %u, byte %02x (%c)\n", __FUNCTION__, s->state, ch, ch); - fflush(stdout); + printf( "%s: state %u, byte %02x (%c)\n", __FUNCTION__, s->state, ch, ch ); + fflush( stdout ); #endif { - switch(s->state) { - case RS_IDLE: - if (ch == '$') { - s->line_buf_index = 0; - s->state = RS_GETLINE; - } else if (ch == 0x03) { - snprintf(buf, sizeof(buf), "S%02x", SIGINT); - put_packet(s, buf); - } - break; - case RS_GETLINE: - if (ch == '#') { - s->state = RS_CHKSUM1; - } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { - s->state = RS_IDLE; - } else { - s->line_buf[s->line_buf_index++] = ch; - } - break; - case RS_CHKSUM1: - s->line_buf[s->line_buf_index] = '\0'; - s->line_csum = fromhex(ch) << 4; - s->state = RS_CHKSUM2; - break; - case RS_CHKSUM2: - s->line_csum |= fromhex(ch); - csum = 0; - for(i = 0; i < s->line_buf_index; i++) { - csum += s->line_buf[i]; - } - if (s->line_csum != (csum & 0xff)) { - reply = '-'; - put_buffer(s, &reply, 1); - s->state = RS_IDLE; - } else { - reply = '+'; - put_buffer(s, &reply, 1); - s->state = gdb_handle_packet(s, s->line_buf); - } - break; - default: - abort(); + switch ( s->state ) { + case RS_IDLE: + if ( ch == '$' ) { + s->line_buf_index = 0; + s->state = RS_GETLINE; + } else if ( ch == 0x03 ) { + snprintf( buf, sizeof( buf ), "S%02x", SIGINT ); + put_packet( s, buf ); + } + break; + case RS_GETLINE: + if ( ch == '#' ) { + s->state = RS_CHKSUM1; + } else if ( s->line_buf_index >= sizeof( s->line_buf ) - 1 ) { + s->state = RS_IDLE; + } else { + s->line_buf[ s->line_buf_index++ ] = ch; + } + break; + case RS_CHKSUM1: + s->line_buf[ s->line_buf_index ] = '\0'; + s->line_csum = fromhex( ch ) << 4; + s->state = RS_CHKSUM2; + break; + case RS_CHKSUM2: + s->line_csum |= fromhex( ch ); + csum = 0; + for ( i = 0; i < s->line_buf_index; i++ ) { + csum += s->line_buf[ i ]; + } + if ( s->line_csum != ( csum & 0xff ) ) { + reply = '-'; + put_buffer( s, &reply, 1 ); + s->state = RS_IDLE; + } else { + reply = '+'; + put_buffer( s, &reply, 1 ); + s->state = gdb_handle_packet( s, s->line_buf ); + } + break; + default: + abort(); } } } -int -gdb_queuesig (void) +int gdb_queuesig( void ) { - GDBState *s; + GDBState* s; s = gdbserver_state; - if (gdbserver_fd < 0 || s->fd < 0) + if ( gdbserver_fd < 0 || s->fd < 0 ) return 0; else return 1; } -int -gdb_handlesig (CPUState *env, int sig) +int gdb_handlesig( CPUState* env, int sig ) { - GDBState *s; - char buf[256]; - int n; + GDBState* s; + char buf[ 256 ]; + int n; - s = gdbserver_state; - if (gdbserver_fd < 0 || s->fd < 0) - return sig; + s = gdbserver_state; + if ( gdbserver_fd < 0 || s->fd < 0 ) + return sig; #ifdef DEBUG_GDB - printf("%s: sig: %u\n", __FUNCTION__, sig); - fflush(stdout); + printf( "%s: sig: %u\n", __FUNCTION__, sig ); + fflush( stdout ); #endif - /* disable single step if it was enabled */ - cpu_single_step(env, 0); - tb_flush(env); + /* disable single step if it was enabled */ + cpu_single_step( env, 0 ); + tb_flush( env ); - if (sig != 0) - { - snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig)); - put_packet(s, buf); + if ( sig != 0 ) { + snprintf( buf, sizeof( buf ), "S%02x", target_signal_to_gdb( sig ) ); + put_packet( s, buf ); } - /* put_packet() might have detected that the peer terminated the - connection. */ - if (s->fd < 0) - return sig; + /* put_packet() might have detected that the peer terminated the + connection. */ + if ( s->fd < 0 ) + return sig; - sig = 0; - s->state = RS_IDLE; - s->running_state = 0; - while (s->running_state == 0) { - n = read (s->fd, buf, 256); - if (n > 0) - { - int i; + sig = 0; + s->state = RS_IDLE; + s->running_state = 0; + while ( s->running_state == 0 ) { + n = read( s->fd, buf, 256 ); + if ( n > 0 ) { + int i; #ifdef DEBUG_GDB - printf("%s: read: %d\n", __FUNCTION__, n); - fflush(stdout); + printf( "%s: read: %d\n", __FUNCTION__, n ); + fflush( stdout ); #endif - for (i = 0; i < n; i++) - gdb_read_byte (s, buf[i]); + for ( i = 0; i < n; i++ ) + gdb_read_byte( s, buf[ i ] ); + } else if ( n == 0 || errno != EAGAIN ) { + /* XXX: Connection closed. Should probably wait for annother + connection before continuing. */ + gdbserver_fd = -1; + return sig; } - else if (n == 0 || errno != EAGAIN) - { - /* XXX: Connection closed. Should probably wait for annother - connection before continuing. */ - gdbserver_fd = -1; - return sig; - } - } - sig = s->signal; - s->signal = 0; - return sig; + } + sig = s->signal; + s->signal = 0; + return sig; } -int -gdb_poll (CPUState *env) +int gdb_poll( CPUState* env ) { - GDBState *s; - struct pollfd pfd; + GDBState* s; + struct pollfd pfd; - if (gdbserver_fd < 0) - return 0; + if ( gdbserver_fd < 0 ) + return 0; - s = gdbserver_state; + s = gdbserver_state; - pfd.fd = s->fd; - pfd.events = POLLIN | POLLHUP; + pfd.fd = s->fd; + pfd.events = POLLIN | POLLHUP; - if (poll(&pfd, 1, 0) <= 0) { - if (errno != EAGAIN) - return 0; - return 0; - } + if ( poll( &pfd, 1, 0 ) <= 0 ) { + if ( errno != EAGAIN ) + return 0; + return 0; + } #ifdef DEBUG_GDB - printf("%s: revents: %08x\n", __FUNCTION__, pfd.revents); - fflush(stdout); + printf( "%s: revents: %08x\n", __FUNCTION__, pfd.revents ); + fflush( stdout ); #endif - if (pfd.revents & (POLLIN | POLLHUP)) - return 1; + if ( pfd.revents & ( POLLIN | POLLHUP ) ) + return 1; - return 0; + return 0; } /* Tell the remote gdb that the process has exited. */ -void gdb_exit(CPUState *env, int code) +void gdb_exit( CPUState* env, int code ) { - GDBState *s; - char buf[4]; + GDBState* s; + char buf[ 4 ]; - s = gdbserver_state; - if (gdbserver_fd < 0 || s->fd < 0) - return; + s = gdbserver_state; + if ( gdbserver_fd < 0 || s->fd < 0 ) + return; - snprintf(buf, sizeof(buf), "W%02x", code); - put_packet(s, buf); + snprintf( buf, sizeof( buf ), "W%02x", code ); + put_packet( s, buf ); } /* Tell the remote gdb that the process has exited due to SIG. */ -void gdb_signalled(CPUState *env, int sig) +void gdb_signalled( CPUState* env, int sig ) { - GDBState *s; - char buf[4]; + GDBState* s; + char buf[ 4 ]; - s = gdbserver_state; - if (gdbserver_fd < 0 || s->fd < 0) - return; + s = gdbserver_state; + if ( gdbserver_fd < 0 || s->fd < 0 ) + return; - snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig)); - put_packet(s, buf); + snprintf( buf, sizeof( buf ), "X%02x", target_signal_to_gdb( sig ) ); + put_packet( s, buf ); } -static void gdb_accept(void) +static void gdb_accept( void ) { - GDBState *s; + GDBState* s; struct sockaddr_in sockaddr; socklen_t len; int val, fd; - for(;;) { - len = sizeof(sockaddr); - fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); - if (fd < 0 && errno != EINTR) { - perror("accept"); + for ( ;; ) { + len = sizeof( sockaddr ); + fd = accept( gdbserver_fd, ( struct sockaddr* )&sockaddr, &len ); + if ( fd < 0 && errno != EINTR ) { + perror( "accept" ); return; - } else if (fd >= 0) { + } else if ( fd >= 0 ) { #ifndef _WIN32 - fcntl(fd, F_SETFD, FD_CLOEXEC); + fcntl( fd, F_SETFD, FD_CLOEXEC ); #endif break; } @@ -1280,9 +1249,9 @@ static void gdb_accept(void) /* set short latency */ val = 1; - setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); + setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, ( char* )&val, sizeof( val ) ); - s = qemu_mallocz(sizeof(GDBState)); + s = qemu_mallocz( sizeof( GDBState ) ); s->c_cpu = first_cpu; s->g_cpu = first_cpu; s->fd = fd; @@ -1290,54 +1259,54 @@ static void gdb_accept(void) gdbserver_state = s; - fcntl(fd, F_SETFL, O_NONBLOCK); + fcntl( fd, F_SETFL, O_NONBLOCK ); /* When the debugger is connected, stop accepting connections */ /* to free the port up for other concurrent instances. */ - close(gdbserver_fd); + close( gdbserver_fd ); } -static int gdbserver_open(int port) +static int gdbserver_open( int port ) { struct sockaddr_in sockaddr; int fd, val, ret; - fd = socket(PF_INET, SOCK_STREAM, 0); - if (fd < 0) { - perror("socket"); + fd = socket( PF_INET, SOCK_STREAM, 0 ); + if ( fd < 0 ) { + perror( "socket" ); return -1; } #ifndef _WIN32 - fcntl(fd, F_SETFD, FD_CLOEXEC); + fcntl( fd, F_SETFD, FD_CLOEXEC ); #endif /* allow fast reuse */ val = 1; - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); + setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, ( char* )&val, sizeof( val ) ); sockaddr.sin_family = AF_INET; - sockaddr.sin_port = htons(port); + sockaddr.sin_port = htons( port ); sockaddr.sin_addr.s_addr = 0; - ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); - if (ret < 0) { - perror("bind"); + ret = bind( fd, ( struct sockaddr* )&sockaddr, sizeof( sockaddr ) ); + if ( ret < 0 ) { + perror( "bind" ); return -1; } - ret = listen(fd, 0); - if (ret < 0) { - perror("listen"); + ret = listen( fd, 0 ); + if ( ret < 0 ) { + perror( "listen" ); return -1; } return fd; } -int gdbserver_start(int port) +int gdbserver_start( int port ) { - if (gdbserver_fd >= 0) + if ( gdbserver_fd >= 0 ) return -1; - gdbserver_fd = gdbserver_open(port); - if (gdbserver_fd < 0) + gdbserver_fd = gdbserver_open( port ); + if ( gdbserver_fd < 0 ) return -1; /* accept connections */ gdb_accept(); @@ -1345,18 +1314,15 @@ int gdbserver_start(int port) } /* Disable gdb stub for child processes. */ -void gdbserver_fork(CPUState *env) +void gdbserver_fork( CPUState* env ) { - GDBState *s = gdbserver_state; - if (gdbserver_fd < 0 || s->fd < 0) - return; - close(s->fd); + GDBState* s = gdbserver_state; + if ( gdbserver_fd < 0 || s->fd < 0 ) + return; + close( s->fd ); s->fd = -1; - cpu_breakpoint_remove_all(env, BP_GDB); - cpu_watchpoint_remove_all(env, BP_GDB); + cpu_breakpoint_remove_all( env, BP_GDB ); + cpu_watchpoint_remove_all( env, BP_GDB ); } -int gdbserver_isactive() -{ - return (gdbserver_fd >= 0); -} +int gdbserver_isactive() { return ( gdbserver_fd >= 0 ); } diff --git a/src/gdbstub.h b/src/gdbstub.h index 4d72989..ebb7dcc 100644 --- a/src/gdbstub.h +++ b/src/gdbstub.h @@ -6,33 +6,30 @@ #define DEFAULT_GDBSTUB_PORT 1234 /* GDB breakpoint/watchpoint types */ -#define GDB_BREAKPOINT_SW 0 -#define GDB_BREAKPOINT_HW 1 -#define GDB_WATCHPOINT_WRITE 2 -#define GDB_WATCHPOINT_READ 3 -#define GDB_WATCHPOINT_ACCESS 4 +#define GDB_BREAKPOINT_SW 0 +#define GDB_BREAKPOINT_HW 1 +#define GDB_WATCHPOINT_WRITE 2 +#define GDB_WATCHPOINT_READ 3 +#define GDB_WATCHPOINT_ACCESS 4 -typedef void (*gdb_syscall_complete_cb)(CPUState *env, - target_ulong ret, target_ulong err); +typedef void ( *gdb_syscall_complete_cb )( CPUState* env, target_ulong ret, target_ulong err ); -void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...); -int use_gdb_syscalls(void); -void gdb_set_stop_cpu(CPUState *env); +void gdb_do_syscall( gdb_syscall_complete_cb cb, const char* fmt, ... ); +int use_gdb_syscalls( void ); +void gdb_set_stop_cpu( CPUState* env ); -int gdb_poll(CPUState *); +int gdb_poll( CPUState* ); -int gdb_queuesig (void); -int gdb_handlesig (CPUState *, int); -void gdb_exit(CPUState *, int); -void gdb_signalled(CPUState *, int); -int gdbserver_start(int); -void gdbserver_fork(CPUState *); +int gdb_queuesig( void ); +int gdb_handlesig( CPUState*, int ); +void gdb_exit( CPUState*, int ); +void gdb_signalled( CPUState*, int ); +int gdbserver_start( int ); +void gdbserver_fork( CPUState* ); /* Get or set a register. Returns the size of the register. */ -typedef int (*gdb_reg_cb)(CPUState *env, uint8_t *buf, int reg); -void gdb_register_coprocessor(CPUState *env, - gdb_reg_cb get_reg, gdb_reg_cb set_reg, - int num_regs, const char *xml, int g_pos); +typedef int ( *gdb_reg_cb )( CPUState* env, uint8_t* buf, int reg ); +void gdb_register_coprocessor( CPUState* env, gdb_reg_cb get_reg, gdb_reg_cb set_reg, int num_regs, const char* xml, int g_pos ); int gdbserver_isactive(); diff --git a/src/glyphname.h b/src/glyphname.h index 787d8e9..440a1c4 100644 --- a/src/glyphname.h +++ b/src/glyphname.h @@ -5,234 +5,233 @@ #define _X49GP_GLYPHNAME_H 1 typedef struct { - const char *name; - gunichar unichar; + const char* name; + gunichar unichar; } x49gp_glyph_t; -static const x49gp_glyph_t x49gp_glyphs[] = -{ - { "exclamdown", 0x00a1 }, - { "cent", 0x00a2 }, - { "sterling", 0x00a3 }, - { "fraction", 0x2044 }, - { "yen", 0x00a5 }, - { "florin", 0x0192 }, - { "section", 0x00a7 }, - { "currency", 0x00a4 }, - { "quotesingle", 0x0027 }, - { "quotedblleft", 0x201c }, - { "guillemotleft", 0x00ab }, - { "guilsinglleft", 0x2039 }, - { "guilsinglright", 0x203a }, - { "fi", 0xfb01 }, - { "fl", 0xfb02 }, - { "endash", 0x2013 }, - { "dagger", 0x2020 }, - { "daggerdbl", 0x2021 }, - { "periodcentered", 0x00b7 }, - { "paragraph", 0x00b6 }, - { "bullet", 0x2022 }, - { "quotesinglbase", 0x201a }, - { "quotedblbase", 0x201e }, - { "quotedblright", 0x201d }, - { "guillemotright", 0x00bb }, - { "ellipsis", 0x2026 }, - { "perthousand", 0x2030 }, - { "questiondown", 0x00bf }, - { "grave", 0x0060 }, - { "acute", 0x00b4 }, - { "circumflex", 0x02c6 }, - { "tilde", 0x02dc }, - { "macron", 0x00af }, - { "breve", 0x02d8 }, - { "dotaccent", 0x02d9 }, - { "dieresis", 0x00a8 }, - { "ring", 0x02da }, - { "cedilla", 0x00b8 }, - { "hungarumlaut", 0x02dd }, - { "ogonek", 0x02db }, - { "caron", 0x02c7 }, - { "emdash", 0x2014 }, - { "AE", 0x00c6 }, - { "ordfeminine", 0x00aa }, - { "Lslash", 0x0141 }, - { "Oslash", 0x00d8 }, - { "OE", 0x0152 }, - { "ordmasculine", 0x00ba }, - { "ae", 0x00e6 }, - { "dotlessi", 0x0131 }, - { "lslash", 0x0142 }, - { "oslash", 0x00f8 }, - { "oe", 0x0153 }, - { "germandbls", 0x00df }, - { "Udieresis", 0x00dc }, - { "Uacute", 0x00da }, - { "Scedilla", 0x015e }, - { "Tcaron", 0x0164 }, - { "Scaron", 0x0160 }, - { "Rcaron", 0x0158 }, - { "Racute", 0x0154 }, - { "Sacute", 0x015a }, - { "Otilde", 0x00d5 }, - { "ucircumflex", 0x00fb }, - { "Ohungarumlaut", 0x0150 }, - { "Uhungarumlaut", 0x0170 }, - { "Yacute", 0x00dd }, - { "Eth", 0x00d0 }, - { "Dcroat", 0x0110 }, - { "Zacute", 0x0179 }, - { "Uring", 0x016e }, - { "gbreve", 0x011f }, - { "eogonek", 0x0119 }, - { "edotaccent", 0x0117 }, - { "ecaron", 0x011b }, - { "Ugrave", 0x00d9 }, - { "Thorn", 0x00de }, - { "eacute", 0x00e9 }, - { "edieresis", 0x00eb }, - { "dcaron", 0x010f }, - { "ccedilla", 0x00e7 }, - { "ccaron", 0x010d }, - { "cacute", 0x0107 }, - { "aogonek", 0x0105 }, - { "aring", 0x00e5 }, - { "atilde", 0x00e3 }, - { "abreve", 0x0103 }, - { "egrave", 0x00e8 }, - { "agrave", 0x00e0 }, - { "aacute", 0x00e1 }, - { "adieresis", 0x00e4 }, - { "Uogonek", 0x0172 }, - { "ugrave", 0x00f9 }, - { "uacute", 0x00fa }, - { "udieresis", 0x00fc }, - { "tcaron", 0x0165 }, - { "scommaaccent", 0x0219 }, - { "Zcaron", 0x017d }, - { "ecircumflex", 0x00ea }, - { "Ucircumflex", 0x00db }, - { "acircumflex", 0x00e2 }, - { "Zdotaccent", 0x017b }, - { "scaron", 0x0161 }, - { "Amacron", 0x0100 }, - { "sacute", 0x015b }, - { "Tcommaaccent", 0x0162 }, - { "Ydieresis", 0x0178 }, - { "thorn", 0x00fe }, - { "Emacron", 0x0112 }, - { "Ograve", 0x00d2 }, - { "Oacute", 0x00d3 }, - { "Odieresis", 0x00d6 }, - { "Ntilde", 0x00d1 }, - { "Ncaron", 0x0147 }, - { "Nacute", 0x0143 }, - { "Lcaron", 0x013d }, - { "Lacute", 0x0139 }, - { "Idotaccent", 0x0130 }, - { "racute", 0x0155 }, - { "Icircumflex", 0x00ce }, - { "ohungarumlaut", 0x0151 }, - { "otilde", 0x00f5 }, - { "Euro", 0x20ac }, - { "ocircumflex", 0x00f4 }, - { "onesuperior", 0x00b9 }, - { "twosuperior", 0x00b2 }, - { "threesuperior", 0x00b3 }, - { "Igrave", 0x00cc }, - { "Iacute", 0x00cd }, - { "Imacron", 0x012a }, - { "Iogonek", 0x012e }, - { "Idieresis", 0x00cf }, - { "Gbreve", 0x011e }, - { "Umacron", 0x016a }, - { "Kcommaaccent", 0x0136 }, - { "ograve", 0x00f2 }, - { "Scommaaccent", 0x0218 }, - { "Eogonek", 0x0118 }, - { "oacute", 0x00f3 }, - { "Edotaccent", 0x0116 }, - { "iogonek", 0x012f }, - { "gcommaaccent", 0x0123 }, - { "odieresis", 0x00f6 }, - { "ntilde", 0x00f1 }, - { "ncaron", 0x0148 }, - { "Ecaron", 0x011a }, - { "Ecircumflex", 0x00ca }, - { "scedilla", 0x015f }, - { "rcaron", 0x0159 }, - { "Egrave", 0x00c8 }, - { "Eacute", 0x00c9 }, - { "Gcommaaccent", 0x0122 }, - { "Rcommaaccent", 0x0156 }, - { "Edieresis", 0x00cb }, - { "nacute", 0x0144 }, - { "uogonek", 0x0173 }, - { "umacron", 0x016b }, - { "Dcaron", 0x010e }, - { "lcaron", 0x013e }, - { "Ccaron", 0x010c }, - { "Cacute", 0x0106 }, - { "Ccedilla", 0x00c7 }, - { "degree", 0x00b0 }, - { "Aogonek", 0x0104 }, - { "minus", 0x2212 }, - { "multiply", 0x00d7 }, - { "divide", 0x00f7 }, - { "Aring", 0x00c5 }, - { "trademark", 0x2122 }, - { "rcommaaccent", 0x0157 }, - { "lacute", 0x013a }, - { "omacron", 0x014d }, - { "Atilde", 0x00c3 }, - { "icircumflex", 0x00ee }, - { "igrave", 0x00ec }, - { "ncommaaccent", 0x0146 }, - { "lcommaaccent", 0x013c }, - { "plusminus", 0x00b1 }, - { "onehalf", 0x00bd }, - { "onequarter", 0x00bc }, - { "threequarters", 0x00be }, - { "iacute", 0x00ed }, - { "Abreve", 0x0102 }, - { "kcommaaccent", 0x0137 }, - { "Omacron", 0x014c }, - { "imacron", 0x012b }, - { "emacron", 0x0113 }, - { "amacron", 0x0101 }, - { "tcommaaccent", 0x0163 }, - { "ydieresis", 0x00ff }, - { "zdotaccent", 0x017c }, - { "zcaron", 0x017e }, - { "zacute", 0x017a }, - { "yacute", 0x00fd }, - { "uhungarumlaut", 0x0171 }, - { "eth", 0x00f0 }, - { "uring", 0x016f }, - { "Ocircumflex", 0x00d4 }, - { "commaaccent", 0xf6c3 }, - { "copyright", 0x00a9 }, - { "registered", 0x00ae }, - { "Acircumflex", 0x00c2 }, - { "idieresis", 0x00ef }, - { "lozenge", 0x25ca }, - { "Delta", 0x2206 }, - { "notequal", 0x2260 }, - { "radical", 0x221a }, - { "Agrave", 0x00c0 }, - { "Aacute", 0x00c1 }, - { "lessequal", 0x2264 }, - { "greaterequal", 0x2265 }, - { "logicalnot", 0x00ac }, - { "summation", 0x2211 }, - { "partialdiff", 0x2202 }, - { "Ncommaaccent", 0x0145 }, - { "dcroat", 0x0111 }, - { "brokenbar", 0x00a6 }, - { "Lcommaaccent", 0x013b }, - { "Adieresis", 0x00c4 }, - { "mu", 0x00b5 } +static const x49gp_glyph_t x49gp_glyphs[] = { + {"exclamdown", 0x00a1}, + {"cent", 0x00a2}, + {"sterling", 0x00a3}, + {"fraction", 0x2044}, + {"yen", 0x00a5}, + {"florin", 0x0192}, + {"section", 0x00a7}, + {"currency", 0x00a4}, + {"quotesingle", 0x0027}, + {"quotedblleft", 0x201c}, + {"guillemotleft", 0x00ab}, + {"guilsinglleft", 0x2039}, + {"guilsinglright", 0x203a}, + {"fi", 0xfb01}, + {"fl", 0xfb02}, + {"endash", 0x2013}, + {"dagger", 0x2020}, + {"daggerdbl", 0x2021}, + {"periodcentered", 0x00b7}, + {"paragraph", 0x00b6}, + {"bullet", 0x2022}, + {"quotesinglbase", 0x201a}, + {"quotedblbase", 0x201e}, + {"quotedblright", 0x201d}, + {"guillemotright", 0x00bb}, + {"ellipsis", 0x2026}, + {"perthousand", 0x2030}, + {"questiondown", 0x00bf}, + {"grave", 0x0060}, + {"acute", 0x00b4}, + {"circumflex", 0x02c6}, + {"tilde", 0x02dc}, + {"macron", 0x00af}, + {"breve", 0x02d8}, + {"dotaccent", 0x02d9}, + {"dieresis", 0x00a8}, + {"ring", 0x02da}, + {"cedilla", 0x00b8}, + {"hungarumlaut", 0x02dd}, + {"ogonek", 0x02db}, + {"caron", 0x02c7}, + {"emdash", 0x2014}, + {"AE", 0x00c6}, + {"ordfeminine", 0x00aa}, + {"Lslash", 0x0141}, + {"Oslash", 0x00d8}, + {"OE", 0x0152}, + {"ordmasculine", 0x00ba}, + {"ae", 0x00e6}, + {"dotlessi", 0x0131}, + {"lslash", 0x0142}, + {"oslash", 0x00f8}, + {"oe", 0x0153}, + {"germandbls", 0x00df}, + {"Udieresis", 0x00dc}, + {"Uacute", 0x00da}, + {"Scedilla", 0x015e}, + {"Tcaron", 0x0164}, + {"Scaron", 0x0160}, + {"Rcaron", 0x0158}, + {"Racute", 0x0154}, + {"Sacute", 0x015a}, + {"Otilde", 0x00d5}, + {"ucircumflex", 0x00fb}, + {"Ohungarumlaut", 0x0150}, + {"Uhungarumlaut", 0x0170}, + {"Yacute", 0x00dd}, + {"Eth", 0x00d0}, + {"Dcroat", 0x0110}, + {"Zacute", 0x0179}, + {"Uring", 0x016e}, + {"gbreve", 0x011f}, + {"eogonek", 0x0119}, + {"edotaccent", 0x0117}, + {"ecaron", 0x011b}, + {"Ugrave", 0x00d9}, + {"Thorn", 0x00de}, + {"eacute", 0x00e9}, + {"edieresis", 0x00eb}, + {"dcaron", 0x010f}, + {"ccedilla", 0x00e7}, + {"ccaron", 0x010d}, + {"cacute", 0x0107}, + {"aogonek", 0x0105}, + {"aring", 0x00e5}, + {"atilde", 0x00e3}, + {"abreve", 0x0103}, + {"egrave", 0x00e8}, + {"agrave", 0x00e0}, + {"aacute", 0x00e1}, + {"adieresis", 0x00e4}, + {"Uogonek", 0x0172}, + {"ugrave", 0x00f9}, + {"uacute", 0x00fa}, + {"udieresis", 0x00fc}, + {"tcaron", 0x0165}, + {"scommaaccent", 0x0219}, + {"Zcaron", 0x017d}, + {"ecircumflex", 0x00ea}, + {"Ucircumflex", 0x00db}, + {"acircumflex", 0x00e2}, + {"Zdotaccent", 0x017b}, + {"scaron", 0x0161}, + {"Amacron", 0x0100}, + {"sacute", 0x015b}, + {"Tcommaaccent", 0x0162}, + {"Ydieresis", 0x0178}, + {"thorn", 0x00fe}, + {"Emacron", 0x0112}, + {"Ograve", 0x00d2}, + {"Oacute", 0x00d3}, + {"Odieresis", 0x00d6}, + {"Ntilde", 0x00d1}, + {"Ncaron", 0x0147}, + {"Nacute", 0x0143}, + {"Lcaron", 0x013d}, + {"Lacute", 0x0139}, + {"Idotaccent", 0x0130}, + {"racute", 0x0155}, + {"Icircumflex", 0x00ce}, + {"ohungarumlaut", 0x0151}, + {"otilde", 0x00f5}, + {"Euro", 0x20ac}, + {"ocircumflex", 0x00f4}, + {"onesuperior", 0x00b9}, + {"twosuperior", 0x00b2}, + {"threesuperior", 0x00b3}, + {"Igrave", 0x00cc}, + {"Iacute", 0x00cd}, + {"Imacron", 0x012a}, + {"Iogonek", 0x012e}, + {"Idieresis", 0x00cf}, + {"Gbreve", 0x011e}, + {"Umacron", 0x016a}, + {"Kcommaaccent", 0x0136}, + {"ograve", 0x00f2}, + {"Scommaaccent", 0x0218}, + {"Eogonek", 0x0118}, + {"oacute", 0x00f3}, + {"Edotaccent", 0x0116}, + {"iogonek", 0x012f}, + {"gcommaaccent", 0x0123}, + {"odieresis", 0x00f6}, + {"ntilde", 0x00f1}, + {"ncaron", 0x0148}, + {"Ecaron", 0x011a}, + {"Ecircumflex", 0x00ca}, + {"scedilla", 0x015f}, + {"rcaron", 0x0159}, + {"Egrave", 0x00c8}, + {"Eacute", 0x00c9}, + {"Gcommaaccent", 0x0122}, + {"Rcommaaccent", 0x0156}, + {"Edieresis", 0x00cb}, + {"nacute", 0x0144}, + {"uogonek", 0x0173}, + {"umacron", 0x016b}, + {"Dcaron", 0x010e}, + {"lcaron", 0x013e}, + {"Ccaron", 0x010c}, + {"Cacute", 0x0106}, + {"Ccedilla", 0x00c7}, + {"degree", 0x00b0}, + {"Aogonek", 0x0104}, + {"minus", 0x2212}, + {"multiply", 0x00d7}, + {"divide", 0x00f7}, + {"Aring", 0x00c5}, + {"trademark", 0x2122}, + {"rcommaaccent", 0x0157}, + {"lacute", 0x013a}, + {"omacron", 0x014d}, + {"Atilde", 0x00c3}, + {"icircumflex", 0x00ee}, + {"igrave", 0x00ec}, + {"ncommaaccent", 0x0146}, + {"lcommaaccent", 0x013c}, + {"plusminus", 0x00b1}, + {"onehalf", 0x00bd}, + {"onequarter", 0x00bc}, + {"threequarters", 0x00be}, + {"iacute", 0x00ed}, + {"Abreve", 0x0102}, + {"kcommaaccent", 0x0137}, + {"Omacron", 0x014c}, + {"imacron", 0x012b}, + {"emacron", 0x0113}, + {"amacron", 0x0101}, + {"tcommaaccent", 0x0163}, + {"ydieresis", 0x00ff}, + {"zdotaccent", 0x017c}, + {"zcaron", 0x017e}, + {"zacute", 0x017a}, + {"yacute", 0x00fd}, + {"uhungarumlaut", 0x0171}, + {"eth", 0x00f0}, + {"uring", 0x016f}, + {"Ocircumflex", 0x00d4}, + {"commaaccent", 0xf6c3}, + {"copyright", 0x00a9}, + {"registered", 0x00ae}, + {"Acircumflex", 0x00c2}, + {"idieresis", 0x00ef}, + {"lozenge", 0x25ca}, + {"Delta", 0x2206}, + {"notequal", 0x2260}, + {"radical", 0x221a}, + {"Agrave", 0x00c0}, + {"Aacute", 0x00c1}, + {"lessequal", 0x2264}, + {"greaterequal", 0x2265}, + {"logicalnot", 0x00ac}, + {"summation", 0x2211}, + {"partialdiff", 0x2202}, + {"Ncommaaccent", 0x0145}, + {"dcroat", 0x0111}, + {"brokenbar", 0x00a6}, + {"Lcommaaccent", 0x013b}, + {"Adieresis", 0x00c4}, + {"mu", 0x00b5} }; -#define NR_GLYPHNAMES (sizeof(x49gp_glyphs) / sizeof(x49gp_glyphs[0])) +#define NR_GLYPHNAMES ( sizeof( x49gp_glyphs ) / sizeof( x49gp_glyphs[ 0 ] ) ) #endif /* !(_X49GP_GLYPHNAME_H) */ diff --git a/src/hex2bin.c b/src/hex2bin.c index fed518b..df45a61 100644 --- a/src/hex2bin.c +++ b/src/hex2bin.c @@ -26,92 +26,89 @@ #include #include -int -main(int argc, char **argv) +int main( int argc, char** argv ) { - unsigned char *input, *p; - unsigned char *memory = NULL; - size_t size; - int in, out; - int i; + unsigned char *input, *p; + unsigned char* memory = NULL; + size_t size; + int in, out; + int i; - if (argc < 3) { - fprintf(stderr, "usage: %s \n", argv[0]); - exit(1); - } + if ( argc < 3 ) { + fprintf( stderr, "usage: %s \n", argv[ 0 ] ); + exit( 1 ); + } - if (!strcmp(argv[1], "-")) - in = 0; - else { - in = open(argv[1], O_RDONLY); - if (in < 0) { - perror(argv[1]); - exit(1); - } - } + if ( !strcmp( argv[ 1 ], "-" ) ) + in = 0; + else { + in = open( argv[ 1 ], O_RDONLY ); + if ( in < 0 ) { + perror( argv[ 1 ] ); + exit( 1 ); + } + } - if (!strcmp(argv[2], "-")) - out = 1; - else { - out = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666); - if (out < 0) { - perror(argv[2]); - exit(1); - } - } + if ( !strcmp( argv[ 2 ], "-" ) ) + out = 1; + else { + out = open( argv[ 2 ], O_WRONLY | O_CREAT | O_TRUNC, 0666 ); + if ( out < 0 ) { + perror( argv[ 2 ] ); + exit( 1 ); + } + } - size = lseek(in, 0, SEEK_END); - lseek(in, 0, SEEK_SET); + size = lseek( in, 0, SEEK_END ); + lseek( in, 0, SEEK_SET ); - input = (unsigned char *)malloc(size); - if (!input) { - fprintf(stderr, "%s: out of memory\n", argv[0]); - exit(1); - } + input = ( unsigned char* )malloc( size ); + if ( !input ) { + fprintf( stderr, "%s: out of memory\n", argv[ 0 ] ); + exit( 1 ); + } - if (read(in, input, size) != size) { - perror("read"); - exit(1); - } + if ( read( in, input, size ) != size ) { + perror( "read" ); + exit( 1 ); + } - close(in); + close( in ); - memory = malloc(size >> 1); - if (!memory) { - fprintf(stderr, "%s: out of memory\n", argv[0]); - exit(1); - } + memory = malloc( size >> 1 ); + if ( !memory ) { + fprintf( stderr, "%s: out of memory\n", argv[ 0 ] ); + exit( 1 ); + } - p = input; - for (i = 0; i < (size >> 1); i++) { - if ('0' <= *p && *p <= '9') - memory[i] = (*p - '0') << 0; - else if ('a' <= *p && *p <= 'f') - memory[i] = (*p - 'a' + 10) << 0; - else if ('A' <= *p && *p <= 'F') - memory[i] = (*p - 'A' + 10) << 0; - else { - fprintf(stderr, "%s: parse error at byte %d\n", - argv[0], i); - exit(1); - } - p++; - if ('0' <= *p && *p <= '9') - memory[i] |= (*p - '0') << 4; - else if ('a' <= *p && *p <= 'f') - memory[i] |= (*p - 'a' + 10) << 4; - else if ('A' <= *p && *p <= 'F') - memory[i] |= (*p - 'A' + 10) << 4; - else { - fprintf(stderr, "%s: parse error at byte %d\n", - argv[0], i); - exit(1); - } - p++; - } + p = input; + for ( i = 0; i < ( size >> 1 ); i++ ) { + if ( '0' <= *p && *p <= '9' ) + memory[ i ] = ( *p - '0' ) << 0; + else if ( 'a' <= *p && *p <= 'f' ) + memory[ i ] = ( *p - 'a' + 10 ) << 0; + else if ( 'A' <= *p && *p <= 'F' ) + memory[ i ] = ( *p - 'A' + 10 ) << 0; + else { + fprintf( stderr, "%s: parse error at byte %d\n", argv[ 0 ], i ); + exit( 1 ); + } + p++; + if ( '0' <= *p && *p <= '9' ) + memory[ i ] |= ( *p - '0' ) << 4; + else if ( 'a' <= *p && *p <= 'f' ) + memory[ i ] |= ( *p - 'a' + 10 ) << 4; + else if ( 'A' <= *p && *p <= 'F' ) + memory[ i ] |= ( *p - 'A' + 10 ) << 4; + else { + fprintf( stderr, "%s: parse error at byte %d\n", argv[ 0 ], i ); + exit( 1 ); + } + p++; + } - write(out, memory, size >> 1); - close(out); + write( out, memory, size >> 1 ); + close( out ); - return 0; + return 0; } diff --git a/src/le32hex2bin.c b/src/le32hex2bin.c index 2739c60..167acbd 100644 --- a/src/le32hex2bin.c +++ b/src/le32hex2bin.c @@ -26,92 +26,89 @@ #include #include -int -main(int argc, char **argv) +int main( int argc, char** argv ) { - unsigned char *input, *p; - unsigned char *memory = NULL; - size_t size; - int in, out; - int i; + unsigned char *input, *p; + unsigned char* memory = NULL; + size_t size; + int in, out; + int i; - if (argc < 3) { - fprintf(stderr, "usage: %s \n", argv[0]); - exit(1); - } + if ( argc < 3 ) { + fprintf( stderr, "usage: %s \n", argv[ 0 ] ); + exit( 1 ); + } - if (!strcmp(argv[1], "-")) - in = 0; - else { - in = open(argv[1], O_RDONLY); - if (in < 0) { - perror(argv[1]); - exit(1); - } - } + if ( !strcmp( argv[ 1 ], "-" ) ) + in = 0; + else { + in = open( argv[ 1 ], O_RDONLY ); + if ( in < 0 ) { + perror( argv[ 1 ] ); + exit( 1 ); + } + } - if (!strcmp(argv[2], "-")) - out = 1; - else { - out = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666); - if (out < 0) { - perror(argv[2]); - exit(1); - } - } + if ( !strcmp( argv[ 2 ], "-" ) ) + out = 1; + else { + out = open( argv[ 2 ], O_WRONLY | O_CREAT | O_TRUNC, 0666 ); + if ( out < 0 ) { + perror( argv[ 2 ] ); + exit( 1 ); + } + } - size = lseek(in, 0, SEEK_END); - lseek(in, 0, SEEK_SET); + size = lseek( in, 0, SEEK_END ); + lseek( in, 0, SEEK_SET ); - input = (unsigned char *)malloc(size); - if (!input) { - fprintf(stderr, "%s: out of memory\n", argv[0]); - exit(1); - } + input = ( unsigned char* )malloc( size ); + if ( !input ) { + fprintf( stderr, "%s: out of memory\n", argv[ 0 ] ); + exit( 1 ); + } - if (read(in, input, size) != size) { - perror("read"); - exit(1); - } + if ( read( in, input, size ) != size ) { + perror( "read" ); + exit( 1 ); + } - close(in); + close( in ); - memory = malloc(size >> 1); - if (!memory) { - fprintf(stderr, "%s: out of memory\n", argv[0]); - exit(1); - } + memory = malloc( size >> 1 ); + if ( !memory ) { + fprintf( stderr, "%s: out of memory\n", argv[ 0 ] ); + exit( 1 ); + } - p = input; - for (i = 0; i < (size >> 1); i++) { - if ('0' <= *p && *p <= '9') - memory[(i & ~3) + 3 - (i & 3)] = (*p - '0') << 0; - else if ('a' <= *p && *p <= 'f') - memory[(i & ~3) + 3 - (i & 3)] = (*p - 'a' + 10) << 0; - else if ('A' <= *p && *p <= 'F') - memory[(i & ~3) + 3 - (i & 3)] = (*p - 'A' + 10) << 0; - else { - fprintf(stderr, "%s: parse error at byte %d\n", - argv[0], i); - exit(1); - } - p++; - if ('0' <= *p && *p <= '9') - memory[(i & ~3) + 3 - (i & 3)] |= (*p - '0') << 4; - else if ('a' <= *p && *p <= 'f') - memory[(i & ~3) + 3 - (i & 3)] |= (*p - 'a' + 10) << 4; - else if ('A' <= *p && *p <= 'F') - memory[(i & ~3) + 3 - (i & 3)] |= (*p - 'A' + 10) << 4; - else { - fprintf(stderr, "%s: parse error at byte %d\n", - argv[0], i); - exit(1); - } - p++; - } + p = input; + for ( i = 0; i < ( size >> 1 ); i++ ) { + if ( '0' <= *p && *p <= '9' ) + memory[ ( i & ~3 ) + 3 - ( i & 3 ) ] = ( *p - '0' ) << 0; + else if ( 'a' <= *p && *p <= 'f' ) + memory[ ( i & ~3 ) + 3 - ( i & 3 ) ] = ( *p - 'a' + 10 ) << 0; + else if ( 'A' <= *p && *p <= 'F' ) + memory[ ( i & ~3 ) + 3 - ( i & 3 ) ] = ( *p - 'A' + 10 ) << 0; + else { + fprintf( stderr, "%s: parse error at byte %d\n", argv[ 0 ], i ); + exit( 1 ); + } + p++; + if ( '0' <= *p && *p <= '9' ) + memory[ ( i & ~3 ) + 3 - ( i & 3 ) ] |= ( *p - '0' ) << 4; + else if ( 'a' <= *p && *p <= 'f' ) + memory[ ( i & ~3 ) + 3 - ( i & 3 ) ] |= ( *p - 'a' + 10 ) << 4; + else if ( 'A' <= *p && *p <= 'F' ) + memory[ ( i & ~3 ) + 3 - ( i & 3 ) ] |= ( *p - 'A' + 10 ) << 4; + else { + fprintf( stderr, "%s: parse error at byte %d\n", argv[ 0 ], i ); + exit( 1 ); + } + p++; + } - write(out, memory, size >> 1); - close(out); + write( out, memory, size >> 1 ); + close( out ); - return 0; + return 0; } diff --git a/src/list.h b/src/list.h index c971ae0..4ff380e 100644 --- a/src/list.h +++ b/src/list.h @@ -3,10 +3,10 @@ #include -static inline void prefetch(const void *x) {;} +static inline void prefetch( const void* x ) { ; } #ifndef offsetof -#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +# define offsetof( TYPE, MEMBER ) ( ( size_t )&( ( TYPE* )0 )->MEMBER ) #endif /* @@ -14,8 +14,8 @@ static inline void prefetch(const void *x) {;} * under normal circumstances, used to verify that nobody uses * non-initialized list entries. */ -#define LIST_POISON1 ((void *) 0x00100100) -#define LIST_POISON2 ((void *) 0x00200200) +#define LIST_POISON1 ( ( void* )0x00100100 ) +#define LIST_POISON2 ( ( void* )0x00200200 ) /* * Simple doubly linked list implementation. @@ -28,17 +28,18 @@ static inline void prefetch(const void *x) {;} */ struct list_head { - struct list_head *next, *prev; + struct list_head *next, *prev; }; -#define LIST_HEAD_INIT(name) { &(name), &(name) } +#define LIST_HEAD_INIT( name ) { &( name ), &( name ) } -#define LIST_HEAD(name) \ - struct list_head name = LIST_HEAD_INIT(name) +#define LIST_HEAD( name ) struct list_head name = LIST_HEAD_INIT( name ) -#define INIT_LIST_HEAD(ptr) do { \ - (ptr)->next = (ptr); (ptr)->prev = (ptr); \ -} while (0) +#define INIT_LIST_HEAD( ptr ) \ + do { \ + ( ptr )->next = ( ptr ); \ + ( ptr )->prev = ( ptr ); \ + } while ( 0 ) /* * Insert a new entry between two known consecutive entries. @@ -46,14 +47,12 @@ struct list_head { * This is only for internal list manipulation where we know * the prev/next entries already! */ -static inline void __list_add(struct list_head *new, - struct list_head *prev, - struct list_head *next) +static inline void __list_add( struct list_head* new, struct list_head* prev, struct list_head* next ) { - next->prev = new; - new->next = next; - new->prev = prev; - prev->next = new; + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; } /** @@ -64,10 +63,7 @@ static inline void __list_add(struct list_head *new, * Insert a new entry after the specified head. * This is good for implementing stacks. */ -static inline void list_add(struct list_head *new, struct list_head *head) -{ - __list_add(new, head, head->next); -} +static inline void list_add( struct list_head* new, struct list_head* head ) { __list_add( new, head, head->next ); } /** * list_add_tail - add a new entry @@ -77,10 +73,7 @@ static inline void list_add(struct list_head *new, struct list_head *head) * Insert a new entry before the specified head. * This is useful for implementing queues. */ -static inline void list_add_tail(struct list_head *new, struct list_head *head) -{ - __list_add(new, head->prev, head); -} +static inline void list_add_tail( struct list_head* new, struct list_head* head ) { __list_add( new, head->prev, head ); } /* * Delete a list entry by making the prev/next entries @@ -89,10 +82,10 @@ static inline void list_add_tail(struct list_head *new, struct list_head *head) * This is only for internal list manipulation where we know * the prev/next entries already! */ -static inline void __list_del(struct list_head * prev, struct list_head * next) +static inline void __list_del( struct list_head* prev, struct list_head* next ) { - next->prev = prev; - prev->next = next; + next->prev = prev; + prev->next = next; } /** @@ -101,21 +94,21 @@ static inline void __list_del(struct list_head * prev, struct list_head * next) * Note: list_empty on entry does not return true after this, the entry is * in an undefined state. */ -static inline void list_del(struct list_head *entry) +static inline void list_del( struct list_head* entry ) { - __list_del(entry->prev, entry->next); - entry->next = LIST_POISON1; - entry->prev = LIST_POISON2; + __list_del( entry->prev, entry->next ); + entry->next = LIST_POISON1; + entry->prev = LIST_POISON2; } /** * list_del_init - deletes entry from list and reinitialize it. * @entry: the element to delete from the list. */ -static inline void list_del_init(struct list_head *entry) +static inline void list_del_init( struct list_head* entry ) { - __list_del(entry->prev, entry->next); - INIT_LIST_HEAD(entry); + __list_del( entry->prev, entry->next ); + INIT_LIST_HEAD( entry ); } /** @@ -123,10 +116,10 @@ static inline void list_del_init(struct list_head *entry) * @list: the entry to move * @head: the head that will precede our entry */ -static inline void list_move(struct list_head *list, struct list_head *head) +static inline void list_move( struct list_head* list, struct list_head* head ) { - __list_del(list->prev, list->next); - list_add(list, head); + __list_del( list->prev, list->next ); + list_add( list, head ); } /** @@ -134,21 +127,17 @@ static inline void list_move(struct list_head *list, struct list_head *head) * @list: the entry to move * @head: the head that will follow our entry */ -static inline void list_move_tail(struct list_head *list, - struct list_head *head) +static inline void list_move_tail( struct list_head* list, struct list_head* head ) { - __list_del(list->prev, list->next); - list_add_tail(list, head); + __list_del( list->prev, list->next ); + list_add_tail( list, head ); } /** * list_empty - tests whether a list is empty * @head: the list to test. */ -static inline int list_empty(const struct list_head *head) -{ - return head->next == head; -} +static inline int list_empty( const struct list_head* head ) { return head->next == head; } /** * list_empty_careful - tests whether a list is @@ -162,24 +151,23 @@ static inline int list_empty(const struct list_head *head) * * @head: the list to test. */ -static inline int list_empty_careful(const struct list_head *head) +static inline int list_empty_careful( const struct list_head* head ) { - struct list_head *next = head->next; - return (next == head) && (next == head->prev); + struct list_head* next = head->next; + return ( next == head ) && ( next == head->prev ); } -static inline void __list_splice(struct list_head *list, - struct list_head *head) +static inline void __list_splice( struct list_head* list, struct list_head* head ) { - struct list_head *first = list->next; - struct list_head *last = list->prev; - struct list_head *at = head->next; + struct list_head* first = list->next; + struct list_head* last = list->prev; + struct list_head* at = head->next; - first->prev = head; - head->next = first; + first->prev = head; + head->next = first; - last->next = at; - at->prev = last; + last->next = at; + at->prev = last; } /** @@ -187,10 +175,10 @@ static inline void __list_splice(struct list_head *list, * @list: the new list to add. * @head: the place to add it in the first list. */ -static inline void list_splice(struct list_head *list, struct list_head *head) +static inline void list_splice( struct list_head* list, struct list_head* head ) { - if (!list_empty(list)) - __list_splice(list, head); + if ( !list_empty( list ) ) + __list_splice( list, head ); } /** @@ -200,13 +188,12 @@ static inline void list_splice(struct list_head *list, struct list_head *head) * * The list at @list is reinitialised */ -static inline void list_splice_init(struct list_head *list, - struct list_head *head) +static inline void list_splice_init( struct list_head* list, struct list_head* head ) { - if (!list_empty(list)) { - __list_splice(list, head); - INIT_LIST_HEAD(list); - } + if ( !list_empty( list ) ) { + __list_splice( list, head ); + INIT_LIST_HEAD( list ); + } } /** @@ -215,17 +202,14 @@ static inline void list_splice_init(struct list_head *list, * @type: the type of the struct this is embedded in. * @member: the name of the list_struct within the struct. */ -#define list_entry(ptr, type, member) \ - container_of(ptr, type, member) +#define list_entry( ptr, type, member ) container_of( ptr, type, member ) /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop counter. * @head: the head for your list. */ -#define list_for_each(pos, head) \ - for (pos = (head)->next; prefetch(pos->next), pos != (head); \ - pos = pos->next) +#define list_for_each( pos, head ) for ( pos = ( head )->next; prefetch( pos->next ), pos != ( head ); pos = pos->next ) /** * __list_for_each - iterate over a list @@ -237,17 +221,14 @@ static inline void list_splice_init(struct list_head *list, * Use this for code that knows the list to be very short (empty * or 1 entry) most of the time. */ -#define __list_for_each(pos, head) \ - for (pos = (head)->next; pos != (head); pos = pos->next) +#define __list_for_each( pos, head ) for ( pos = ( head )->next; pos != ( head ); pos = pos->next ) /** * list_for_each_prev - iterate over a list backwards * @pos: the &struct list_head to use as a loop counter. * @head: the head for your list. */ -#define list_for_each_prev(pos, head) \ - for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ - pos = pos->prev) +#define list_for_each_prev( pos, head ) for ( pos = ( head )->prev; prefetch( pos->prev ), pos != ( head ); pos = pos->prev ) /** * list_for_each_safe - iterate over a list safe against removal of list entry @@ -255,9 +236,7 @@ static inline void list_splice_init(struct list_head *list, * @n: another &struct list_head to use as temporary storage * @head: the head for your list. */ -#define list_for_each_safe(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); \ - pos = n, n = pos->next) +#define list_for_each_safe( pos, n, head ) for ( pos = ( head )->next, n = pos->next; pos != ( head ); pos = n, n = pos->next ) /** * list_for_each_entry - iterate over list of given type @@ -265,10 +244,9 @@ static inline void list_splice_init(struct list_head *list, * @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry(pos, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member); \ - prefetch(pos->member.next), &pos->member != (head); \ - pos = list_entry(pos->member.next, typeof(*pos), member)) +#define list_for_each_entry( pos, head, member ) \ + for ( pos = list_entry( ( head )->next, typeof( *pos ), member ); prefetch( pos->member.next ), &pos->member != ( head ); \ + pos = list_entry( pos->member.next, typeof( *pos ), member ) ) /** * list_for_each_entry_reverse - iterate backwards over list of given type. @@ -276,10 +254,9 @@ static inline void list_splice_init(struct list_head *list, * @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_reverse(pos, head, member) \ - for (pos = list_entry((head)->prev, typeof(*pos), member); \ - prefetch(pos->member.prev), &pos->member != (head); \ - pos = list_entry(pos->member.prev, typeof(*pos), member)) +#define list_for_each_entry_reverse( pos, head, member ) \ + for ( pos = list_entry( ( head )->prev, typeof( *pos ), member ); prefetch( pos->member.prev ), &pos->member != ( head ); \ + pos = list_entry( pos->member.prev, typeof( *pos ), member ) ) /** * list_prepare_entry - prepare a pos entry for use as a start point in @@ -288,8 +265,7 @@ static inline void list_splice_init(struct list_head *list, * @head: the head of the list * @member: the name of the list_struct within the struct. */ -#define list_prepare_entry(pos, head, member) \ - ((pos) ? : list_entry(head, typeof(*pos), member)) +#define list_prepare_entry( pos, head, member ) ( ( pos ) ?: list_entry( head, typeof( *pos ), member ) ) /** * list_for_each_entry_continue - iterate over list of given type @@ -298,10 +274,9 @@ static inline void list_splice_init(struct list_head *list, * @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_continue(pos, head, member) \ - for (pos = list_entry(pos->member.next, typeof(*pos), member); \ - prefetch(pos->member.next), &pos->member != (head); \ - pos = list_entry(pos->member.next, typeof(*pos), member)) +#define list_for_each_entry_continue( pos, head, member ) \ + for ( pos = list_entry( pos->member.next, typeof( *pos ), member ); prefetch( pos->member.next ), &pos->member != ( head ); \ + pos = list_entry( pos->member.next, typeof( *pos ), member ) ) /** * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry @@ -310,11 +285,9 @@ static inline void list_splice_init(struct list_head *list, * @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe(pos, n, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member), \ - n = list_entry(pos->member.next, typeof(*pos), member); \ - &pos->member != (head); \ - pos = n, n = list_entry(n->member.next, typeof(*n), member)) +#define list_for_each_entry_safe( pos, n, head, member ) \ + for ( pos = list_entry( ( head )->next, typeof( *pos ), member ), n = list_entry( pos->member.next, typeof( *pos ), member ); \ + &pos->member != ( head ); pos = n, n = list_entry( n->member.next, typeof( *n ), member ) ) /** * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against removal of list entry. @@ -323,11 +296,9 @@ static inline void list_splice_init(struct list_head *list, * @head: the head for your list. * @member: the name of the list_struct within the struct. */ -#define list_for_each_entry_safe_reverse(pos, n, head, member) \ - for (pos = list_entry((head)->prev, typeof(*pos), member), \ - n = list_entry(pos->member.prev, typeof(*pos), member); \ - prefetch(pos->member.prev), &pos->member != (head); \ - pos = n, n = list_entry(n->member.prev, typeof(*n), member)) +#define list_for_each_entry_safe_reverse( pos, n, head, member ) \ + for ( pos = list_entry( ( head )->prev, typeof( *pos ), member ), n = list_entry( pos->member.prev, typeof( *pos ), member ); \ + prefetch( pos->member.prev ), &pos->member != ( head ); pos = n, n = list_entry( n->member.prev, typeof( *n ), member ) ) /** * list_for_each_rcu - iterate over an rcu-protected list @@ -338,13 +309,10 @@ static inline void list_splice_init(struct list_head *list, * the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_rcu(pos, head) \ - for (pos = (head)->next; prefetch(pos->next), pos != (head); \ - pos = rcu_dereference(pos->next)) +#define list_for_each_rcu( pos, head ) \ + for ( pos = ( head )->next; prefetch( pos->next ), pos != ( head ); pos = rcu_dereference( pos->next ) ) -#define __list_for_each_rcu(pos, head) \ - for (pos = (head)->next; pos != (head); \ - pos = rcu_dereference(pos->next)) +#define __list_for_each_rcu( pos, head ) for ( pos = ( head )->next; pos != ( head ); pos = rcu_dereference( pos->next ) ) /** * list_for_each_safe_rcu - iterate over an rcu-protected list safe @@ -357,9 +325,8 @@ static inline void list_splice_init(struct list_head *list, * the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_safe_rcu(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); \ - pos = rcu_dereference(n), n = pos->next) +#define list_for_each_safe_rcu( pos, n, head ) \ + for ( pos = ( head )->next, n = pos->next; pos != ( head ); pos = rcu_dereference( n ), n = pos->next ) /** * list_for_each_entry_rcu - iterate over rcu list of given type @@ -371,12 +338,9 @@ static inline void list_splice_init(struct list_head *list, * the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_entry_rcu(pos, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member); \ - prefetch(pos->member.next), &pos->member != (head); \ - pos = rcu_dereference(list_entry(pos->member.next, \ - typeof(*pos), member))) - +#define list_for_each_entry_rcu( pos, head, member ) \ + for ( pos = list_entry( ( head )->next, typeof( *pos ), member ); prefetch( pos->member.next ), &pos->member != ( head ); \ + pos = rcu_dereference( list_entry( pos->member.next, typeof( *pos ), member ) ) ) /** * list_for_each_continue_rcu - iterate over an rcu-protected list @@ -388,9 +352,8 @@ static inline void list_splice_init(struct list_head *list, * the _rcu list-mutation primitives such as list_add_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define list_for_each_continue_rcu(pos, head) \ - for ((pos) = (pos)->next; prefetch((pos)->next), (pos) != (head); \ - (pos) = rcu_dereference((pos)->next)) +#define list_for_each_continue_rcu( pos, head ) \ + for ( ( pos ) = ( pos )->next; prefetch( ( pos )->next ), ( pos ) != ( head ); ( pos ) = rcu_dereference( ( pos )->next ) ) /* * Double linked lists with a single pointer list head. @@ -400,42 +363,36 @@ static inline void list_splice_init(struct list_head *list, */ struct hlist_head { - struct hlist_node *first; + struct hlist_node* first; }; struct hlist_node { - struct hlist_node *next, **pprev; + struct hlist_node *next, **pprev; }; #define HLIST_HEAD_INIT { .first = NULL } -#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } -#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) -#define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL) +#define HLIST_HEAD( name ) struct hlist_head name = { .first = NULL } +#define INIT_HLIST_HEAD( ptr ) ( ( ptr )->first = NULL ) +#define INIT_HLIST_NODE( ptr ) ( ( ptr )->next = NULL, ( ptr )->pprev = NULL ) -static inline int hlist_unhashed(const struct hlist_node *h) +static inline int hlist_unhashed( const struct hlist_node* h ) { return !h->pprev; } + +static inline int hlist_empty( const struct hlist_head* h ) { return !h->first; } + +static inline void __hlist_del( struct hlist_node* n ) { - return !h->pprev; + struct hlist_node* next = n->next; + struct hlist_node** pprev = n->pprev; + *pprev = next; + if ( next ) + next->pprev = pprev; } -static inline int hlist_empty(const struct hlist_head *h) +static inline void hlist_del( struct hlist_node* n ) { - return !h->first; -} - -static inline void __hlist_del(struct hlist_node *n) -{ - struct hlist_node *next = n->next; - struct hlist_node **pprev = n->pprev; - *pprev = next; - if (next) - next->pprev = pprev; -} - -static inline void hlist_del(struct hlist_node *n) -{ - __hlist_del(n); - n->next = LIST_POISON1; - n->pprev = LIST_POISON2; + __hlist_del( n ); + n->next = LIST_POISON1; + n->pprev = LIST_POISON2; } /** @@ -457,64 +414,71 @@ static inline void hlist_del(struct hlist_node *n) * the _rcu list-traversal primitives, such as * hlist_for_each_entry(). */ -static inline void hlist_del_rcu(struct hlist_node *n) +static inline void hlist_del_rcu( struct hlist_node* n ) { - __hlist_del(n); - n->pprev = LIST_POISON2; + __hlist_del( n ); + n->pprev = LIST_POISON2; } -static inline void hlist_del_init(struct hlist_node *n) +static inline void hlist_del_init( struct hlist_node* n ) { - if (n->pprev) { - __hlist_del(n); - INIT_HLIST_NODE(n); - } + if ( n->pprev ) { + __hlist_del( n ); + INIT_HLIST_NODE( n ); + } } -static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) +static inline void hlist_add_head( struct hlist_node* n, struct hlist_head* h ) { - struct hlist_node *first = h->first; - n->next = first; - if (first) - first->pprev = &n->next; - h->first = n; - n->pprev = &h->first; + struct hlist_node* first = h->first; + n->next = first; + if ( first ) + first->pprev = &n->next; + h->first = n; + n->pprev = &h->first; } /* next must be != NULL */ -static inline void hlist_add_before(struct hlist_node *n, - struct hlist_node *next) +static inline void hlist_add_before( struct hlist_node* n, struct hlist_node* next ) { - n->pprev = next->pprev; - n->next = next; - next->pprev = &n->next; - *(n->pprev) = n; + n->pprev = next->pprev; + n->next = next; + next->pprev = &n->next; + *( n->pprev ) = n; } -static inline void hlist_add_after(struct hlist_node *n, - struct hlist_node *next) +static inline void hlist_add_after( struct hlist_node* n, struct hlist_node* next ) { - next->next = n->next; - n->next = next; - next->pprev = &n->next; + next->next = n->next; + n->next = next; + next->pprev = &n->next; - if(next->next) - next->next->pprev = &next->next; + if ( next->next ) + next->next->pprev = &next->next; } -#define hlist_entry(ptr, type, member) container_of(ptr,type,member) +#define hlist_entry( ptr, type, member ) container_of( ptr, type, member ) -#define hlist_for_each(pos, head) \ - for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ - pos = pos->next) +#define hlist_for_each( pos, head ) \ + for ( pos = ( head )->first; pos && ( { \ + prefetch( pos->next ); \ + 1; \ + } ); \ + pos = pos->next ) -#define hlist_for_each_safe(pos, n, head) \ - for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ - pos = n) +#define hlist_for_each_safe( pos, n, head ) \ + for ( pos = ( head )->first; pos && ( { \ + n = pos->next; \ + 1; \ + } ); \ + pos = n ) -#define hlist_for_each_rcu(pos, head) \ - for ((pos) = (head)->first; pos && ({ prefetch((pos)->next); 1; }); \ - (pos) = rcu_dereference((pos)->next)) +#define hlist_for_each_rcu( pos, head ) \ + for ( ( pos ) = ( head )->first; pos && ( { \ + prefetch( ( pos )->next ); \ + 1; \ + } ); \ + ( pos ) = rcu_dereference( ( pos )->next ) ) /** * hlist_for_each_entry - iterate over list of given type @@ -523,11 +487,16 @@ static inline void hlist_add_after(struct hlist_node *n, * @head: the head for your list. * @member: the name of the hlist_node within the struct. */ -#define hlist_for_each_entry(tpos, pos, head, member) \ - for (pos = (head)->first; \ - pos && ({ prefetch(pos->next); 1;}) && \ - ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ - pos = pos->next) +#define hlist_for_each_entry( tpos, pos, head, member ) \ + for ( pos = ( head )->first; pos && ( { \ + prefetch( pos->next ); \ + 1; \ + } ) && \ + ( { \ + tpos = hlist_entry( pos, typeof( *tpos ), member ); \ + 1; \ + } ); \ + pos = pos->next ) /** * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point @@ -535,11 +504,16 @@ static inline void hlist_add_after(struct hlist_node *n, * @pos: the &struct hlist_node to use as a loop counter. * @member: the name of the hlist_node within the struct. */ -#define hlist_for_each_entry_continue(tpos, pos, member) \ - for (pos = (pos)->next; \ - pos && ({ prefetch(pos->next); 1;}) && \ - ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ - pos = pos->next) +#define hlist_for_each_entry_continue( tpos, pos, member ) \ + for ( pos = ( pos )->next; pos && ( { \ + prefetch( pos->next ); \ + 1; \ + } ) && \ + ( { \ + tpos = hlist_entry( pos, typeof( *tpos ), member ); \ + 1; \ + } ); \ + pos = pos->next ) /** * hlist_for_each_entry_from - iterate over a hlist continuing from existing point @@ -547,10 +521,16 @@ static inline void hlist_add_after(struct hlist_node *n, * @pos: the &struct hlist_node to use as a loop counter. * @member: the name of the hlist_node within the struct. */ -#define hlist_for_each_entry_from(tpos, pos, member) \ - for (; pos && ({ prefetch(pos->next); 1;}) && \ - ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ - pos = pos->next) +#define hlist_for_each_entry_from( tpos, pos, member ) \ + for ( ; pos && ( { \ + prefetch( pos->next ); \ + 1; \ + } ) && \ + ( { \ + tpos = hlist_entry( pos, typeof( *tpos ), member ); \ + 1; \ + } ); \ + pos = pos->next ) /** * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry @@ -560,11 +540,16 @@ static inline void hlist_add_after(struct hlist_node *n, * @head: the head for your list. * @member: the name of the hlist_node within the struct. */ -#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ - for (pos = (head)->first; \ - pos && ({ n = pos->next; 1; }) && \ - ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ - pos = n) +#define hlist_for_each_entry_safe( tpos, pos, n, head, member ) \ + for ( pos = ( head )->first; pos && ( { \ + n = pos->next; \ + 1; \ + } ) && \ + ( { \ + tpos = hlist_entry( pos, typeof( *tpos ), member ); \ + 1; \ + } ); \ + pos = n ) /** * hlist_for_each_entry_rcu - iterate over rcu list of given type @@ -577,10 +562,15 @@ static inline void hlist_add_after(struct hlist_node *n, * the _rcu list-mutation primitives such as hlist_add_head_rcu() * as long as the traversal is guarded by rcu_read_lock(). */ -#define hlist_for_each_entry_rcu(tpos, pos, head, member) \ - for (pos = (head)->first; \ - pos && ({ prefetch(pos->next); 1;}) && \ - ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ - pos = rcu_dereference(pos->next)) +#define hlist_for_each_entry_rcu( tpos, pos, head, member ) \ + for ( pos = ( head )->first; pos && ( { \ + prefetch( pos->next ); \ + 1; \ + } ) && \ + ( { \ + tpos = hlist_entry( pos, typeof( *tpos ), member ); \ + 1; \ + } ); \ + pos = rcu_dereference( pos->next ) ) -#endif /* _LINUX_LIST_H */ +#endif /* _LINUX_LIST_H */ diff --git a/src/main.c b/src/main.c index d0b4268..c13ccdb 100644 --- a/src/main.c +++ b/src/main.c @@ -26,682 +26,597 @@ #include "gdbstub.h" -static x49gp_t *x49gp; +static x49gp_t* x49gp; /* LD TEMPO HACK */ -CPUState *__GLOBAL_env; +CPUState* __GLOBAL_env; int semihosting_enabled = 1; -uint8_t *phys_ram_base; +uint8_t* phys_ram_base; int phys_ram_size; ram_addr_t ram_size = 0x80000; // LD ??? /* vl.c */ int singlestep; -#if !(defined(__APPLE__) || defined(_POSIX_C_SOURCE) && !defined(__sun__)) -static void *oom_check(void *ptr) +#if !( defined( __APPLE__ ) || defined( _POSIX_C_SOURCE ) && !defined( __sun__ ) ) +static void* oom_check( void* ptr ) { - if (ptr == NULL) { + if ( ptr == NULL ) { abort(); } return ptr; } #endif -void *qemu_memalign(size_t alignment, size_t size) +void* qemu_memalign( size_t alignment, size_t size ) { -#if defined(__APPLE__) || defined(_POSIX_C_SOURCE) && !defined(__sun__) +#if defined( __APPLE__ ) || defined( _POSIX_C_SOURCE ) && !defined( __sun__ ) int ret; - void *ptr; - ret = posix_memalign(&ptr, alignment, size); - if (ret != 0) + void* ptr; + ret = posix_memalign( &ptr, alignment, size ); + if ( ret != 0 ) abort(); return ptr; -#elif defined(CONFIG_BSD) - return oom_check(valloc(size)); +#elif defined( CONFIG_BSD ) + return oom_check( valloc( size ) ); #else - return oom_check(memalign(alignment, size)); + return oom_check( memalign( alignment, size ) ); #endif } - -void qemu_init_vcpu(void *_env) +void qemu_init_vcpu( void* _env ) { - CPUState *env = _env; + CPUState* env = _env; env->nr_cores = 1; env->nr_threads = 1; } -int qemu_cpu_self(void *env) +int qemu_cpu_self( void* env ) { return 1; } + +void qemu_cpu_kick( void* env ) {} + +void armv7m_nvic_set_pending( void* opaque, int irq ) { abort(); } +int armv7m_nvic_acknowledge_irq( void* opaque ) { abort(); } +void armv7m_nvic_complete_irq( void* opaque, int irq ) { abort(); } + +void* qemu_malloc( size_t size ) { return malloc( size ); } + +void* qemu_mallocz( size_t size ) { - return 1; + void* ptr; + + ptr = qemu_malloc( size ); + if ( NULL == ptr ) + return NULL; + memset( ptr, 0, size ); + return ptr; } -void qemu_cpu_kick(void *env) -{ -} +void qemu_free( void* ptr ) { free( ptr ); } -void armv7m_nvic_set_pending(void *opaque, int irq) +void* qemu_vmalloc( size_t size ) { - abort(); -} -int armv7m_nvic_acknowledge_irq(void *opaque) -{ - abort(); -} -void armv7m_nvic_complete_irq(void *opaque, int irq) -{ - abort(); -} - -void * -qemu_malloc(size_t size) -{ - return malloc(size); -} - -void * -qemu_mallocz(size_t size) -{ - void *ptr; - - ptr = qemu_malloc(size); - if (NULL == ptr) - return NULL; - memset(ptr, 0, size); - return ptr; -} - -void -qemu_free(void *ptr) -{ - free(ptr); -} - -void * -qemu_vmalloc(size_t size) -{ -#if defined(__linux__) - void *mem; - if (0 == posix_memalign(&mem, sysconf(_SC_PAGE_SIZE), size)) - return mem; - return NULL; +#if defined( __linux__ ) + void* mem; + if ( 0 == posix_memalign( &mem, sysconf( _SC_PAGE_SIZE ), size ) ) + return mem; + return NULL; #else - return valloc(size); + return valloc( size ); #endif } #define SWI_Breakpoint 0x180000 -uint32_t -do_arm_semihosting(CPUState *env) +uint32_t do_arm_semihosting( CPUState* env ) { - uint32_t number; - if (env->thumb) { - number = lduw_code(env->regs[15] - 2) & 0xff; - } else { - number = ldl_code(env->regs[15] - 4) & 0xffffff; - } - switch (number) { - case SWI_Breakpoint: - break; + uint32_t number; + if ( env->thumb ) { + number = lduw_code( env->regs[ 15 ] - 2 ) & 0xff; + } else { + number = ldl_code( env->regs[ 15 ] - 4 ) & 0xffffff; + } + switch ( number ) { + case SWI_Breakpoint: + break; - case 0: + case 0: #ifdef DEBUG_X49GP_SYSCALL - printf("%s: SWI LR %08x: syscall %u: args %08x %08x %08x %08x %08x %08x %08x\n", - __FUNCTION__, env->regs[14], env->regs[0], - env->regs[1], env->regs[2], env->regs[3], - env->regs[4], env->regs[5], env->regs[6], - env->regs[7]); + printf( "%s: SWI LR %08x: syscall %u: args %08x %08x %08x %08x %08x %08x %08x\n", __FUNCTION__, env->regs[ 14 ], env->regs[ 0 ], + env->regs[ 1 ], env->regs[ 2 ], env->regs[ 3 ], env->regs[ 4 ], env->regs[ 5 ], env->regs[ 6 ], env->regs[ 7 ] ); #endif #if 1 - switch (env->regs[0]) { - case 305: /* Beep */ - printf("%s: BEEP: frequency %u, time %u, override %u\n", - __FUNCTION__, env->regs[1], env->regs[2], env->regs[3]); + switch ( env->regs[ 0 ] ) { + case 305: /* Beep */ + printf( "%s: BEEP: frequency %u, time %u, override %u\n", __FUNCTION__, env->regs[ 1 ], env->regs[ 2 ], + env->regs[ 3 ] ); - gdk_beep(); - env->regs[0] = 0; - return 1; + gdk_beep(); + env->regs[ 0 ] = 0; + return 1; - case 28: /* CheckBeepEnd */ - env->regs[0] = 0; - return 1; + case 28: /* CheckBeepEnd */ + env->regs[ 0 ] = 0; + return 1; - case 29: /* StopBeep */ - env->regs[0] = 0; - return 1; + case 29: /* StopBeep */ + env->regs[ 0 ] = 0; + return 1; - default: - break; - } + default: + break; + } #endif - break; + break; - default: - break; - } + default: + break; + } - return 0; + return 0; } -void -x49gp_set_idle(x49gp_t *x49gp, x49gp_arm_idle_t idle) +void x49gp_set_idle( x49gp_t* x49gp, x49gp_arm_idle_t idle ) { #ifdef DEBUG_X49GP_ARM_IDLE - if (idle != x49gp->arm_idle) { - printf("%s: arm_idle %u, idle %u\n", __FUNCTION__, x49gp->arm_idle, idle); - } + if ( idle != x49gp->arm_idle ) { + printf( "%s: arm_idle %u, idle %u\n", __FUNCTION__, x49gp->arm_idle, idle ); + } #endif - x49gp->arm_idle = idle; + x49gp->arm_idle = idle; - if (x49gp->arm_idle == X49GP_ARM_RUN) { - x49gp->env->halted = 0; - } else { - x49gp->env->halted = 1; - cpu_exit(x49gp->env); - } + if ( x49gp->arm_idle == X49GP_ARM_RUN ) { + x49gp->env->halted = 0; + } else { + x49gp->env->halted = 1; + cpu_exit( x49gp->env ); + } } -static void -arm_sighnd(int sig) +static void arm_sighnd( int sig ) { - switch (sig) { - case SIGUSR1: -// stop_simulator = 1; -// x49gp->arm->CallDebug ^= 1; - break; - default: - fprintf(stderr, "%s: sig %u\n", __FUNCTION__, sig); - break; - } + switch ( sig ) { + case SIGUSR1: + // stop_simulator = 1; + // x49gp->arm->CallDebug ^= 1; + break; + default: + fprintf( stderr, "%s: sig %u\n", __FUNCTION__, sig ); + break; + } } -void -x49gp_gtk_timer(void *data) +void x49gp_gtk_timer( void* data ) { - while (gtk_events_pending()) { -// printf("%s: gtk_main_iteration_do()\n", __FUNCTION__); - gtk_main_iteration_do(FALSE); - } + while ( gtk_events_pending() ) { + // printf("%s: gtk_main_iteration_do()\n", __FUNCTION__); + gtk_main_iteration_do( FALSE ); + } - x49gp_mod_timer(x49gp->gtk_timer, - x49gp_get_clock() + X49GP_GTK_REFRESH_INTERVAL); + x49gp_mod_timer( x49gp->gtk_timer, x49gp_get_clock() + X49GP_GTK_REFRESH_INTERVAL ); } -void -x49gp_lcd_timer(void *data) +void x49gp_lcd_timer( void* data ) { - x49gp_t *x49gp = data; - int64_t now, expires; + x49gp_t* x49gp = data; + int64_t now, expires; -// printf("%s: lcd_update\n", __FUNCTION__); - x49gp_lcd_update(x49gp); - gdk_flush(); + // printf("%s: lcd_update\n", __FUNCTION__); + x49gp_lcd_update( x49gp ); + gdk_flush(); - now = x49gp_get_clock(); - expires = now + X49GP_LCD_REFRESH_INTERVAL; + now = x49gp_get_clock(); + expires = now + X49GP_LCD_REFRESH_INTERVAL; -// printf("%s: now: %lld, next update: %lld\n", __FUNCTION__, now, expires); - x49gp_mod_timer(x49gp->lcd_timer, expires); + // printf("%s: now: %lld, next update: %lld\n", __FUNCTION__, now, expires); + x49gp_mod_timer( x49gp->lcd_timer, expires ); } struct options { - char *config; - int debug_port; - int start_debugger; - char *firmware; - x49gp_reinit_t reinit; + char* config; + int debug_port; + int start_debugger; + char* firmware; + x49gp_reinit_t reinit; - int more_options; + int more_options; }; struct option_def; -typedef int (*option_action)(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); +typedef int ( *option_action )( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); struct option_def { - option_action action; - char *longname; - char shortname; + option_action action; + char* longname; + char shortname; }; -static int action_help(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); -static int action_debuglater(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); -static int action_debug(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); -static int action_reinit_flash(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); -static int action_reinit_flash_full(struct options *opt, - struct option_def *match, char *this_opt, - char *param, char *progname); -static int action_reboot(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); +static int action_help( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); +static int action_debuglater( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); +static int action_debug( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); +static int action_reinit_flash( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); +static int action_reinit_flash_full( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); +static int action_reboot( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); -static int action_unknown_with_param(struct options *opt, - struct option_def *match, char *this_opt, - char *param, char *progname); -static int action_longopt(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); -static int action_endopt(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname); +static int action_unknown_with_param( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); +static int action_longopt( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); +static int action_endopt( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ); struct option_def option_defs[] = { - { action_help, "help", 'h' }, - { action_debuglater, "enable-debug", 'D' }, - { action_debug, "debug", 'd' }, - { action_reinit_flash, "reflash", 'f' }, - { action_reinit_flash_full, "reflash-full", 'F' }, - { action_reboot, "reboot", 'r' }, + {action_help, "help", 'h' }, + {action_debuglater, "enable-debug", 'D' }, + {action_debug, "debug", 'd' }, + {action_reinit_flash, "reflash", 'f' }, + {action_reinit_flash_full, "reflash-full", 'F' }, + {action_reboot, "reboot", 'r' }, - { action_longopt, NULL, '-' }, - { action_unknown_with_param, NULL, '=' }, - { action_endopt, "", '\0' } + {action_longopt, NULL, '-' }, + {action_unknown_with_param, NULL, '=' }, + {action_endopt, "", '\0'} }; -static void -warn_unneeded_param(struct option_def *match, char *this_opt) +static void warn_unneeded_param( struct option_def* match, char* this_opt ) { - if (this_opt[1] == '-') { - fprintf(stderr, "The option \"--%s\" does not support" - " parameters\n", match->longname); - } else - fprintf(stderr, "The option '-%c' does not support parameters\n", - match->shortname); + if ( this_opt[ 1 ] == '-' ) { + fprintf( stderr, + "The option \"--%s\" does not support" + " parameters\n", + match->longname ); + } else + fprintf( stderr, "The option '-%c' does not support parameters\n", match->shortname ); } -static int -action_help(struct options *opt, struct option_def *match, char *this_opt, - char *param, char *progname) +static int action_help( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - if (param != NULL) - warn_unneeded_param(match, this_opt); + if ( param != NULL ) + warn_unneeded_param( match, this_opt ); - fprintf(stderr, "Emulator for HP 49G+ / 50G calculators\n" - "Usage: %s [] []\n" - "Valid options:\n" - " -D, --enable-debug[=] like -D, but also start the" - " debugger immediately\n" - " -f, --reflash[=firmware] rebuild the flash using the" - " supplied firmware\n" - " (default: select one" - " interactively)\n" - " (implies -r for safety" - " reasons)\n" - " -F, --reflash-full[=firmware] like -f, but don't preserve the" - " flash contents\n" - " in the area beyond the" - " firmware\n" - " -r, --reboot reboot on startup instead of" - " continuing from the\n" - " saved state in the config" - " file\n" - " -h, --help print this message and exit\n" - "The config file is formatted as INI file and contains the" - " settings for which\n" - "persistence makes sense, like calculator model, CPU" - " registers, etc.\n" - "If the config file is omitted, ~/.%s/config is used.\n" - "Please consult the manual for more details on config file" - " settings.\n", progname, DEFAULT_GDBSTUB_PORT, progname); - exit(0); + fprintf( stderr, + "Emulator for HP 49G+ / 50G calculators\n" + "Usage: %s [] []\n" + "Valid options:\n" + " -D, --enable-debug[=] like -D, but also start the" + " debugger immediately\n" + " -f, --reflash[=firmware] rebuild the flash using the" + " supplied firmware\n" + " (default: select one" + " interactively)\n" + " (implies -r for safety" + " reasons)\n" + " -F, --reflash-full[=firmware] like -f, but don't preserve the" + " flash contents\n" + " in the area beyond the" + " firmware\n" + " -r, --reboot reboot on startup instead of" + " continuing from the\n" + " saved state in the config" + " file\n" + " -h, --help print this message and exit\n" + "The config file is formatted as INI file and contains the" + " settings for which\n" + "persistence makes sense, like calculator model, CPU" + " registers, etc.\n" + "If the config file is omitted, ~/.%s/config is used.\n" + "Please consult the manual for more details on config file" + " settings.\n", + progname, DEFAULT_GDBSTUB_PORT, progname ); + exit( 0 ); } -static int -action_debuglater(struct options *opt, struct option_def *match, char *this_opt, - char *param, char *progname) +static int action_debuglater( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - char *end; - int port; + char* end; + int port; - if (param == NULL) { - if (opt->debug_port == 0) - opt->debug_port = DEFAULT_GDBSTUB_PORT; - return FALSE; - } + if ( param == NULL ) { + if ( opt->debug_port == 0 ) + opt->debug_port = DEFAULT_GDBSTUB_PORT; + return FALSE; + } - port = strtoul(param, &end, 0); - if ((end == param) || (*end != '\0')) { - fprintf(stderr, "Invalid port \"%s\", using default\n", param); - if (opt->debug_port == 0) - opt->debug_port = DEFAULT_GDBSTUB_PORT; - return TRUE; - } + port = strtoul( param, &end, 0 ); + if ( ( end == param ) || ( *end != '\0' ) ) { + fprintf( stderr, "Invalid port \"%s\", using default\n", param ); + if ( opt->debug_port == 0 ) + opt->debug_port = DEFAULT_GDBSTUB_PORT; + return TRUE; + } - if (opt->debug_port != 0 && opt->debug_port != DEFAULT_GDBSTUB_PORT) - fprintf(stderr, "Additional debug port \"%s\" specified," - " overriding\n", param); - opt->debug_port = port; - return TRUE; + if ( opt->debug_port != 0 && opt->debug_port != DEFAULT_GDBSTUB_PORT ) + fprintf( stderr, + "Additional debug port \"%s\" specified," + " overriding\n", + param ); + opt->debug_port = port; + return TRUE; } -static int -action_debug(struct options *opt, struct option_def *match, char *this_opt, - char *param, char *progname) +static int action_debug( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - opt->start_debugger = TRUE; - return action_debuglater(opt, match, this_opt, param, progname); + opt->start_debugger = TRUE; + return action_debuglater( opt, match, this_opt, param, progname ); } -static int -action_reinit_flash(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname) +static int action_reinit_flash( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - if (opt->reinit < X49GP_REINIT_FLASH) - opt->reinit = X49GP_REINIT_FLASH; + if ( opt->reinit < X49GP_REINIT_FLASH ) + opt->reinit = X49GP_REINIT_FLASH; - if (param == NULL) - return FALSE; + if ( param == NULL ) + return FALSE; - if (opt->firmware != NULL) - fprintf(stderr, "Additional firmware file \"%s\" specified," - " overriding\n", param); - opt->firmware = param; - return TRUE; + if ( opt->firmware != NULL ) + fprintf( stderr, + "Additional firmware file \"%s\" specified," + " overriding\n", + param ); + opt->firmware = param; + return TRUE; } -static int -action_reinit_flash_full(struct options *opt, - struct option_def *match, char *this_opt, - char *param, char *progname) +static int action_reinit_flash_full( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - int result = action_reinit_flash(opt, match, this_opt, param, progname); - opt->reinit = X49GP_REINIT_FLASH_FULL; - return result; + int result = action_reinit_flash( opt, match, this_opt, param, progname ); + opt->reinit = X49GP_REINIT_FLASH_FULL; + return result; } -static int -action_reboot(struct options *opt, struct option_def *match, char *this_opt, - char *param, char *progname) +static int action_reboot( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - if (param != NULL) - warn_unneeded_param(match, this_opt); + if ( param != NULL ) + warn_unneeded_param( match, this_opt ); - if (opt->reinit < X49GP_REINIT_REBOOT_ONLY) - opt->reinit = X49GP_REINIT_REBOOT_ONLY; - return param != NULL; + if ( opt->reinit < X49GP_REINIT_REBOOT_ONLY ) + opt->reinit = X49GP_REINIT_REBOOT_ONLY; + return param != NULL; } -static int -action_longopt(struct options *opt, struct option_def *match, char *this_opt, - char *param, char *progname) +static int action_longopt( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - int i; - char *test_str, *option_str; + int i; + char *test_str, *option_str; - if (this_opt[1] != '-' || param != NULL) { - fprintf(stderr, "Unrecognized option '-', ignoring\n"); - return FALSE; - } + if ( this_opt[ 1 ] != '-' || param != NULL ) { + fprintf( stderr, "Unrecognized option '-', ignoring\n" ); + return FALSE; + } - for (i = 0; i < sizeof(option_defs) / sizeof(option_defs[0]); i++) { - if (option_defs[i].longname == NULL) - continue; + for ( i = 0; i < sizeof( option_defs ) / sizeof( option_defs[ 0 ] ); i++ ) { + if ( option_defs[ i ].longname == NULL ) + continue; - test_str = option_defs[i].longname; - option_str = this_opt + 2; + test_str = option_defs[ i ].longname; + option_str = this_opt + 2; - while (*test_str != '\0' && *test_str == *option_str) { - test_str++; - option_str++; - } + while ( *test_str != '\0' && *test_str == *option_str ) { + test_str++; + option_str++; + } - if (*test_str != '\0') continue; + if ( *test_str != '\0' ) + continue; - switch (*option_str) { - case '\0': - (option_defs[i].action)(opt, option_defs + i, this_opt, - NULL, progname); - return TRUE; - case '=': - (option_defs[i].action)(opt, option_defs + i, this_opt, - option_str+1, progname); - return TRUE; - } - } + switch ( *option_str ) { + case '\0': + ( option_defs[ i ].action )( opt, option_defs + i, this_opt, NULL, progname ); + return TRUE; + case '=': + ( option_defs[ i ].action )( opt, option_defs + i, this_opt, option_str + 1, progname ); + return TRUE; + } + } - fprintf(stderr, "Unrecognized option \"%s\", ignoring\n", this_opt + 2); - return TRUE; + fprintf( stderr, "Unrecognized option \"%s\", ignoring\n", this_opt + 2 ); + return TRUE; } -static int -action_unknown_with_param(struct options *opt, struct option_def *match, - char *this_opt, char *param, char *progname) +static int action_unknown_with_param( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - return TRUE; + return TRUE; } -static int -action_endopt(struct options *opt, struct option_def *match, char *this_opt, - char *param, char *progname) +static int action_endopt( struct options* opt, struct option_def* match, char* this_opt, char* param, char* progname ) { - opt->more_options = FALSE; - return TRUE; + opt->more_options = FALSE; + return TRUE; } -static void -parse_shortopt(struct options *opt, char *this_opt, char *progname) +static void parse_shortopt( struct options* opt, char* this_opt, char* progname ) { - char *option = this_opt + 1; - char *param; - int i; + char* option = this_opt + 1; + char* param; + int i; - if (*option == '\0') { - fprintf(stderr, - "Empty option present, ignoring\n"); - return; - } + if ( *option == '\0' ) { + fprintf( stderr, "Empty option present, ignoring\n" ); + return; + } - do { - for (i = 0; i < sizeof(option_defs) / sizeof(option_defs[0]); - i++) { + do { + for ( i = 0; i < sizeof( option_defs ) / sizeof( option_defs[ 0 ] ); i++ ) { - if (*option == option_defs[i].shortname) { - if (*(option + 1) == '=') { - param = option + 2; - } else { - param = NULL; - } + if ( *option == option_defs[ i ].shortname ) { + if ( *( option + 1 ) == '=' ) { + param = option + 2; + } else { + param = NULL; + } - if ((option_defs[i].action)(opt, option_defs + i, - this_opt, param, - progname)) - return; - break; - } - } + if ( ( option_defs[ i ].action )( opt, option_defs + i, this_opt, param, progname ) ) + return; + break; + } + } - - if (i == sizeof(option_defs) / sizeof(option_defs[0])) - fprintf(stderr, - "Unrecognized option '%c', ignoring\n", - *option); - option++; - } while (*option != '\0'); + if ( i == sizeof( option_defs ) / sizeof( option_defs[ 0 ] ) ) + fprintf( stderr, "Unrecognized option '%c', ignoring\n", *option ); + option++; + } while ( *option != '\0' ); } -static void -parse_options(struct options *opt, int argc, char **argv, char *progname) +static void parse_options( struct options* opt, int argc, char** argv, char* progname ) { - opt->more_options = TRUE; + opt->more_options = TRUE; - while (argc > 1) { - switch (argv[1][0]) { - case '\0': - break; - break; + while ( argc > 1 ) { + switch ( argv[ 1 ][ 0 ] ) { + case '\0': + break; + break; - case '-': - if (opt->more_options) { - parse_shortopt(opt, argv[1], progname); - break; - } - /* FALL THROUGH */ + case '-': + if ( opt->more_options ) { + parse_shortopt( opt, argv[ 1 ], progname ); + break; + } + /* FALL THROUGH */ - default: - if (opt->config != NULL) { - fprintf(stderr, - "Additional config file \"%s\"" - " specified, overriding\n", - argv[1]); - } - opt->config = argv[1]; - } - - argc--; - argv++; - } + default: + if ( opt->config != NULL ) { + fprintf( stderr, + "Additional config file \"%s\"" + " specified, overriding\n", + argv[ 1 ] ); + } + opt->config = argv[ 1 ]; + } + argc--; + argv++; + } } -void -ui_sighnd(int sig) +void ui_sighnd( int sig ) { - switch (sig) { - case SIGINT: - case SIGQUIT: - case SIGTERM: - x49gp->arm_exit = 1; - cpu_exit(x49gp->env); - break; - } + switch ( sig ) { + case SIGINT: + case SIGQUIT: + case SIGTERM: + x49gp->arm_exit = 1; + cpu_exit( x49gp->env ); + break; + } } -int -main(int argc, char **argv) +int main( int argc, char** argv ) { - char *progname, *progpath; - int error; - struct options opt; - const char *home; + char *progname, *progpath; + int error; + struct options opt; + const char* home; + progname = g_path_get_basename( argv[ 0 ] ); + progpath = g_path_get_dirname( argv[ 0 ] ); - progname = g_path_get_basename(argv[0]); - progpath = g_path_get_dirname(argv[0]); + gtk_init( &argc, &argv ); + opt.config = NULL; + opt.debug_port = 0; + opt.start_debugger = FALSE; + opt.reinit = X49GP_REINIT_NONE; + opt.firmware = NULL; + parse_options( &opt, argc, argv, progname ); - gtk_init(&argc, &argv); - - - opt.config = NULL; - opt.debug_port = 0; - opt.start_debugger = FALSE; - opt.reinit = X49GP_REINIT_NONE; - opt.firmware = NULL; - parse_options(&opt, argc, argv, progname); - - x49gp = malloc(sizeof(x49gp_t)); - if (NULL == x49gp) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - progname, __FUNCTION__, __LINE__); - exit(1); - } - memset(x49gp, 0, sizeof(x49gp_t)); + x49gp = malloc( sizeof( x49gp_t ) ); + if ( NULL == x49gp ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", progname, __FUNCTION__, __LINE__ ); + exit( 1 ); + } + memset( x49gp, 0, sizeof( x49gp_t ) ); #ifdef DEBUG_X49GP_MAIN - fprintf(stderr, "_SC_PAGE_SIZE: %08lx\n", sysconf(_SC_PAGE_SIZE)); + fprintf( stderr, "_SC_PAGE_SIZE: %08lx\n", sysconf( _SC_PAGE_SIZE ) ); - printf("%s:%u: x49gp: %p\n", __FUNCTION__, __LINE__, x49gp); + printf( "%s:%u: x49gp: %p\n", __FUNCTION__, __LINE__, x49gp ); #endif - INIT_LIST_HEAD(&x49gp->modules); + INIT_LIST_HEAD( &x49gp->modules ); + x49gp->progname = progname; + x49gp->progpath = progpath; + x49gp->clk_tck = sysconf( _SC_CLK_TCK ); - x49gp->progname = progname; - x49gp->progpath = progpath; - x49gp->clk_tck = sysconf(_SC_CLK_TCK); + x49gp->emulator_fclk = 75000000; + x49gp->PCLK_ratio = 4; + x49gp->PCLK = 75000000 / 4; - x49gp->emulator_fclk = 75000000; - x49gp->PCLK_ratio = 4; - x49gp->PCLK = 75000000 / 4; + // cpu_set_log(0xffffffff); + cpu_exec_init_all( 0 ); + x49gp->env = cpu_init( "arm926" ); + __GLOBAL_env = x49gp->env; - //cpu_set_log(0xffffffff); - cpu_exec_init_all(0); - x49gp->env = cpu_init("arm926"); - __GLOBAL_env = x49gp->env; + // cpu_set_log(cpu_str_to_log_mask("all")); -// cpu_set_log(cpu_str_to_log_mask("all")); + x49gp_timer_init( x49gp ); - x49gp_timer_init(x49gp); + x49gp->gtk_timer = x49gp_new_timer( X49GP_TIMER_REALTIME, x49gp_gtk_timer, x49gp ); + x49gp->lcd_timer = x49gp_new_timer( X49GP_TIMER_VIRTUAL, x49gp_lcd_timer, x49gp ); - x49gp->gtk_timer = x49gp_new_timer(X49GP_TIMER_REALTIME, - x49gp_gtk_timer, x49gp); - x49gp->lcd_timer = x49gp_new_timer(X49GP_TIMER_VIRTUAL, - x49gp_lcd_timer, x49gp); + x49gp_ui_init( x49gp ); - x49gp_ui_init(x49gp); + x49gp_s3c2410_arm_init( x49gp ); - x49gp_s3c2410_arm_init(x49gp); + x49gp_flash_init( x49gp ); + x49gp_sram_init( x49gp ); - x49gp_flash_init(x49gp); - x49gp_sram_init(x49gp); + x49gp_s3c2410_init( x49gp ); - x49gp_s3c2410_init(x49gp); + if ( x49gp_modules_init( x49gp ) ) { + exit( 1 ); + } - if (x49gp_modules_init(x49gp)) { - exit(1); - } + if ( opt.config == NULL ) { + char config_dir[ strlen( progname ) + 9 ]; - if (opt.config == NULL) { - char config_dir[strlen(progname) + 9]; + home = g_get_home_dir(); + sprintf( config_dir, ".config/%s", progname ); + opt.config = g_build_filename( home, config_dir, "config", NULL ); + } - home = g_get_home_dir(); - sprintf(config_dir, ".config/%s", progname); - opt.config = g_build_filename(home, config_dir, - "config", NULL); - } + x49gp->basename = g_path_get_dirname( opt.config ); + x49gp->debug_port = opt.debug_port; + x49gp->startup_reinit = opt.reinit; + x49gp->firmware = opt.firmware; - x49gp->basename = g_path_get_dirname(opt.config); - x49gp->debug_port = opt.debug_port; - x49gp->startup_reinit = opt.reinit; - x49gp->firmware = opt.firmware; + error = x49gp_modules_load( x49gp, opt.config ); + if ( error || opt.reinit >= X49GP_REINIT_REBOOT_ONLY ) { + if ( error && error != -EAGAIN ) { + exit( 1 ); + } + x49gp_modules_reset( x49gp, X49GP_RESET_POWER_ON ); + } + // x49gp_modules_reset(x49gp, X49GP_RESET_POWER_ON); - error = x49gp_modules_load(x49gp, opt.config); - if (error || opt.reinit >= X49GP_REINIT_REBOOT_ONLY) { - if (error && error != -EAGAIN) { - exit(1); - } - x49gp_modules_reset(x49gp, X49GP_RESET_POWER_ON); - } -// x49gp_modules_reset(x49gp, X49GP_RESET_POWER_ON); + signal( SIGINT, ui_sighnd ); + signal( SIGTERM, ui_sighnd ); + signal( SIGQUIT, ui_sighnd ); - signal(SIGINT, ui_sighnd); - signal(SIGTERM, ui_sighnd); - signal(SIGQUIT, ui_sighnd); + signal( SIGUSR1, arm_sighnd ); - signal(SIGUSR1, arm_sighnd); + x49gp_set_idle( x49gp, 0 ); + // stl_phys(0x08000a1c, 0x55555555); - x49gp_set_idle(x49gp, 0); + x49gp_mod_timer( x49gp->gtk_timer, x49gp_get_clock() ); + x49gp_mod_timer( x49gp->lcd_timer, x49gp_get_clock() ); -// stl_phys(0x08000a1c, 0x55555555); + if ( opt.debug_port != 0 && opt.start_debugger ) { + gdbserver_start( opt.debug_port ); + gdb_handlesig( x49gp->env, 0 ); + } + x49gp_main_loop( x49gp ); - x49gp_mod_timer(x49gp->gtk_timer, x49gp_get_clock()); - x49gp_mod_timer(x49gp->lcd_timer, x49gp_get_clock()); - - - if(opt.debug_port != 0 && opt.start_debugger) { - gdbserver_start(opt.debug_port); - gdb_handlesig(x49gp->env, 0); - } - - x49gp_main_loop(x49gp); - - - x49gp_modules_save(x49gp, opt.config); - x49gp_modules_exit(x49gp); - + x49gp_modules_save( x49gp, opt.config ); + x49gp_modules_exit( x49gp ); #if 0 printf("ClkTicks: %lu\n", ARMul_Time(x49gp->arm)); @@ -714,5 +629,5 @@ main(int argc, char **argv) x49gp->mmu->iTLB.search, x49gp->mmu->iTLB.nsearch, x49gp->mmu->iTLB.walk); #endif - return 0; + return 0; } diff --git a/src/module.c b/src/module.c index ecc6ea7..b5c685c 100644 --- a/src/module.c +++ b/src/module.c @@ -11,493 +11,434 @@ #include "x49gp.h" -int -x49gp_modules_init(x49gp_t *x49gp) +int x49gp_modules_init( x49gp_t* x49gp ) { - x49gp_module_t *module; - int error; + x49gp_module_t* module; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s:%u:\n", __FUNCTION__, __LINE__); + printf( "%s:%u:\n", __FUNCTION__, __LINE__ ); #endif - phys_ram_size = 0; + phys_ram_size = 0; - list_for_each_entry(module, &x49gp->modules, list) { - error = module->init(module); - if (error) { - return error; - } - } + list_for_each_entry( module, &x49gp->modules, list ) + { + error = module->init( module ); + if ( error ) { + return error; + } + } - phys_ram_base = mmap(0, phys_ram_size, PROT_NONE, MAP_SHARED | MAP_ANON, -1, 0); - if (phys_ram_base == (uint8_t *) -1) { - fprintf(stderr, "%s: can't mmap %08x anonymous bytes\n", - __FUNCTION__, phys_ram_size); - exit(1); - } + phys_ram_base = mmap( 0, phys_ram_size, PROT_NONE, MAP_SHARED | MAP_ANON, -1, 0 ); + if ( phys_ram_base == ( uint8_t* )-1 ) { + fprintf( stderr, "%s: can't mmap %08x anonymous bytes\n", __FUNCTION__, phys_ram_size ); + exit( 1 ); + } #ifdef DEBUG_X49GP_MODULES - printf("%s: phys_ram_base: %p\n", __FUNCTION__, phys_ram_base); + printf( "%s: phys_ram_base: %p\n", __FUNCTION__, phys_ram_base ); #endif - phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS); - memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS); + phys_ram_dirty = qemu_vmalloc( phys_ram_size >> TARGET_PAGE_BITS ); + memset( phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS ); - ram_addr_t x49gp_ram_alloc(ram_addr_t size, uint8_t *base); - x49gp_ram_alloc(phys_ram_size, phys_ram_base); + ram_addr_t x49gp_ram_alloc( ram_addr_t size, uint8_t* base ); + x49gp_ram_alloc( phys_ram_size, phys_ram_base ); - return 0; + return 0; } -int -x49gp_modules_exit(x49gp_t *x49gp) +int x49gp_modules_exit( x49gp_t* x49gp ) { - x49gp_module_t *module, *next; - int error; + x49gp_module_t *module, *next; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s:%u:\n", __FUNCTION__, __LINE__); + printf( "%s:%u:\n", __FUNCTION__, __LINE__ ); #endif - list_for_each_entry_safe_reverse(module, next, &x49gp->modules, list) { - error = module->exit(module); - if (error) { - return error; - } - } + list_for_each_entry_safe_reverse( module, next, &x49gp->modules, list ) + { + error = module->exit( module ); + if ( error ) { + return error; + } + } - return 0; + return 0; } -int -x49gp_modules_reset(x49gp_t *x49gp, x49gp_reset_t reset) +int x49gp_modules_reset( x49gp_t* x49gp, x49gp_reset_t reset ) { - x49gp_module_t *module; - int error; + x49gp_module_t* module; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s:%u:\n", __FUNCTION__, __LINE__); + printf( "%s:%u:\n", __FUNCTION__, __LINE__ ); #endif - list_for_each_entry(module, &x49gp->modules, list) { - error = module->reset(module, reset); - if (error) { - return error; - } - } + list_for_each_entry( module, &x49gp->modules, list ) + { + error = module->reset( module, reset ); + if ( error ) { + return error; + } + } - return 0; + return 0; } -int -x49gp_modules_load(x49gp_t *x49gp, const char *filename) +int x49gp_modules_load( x49gp_t* x49gp, const char* filename ) { - x49gp_module_t *module; - GError *gerror = NULL; - int error, result; + x49gp_module_t* module; + GError* gerror = NULL; + int error, result; #ifdef DEBUG_X49GP_MODULES - printf("%s:%u:\n", __FUNCTION__, __LINE__); + printf( "%s:%u:\n", __FUNCTION__, __LINE__ ); #endif - if (g_mkdir_with_parents(x49gp->basename, 0755)) { - error = -errno; - fprintf(stderr, "%s:%u: g_mkdir_with_parents: %s\n", - __FUNCTION__, __LINE__, strerror(errno)); - return error; - } + if ( g_mkdir_with_parents( x49gp->basename, 0755 ) ) { + error = -errno; + fprintf( stderr, "%s:%u: g_mkdir_with_parents: %s\n", __FUNCTION__, __LINE__, strerror( errno ) ); + return error; + } - x49gp->config = g_key_file_new(); - if (NULL == x49gp->config) { - fprintf(stderr, "%s:%u: g_key_file_new: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + x49gp->config = g_key_file_new(); + if ( NULL == x49gp->config ) { + fprintf( stderr, "%s:%u: g_key_file_new: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - if (! g_key_file_load_from_file(x49gp->config, filename, - G_KEY_FILE_KEEP_COMMENTS, &gerror) - && ! g_error_matches(gerror, G_FILE_ERROR, G_FILE_ERROR_NOENT)) { - fprintf(stderr, "%s:%u: g_key_file_load_from_file: %s\n", - __FUNCTION__, __LINE__, gerror->message); - g_key_file_free(x49gp->config); - return -EIO; - } + if ( !g_key_file_load_from_file( x49gp->config, filename, G_KEY_FILE_KEEP_COMMENTS, &gerror ) && + !g_error_matches( gerror, G_FILE_ERROR, G_FILE_ERROR_NOENT ) ) { + fprintf( stderr, "%s:%u: g_key_file_load_from_file: %s\n", __FUNCTION__, __LINE__, gerror->message ); + g_key_file_free( x49gp->config ); + return -EIO; + } - result = 0; + result = 0; - list_for_each_entry(module, &x49gp->modules, list) { - error = module->load(module, x49gp->config); - if (error) { - if (error == -EAGAIN) { - result = -EAGAIN; - } else { - return error; - } - } - } + list_for_each_entry( module, &x49gp->modules, list ) + { + error = module->load( module, x49gp->config ); + if ( error ) { + if ( error == -EAGAIN ) { + result = -EAGAIN; + } else { + return error; + } + } + } -{ - extern unsigned char *phys_ram_base; + { + extern unsigned char* phys_ram_base; #ifdef DEBUG_X49GP_MODULES - printf("%s: phys_ram_base: %p\n", __FUNCTION__, phys_ram_base); - printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n", - phys_ram_base[0], - phys_ram_base[1], - phys_ram_base[2], - phys_ram_base[3], - phys_ram_base[4], - phys_ram_base[5], - phys_ram_base[6], - phys_ram_base[7]); + printf( "%s: phys_ram_base: %p\n", __FUNCTION__, phys_ram_base ); + printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n", phys_ram_base[ 0 ], phys_ram_base[ 1 ], phys_ram_base[ 2 ], + phys_ram_base[ 3 ], phys_ram_base[ 4 ], phys_ram_base[ 5 ], phys_ram_base[ 6 ], phys_ram_base[ 7 ] ); #endif + } + + return result; } - return result; -} - -int -x49gp_modules_save(x49gp_t *x49gp, const char *filename) +int x49gp_modules_save( x49gp_t* x49gp, const char* filename ) { - x49gp_module_t *module; - GError *gerror = NULL; - gchar *data; - gsize length; - int error; - int fd; + x49gp_module_t* module; + GError* gerror = NULL; + gchar* data; + gsize length; + int error; + int fd; #ifdef DEBUG_X49GP_MODULES - printf("%s:%u:\n", __FUNCTION__, __LINE__); + printf( "%s:%u:\n", __FUNCTION__, __LINE__ ); #endif - list_for_each_entry(module, &x49gp->modules, list) { - error = module->save(module, x49gp->config); - if (error) { - return error; - } - } + list_for_each_entry( module, &x49gp->modules, list ) + { + error = module->save( module, x49gp->config ); + if ( error ) { + return error; + } + } - data = g_key_file_to_data(x49gp->config, &length, &gerror); - if (NULL == data) { - fprintf(stderr, "%s:%u: g_key_file_to_data: %s\n", - __FUNCTION__, __LINE__, gerror->message); - return -ENOMEM; - } + data = g_key_file_to_data( x49gp->config, &length, &gerror ); + if ( NULL == data ) { + fprintf( stderr, "%s:%u: g_key_file_to_data: %s\n", __FUNCTION__, __LINE__, gerror->message ); + return -ENOMEM; + } - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) { - error = -errno; - fprintf(stderr, "%s:%u: open %s: %s\n", - __FUNCTION__, __LINE__, filename, strerror(errno)); - g_free(data); - return error; - } + fd = open( filename, O_WRONLY | O_CREAT | O_TRUNC, 0644 ); + if ( fd < 0 ) { + error = -errno; + fprintf( stderr, "%s:%u: open %s: %s\n", __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( data ); + return error; + } + if ( write( fd, data, length ) != length ) { + error = -errno; + fprintf( stderr, "%s:%u: write %s: %s\n", __FUNCTION__, __LINE__, filename, strerror( errno ) ); + close( fd ); + g_free( data ); + return error; + } - if (write(fd, data, length) != length) { - error = -errno; - fprintf(stderr, "%s:%u: write %s: %s\n", - __FUNCTION__, __LINE__, filename, strerror(errno)); - close(fd); - g_free(data); - return error; - } + close( fd ); + g_free( data ); - close(fd); - g_free(data); - - return 0; + return 0; } -int -x49gp_module_register(x49gp_module_t *module) +int x49gp_module_register( x49gp_module_t* module ) { - x49gp_t *x49gp = module->x49gp; + x49gp_t* x49gp = module->x49gp; #ifdef DEBUG_X49GP_MODULES - printf("%s:%u: %s\n", __FUNCTION__, __LINE__, module->name); + printf( "%s:%u: %s\n", __FUNCTION__, __LINE__, module->name ); #endif - list_add_tail(&module->list, &x49gp->modules); + list_add_tail( &module->list, &x49gp->modules ); - return 0; + return 0; } -int -x49gp_module_unregister(x49gp_module_t *module) +int x49gp_module_unregister( x49gp_module_t* module ) { #ifdef DEBUG_X49GP_MODULES - printf("%s:%u: %s\n", __FUNCTION__, __LINE__, module->name); + printf( "%s:%u: %s\n", __FUNCTION__, __LINE__, module->name ); #endif - list_del(&module->list); + list_del( &module->list ); - return 0; + return 0; } -int -x49gp_module_get_filename(x49gp_module_t *module, GKeyFile *key, - const char *name, char *reset, char **valuep, - char **path) +int x49gp_module_get_filename( x49gp_module_t* module, GKeyFile* key, const char* name, char* reset, char** valuep, char** path ) { - x49gp_t *x49gp = module->x49gp; - int error; + x49gp_t* x49gp = module->x49gp; + int error; - error = x49gp_module_get_string(module, key, name, reset, valuep); + error = x49gp_module_get_string( module, key, name, reset, valuep ); - if (g_path_is_absolute(*valuep)) { - *path = g_strdup(*valuep); - return error; - } + if ( g_path_is_absolute( *valuep ) ) { + *path = g_strdup( *valuep ); + return error; + } - *path = g_build_filename(x49gp->basename, *valuep, NULL); - if (NULL == path) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->name, __FUNCTION__, __LINE__); - g_free(*valuep); - *valuep = NULL; - } + *path = g_build_filename( x49gp->basename, *valuep, NULL ); + if ( NULL == path ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->name, __FUNCTION__, __LINE__ ); + g_free( *valuep ); + *valuep = NULL; + } - return error; + return error; } -int -x49gp_module_set_filename(x49gp_module_t *module, GKeyFile *key, - const char *name, const char *value) +int x49gp_module_set_filename( x49gp_module_t* module, GKeyFile* key, const char* name, const char* value ) { - return x49gp_module_set_string(module, key, name, value); + return x49gp_module_set_string( module, key, name, value ); } -int -x49gp_module_get_int(x49gp_module_t *module, GKeyFile *key, const char *name, - int reset, int *valuep) +int x49gp_module_get_int( x49gp_module_t* module, GKeyFile* key, const char* name, int reset, int* valuep ) { - return x49gp_module_get_u32(module, key, name, reset, - (uint32_t *) valuep); + return x49gp_module_get_u32( module, key, name, reset, ( uint32_t* )valuep ); } -int -x49gp_module_set_int(x49gp_module_t *module, GKeyFile *key, - const char *name, int value) +int x49gp_module_set_int( x49gp_module_t* module, GKeyFile* key, const char* name, int value ) { - char data[16]; + char data[ 16 ]; - snprintf(data, sizeof(data), "%d", value); + snprintf( data, sizeof( data ), "%d", value ); - g_key_file_set_value(key, module->name, name, data); + g_key_file_set_value( key, module->name, name, data ); - return 0; + return 0; } -int -x49gp_module_get_uint(x49gp_module_t *module, GKeyFile *key, const char *name, - unsigned int reset, unsigned int *valuep) +int x49gp_module_get_uint( x49gp_module_t* module, GKeyFile* key, const char* name, unsigned int reset, unsigned int* valuep ) { - return x49gp_module_get_u32(module, key, name, reset, valuep); + return x49gp_module_get_u32( module, key, name, reset, valuep ); } -int -x49gp_module_set_uint(x49gp_module_t *module, GKeyFile *key, - const char *name, unsigned int value) +int x49gp_module_set_uint( x49gp_module_t* module, GKeyFile* key, const char* name, unsigned int value ) { - char data[16]; + char data[ 16 ]; - snprintf(data, sizeof(data), "%u", value); + snprintf( data, sizeof( data ), "%u", value ); - g_key_file_set_value(key, module->name, name, data); + g_key_file_set_value( key, module->name, name, data ); - return 0; + return 0; } -int -x49gp_module_get_u32(x49gp_module_t *module, GKeyFile *key, - const char *name, uint32_t reset, uint32_t *valuep) +int x49gp_module_get_u32( x49gp_module_t* module, GKeyFile* key, const char* name, uint32_t reset, uint32_t* valuep ) { - GError *gerror = NULL; - char *data, *end; - uint32_t value; + GError* gerror = NULL; + char *data, *end; + uint32_t value; - data = g_key_file_get_value(key, module->name, name, &gerror); - if (NULL == data) { - fprintf(stderr, "%s: %s:%u: key \"%s\" not found\n", - module->name, __FUNCTION__, __LINE__, name); - *valuep = reset; - return -EAGAIN; - } + data = g_key_file_get_value( key, module->name, name, &gerror ); + if ( NULL == data ) { + fprintf( stderr, "%s: %s:%u: key \"%s\" not found\n", module->name, __FUNCTION__, __LINE__, name ); + *valuep = reset; + return -EAGAIN; + } - value = strtoul(data, &end, 0); - if ((end == data) || (*end != '\0')) { - *valuep = reset; - g_free(data); - return -EAGAIN; - } + value = strtoul( data, &end, 0 ); + if ( ( end == data ) || ( *end != '\0' ) ) { + *valuep = reset; + g_free( data ); + return -EAGAIN; + } - *valuep = value; + *valuep = value; - g_free(data); - return 0; + g_free( data ); + return 0; } -int -x49gp_module_set_u32(x49gp_module_t *module, GKeyFile *key, - const char *name, uint32_t value) +int x49gp_module_set_u32( x49gp_module_t* module, GKeyFile* key, const char* name, uint32_t value ) { - char data[16]; + char data[ 16 ]; - snprintf(data, sizeof(data), "0x%08x", value); + snprintf( data, sizeof( data ), "0x%08x", value ); - g_key_file_set_value(key, module->name, name, data); + g_key_file_set_value( key, module->name, name, data ); - return 0; + return 0; } -int -x49gp_module_set_u64(x49gp_module_t *module, GKeyFile *key, - const char *name, uint64_t value) +int x49gp_module_set_u64( x49gp_module_t* module, GKeyFile* key, const char* name, uint64_t value ) { - char data[32]; + char data[ 32 ]; - snprintf(data, sizeof(data), "0x%016" PRIx64 "", value); + snprintf( data, sizeof( data ), "0x%016" PRIx64 "", value ); - g_key_file_set_value(key, module->name, name, data); + g_key_file_set_value( key, module->name, name, data ); - return 0; + return 0; } -int -x49gp_module_get_u64(x49gp_module_t *module, GKeyFile *key, - const char *name, uint64_t reset, uint64_t *valuep) +int x49gp_module_get_u64( x49gp_module_t* module, GKeyFile* key, const char* name, uint64_t reset, uint64_t* valuep ) { - GError *gerror = NULL; - char *data, *end; - uint64_t value; + GError* gerror = NULL; + char *data, *end; + uint64_t value; - data = g_key_file_get_value(key, module->name, name, &gerror); - if (NULL == data) { - fprintf(stderr, "%s: %s:%u: key \"%s\" not found\n", - module->name, __FUNCTION__, __LINE__, name); - *valuep = reset; - return -EAGAIN; - } + data = g_key_file_get_value( key, module->name, name, &gerror ); + if ( NULL == data ) { + fprintf( stderr, "%s: %s:%u: key \"%s\" not found\n", module->name, __FUNCTION__, __LINE__, name ); + *valuep = reset; + return -EAGAIN; + } - value = strtoull(data, &end, 0); - if ((end == data) || (*end != '\0')) { - *valuep = reset; - g_free(data); - return -EAGAIN; - } + value = strtoull( data, &end, 0 ); + if ( ( end == data ) || ( *end != '\0' ) ) { + *valuep = reset; + g_free( data ); + return -EAGAIN; + } - *valuep = value; + *valuep = value; - g_free(data); - return 0; + g_free( data ); + return 0; } -int -x49gp_module_get_string(x49gp_module_t *module, GKeyFile *key, - const char *name, char *reset, char **valuep) +int x49gp_module_get_string( x49gp_module_t* module, GKeyFile* key, const char* name, char* reset, char** valuep ) { - GError *gerror = NULL; - char *data; + GError* gerror = NULL; + char* data; - data = g_key_file_get_value(key, module->name, name, &gerror); - if (NULL == data) { - fprintf(stderr, "%s: %s:%u: key \"%s\" not found\n", - module->name, __FUNCTION__, __LINE__, name); - *valuep = g_strdup(reset); - return -EAGAIN; - } + data = g_key_file_get_value( key, module->name, name, &gerror ); + if ( NULL == data ) { + fprintf( stderr, "%s: %s:%u: key \"%s\" not found\n", module->name, __FUNCTION__, __LINE__, name ); + *valuep = g_strdup( reset ); + return -EAGAIN; + } - *valuep = data; - return 0; + *valuep = data; + return 0; } -int x49gp_module_set_string(x49gp_module_t *module, GKeyFile *key, - const char *name, const char *value) +int x49gp_module_set_string( x49gp_module_t* module, GKeyFile* key, const char* name, const char* value ) { - g_key_file_set_value(key, module->name, name, value); + g_key_file_set_value( key, module->name, name, value ); - return 0; + return 0; } -int -x49gp_module_open_rodata(x49gp_module_t *module, const char *name, - char **path) +int x49gp_module_open_rodata( x49gp_module_t* module, const char* name, char** path ) { - x49gp_t *x49gp = module->x49gp; - int fd; - int error; + x49gp_t* x49gp = module->x49gp; + int fd; + int error; - *path = g_build_filename(x49gp->progpath, name, NULL); - if (NULL == *path) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->name, __FUNCTION__, __LINE__); - return -ENOMEM; - } + *path = g_build_filename( x49gp->progpath, name, NULL ); + if ( NULL == *path ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->name, __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - fd = open(*path, O_RDONLY); + fd = open( *path, O_RDONLY ); #ifdef X49GP_DATADIR - if (fd < 0 && (errno == EACCES || errno == ENOENT)) { - g_free(*path); + if ( fd < 0 && ( errno == EACCES || errno == ENOENT ) ) { + g_free( *path ); - *path = g_build_filename(X49GP_DATADIR, name, NULL); - if (NULL == *path) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->name, __FUNCTION__, __LINE__); - return -ENOMEM; - } + *path = g_build_filename( X49GP_DATADIR, name, NULL ); + if ( NULL == *path ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->name, __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - fd = open(*path, O_RDONLY); - } + fd = open( *path, O_RDONLY ); + } #endif - if (fd < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: open %s: %s\n", - module->name, __FUNCTION__, __LINE__, - *path, strerror(errno)); - g_free(*path); - *path = NULL; - return error; - } + if ( fd < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: open %s: %s\n", module->name, __FUNCTION__, __LINE__, *path, strerror( errno ) ); + g_free( *path ); + *path = NULL; + return error; + } - return fd; + return fd; } -int -x49gp_module_init(x49gp_t *x49gp, const char *name, - int (*init)(x49gp_module_t *), - int (*exit)(x49gp_module_t *), - int (*reset)(x49gp_module_t *, x49gp_reset_t), - int (*load)(x49gp_module_t *, GKeyFile *), - int (*save)(x49gp_module_t *, GKeyFile *), - void *user_data, x49gp_module_t **modulep) +int x49gp_module_init( x49gp_t* x49gp, const char* name, int ( *init )( x49gp_module_t* ), int ( *exit )( x49gp_module_t* ), + int ( *reset )( x49gp_module_t*, x49gp_reset_t ), int ( *load )( x49gp_module_t*, GKeyFile* ), + int ( *save )( x49gp_module_t*, GKeyFile* ), void* user_data, x49gp_module_t** modulep ) { - x49gp_module_t *module; + x49gp_module_t* module; - module = malloc(sizeof(x49gp_module_t)); - if (NULL == module) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - name, __FUNCTION__, __LINE__); - return -1; - } - memset(module, 0, sizeof(x49gp_module_t)); + module = malloc( sizeof( x49gp_module_t ) ); + if ( NULL == module ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", name, __FUNCTION__, __LINE__ ); + return -1; + } + memset( module, 0, sizeof( x49gp_module_t ) ); - module->name = name; + module->name = name; - module->init = init; - module->exit = exit; - module->reset = reset; - module->load = load; - module->save = save; + module->init = init; + module->exit = exit; + module->reset = reset; + module->load = load; + module->save = save; - module->user_data = user_data; + module->user_data = user_data; -// module->mutex = g_mutex_new(); - module->x49gp = x49gp; + // module->mutex = g_mutex_new(); + module->x49gp = x49gp; - *modulep = module; - return 0; + *modulep = module; + return 0; } diff --git a/src/s3c2410.c b/src/s3c2410.c index 2a1674c..22ac59e 100644 --- a/src/s3c2410.c +++ b/src/s3c2410.c @@ -34,34 +34,29 @@ * SPI Interface: 0x59000000 * SDI Interface: 0x5a000000 */ -int -x49gp_s3c2410_init(x49gp_t *x49gp) +int x49gp_s3c2410_init( x49gp_t* x49gp ) { - x49gp_s3c2410_sram_init(x49gp); - x49gp_s3c2410_memc_init(x49gp); - /* x49gp_s3c2410_usbhost_init(x49gp); */ - x49gp_s3c2410_intc_init(x49gp); - /* x49gp_s3c2410_dma_init(x49gp); */ - x49gp_s3c2410_power_init(x49gp); - x49gp_s3c2410_lcd_init(x49gp); - x49gp_s3c2410_nand_init(x49gp); - x49gp_s3c2410_uart_init(x49gp); - x49gp_s3c2410_timer_init(x49gp); - x49gp_s3c2410_usbdev_init(x49gp); - x49gp_s3c2410_watchdog_init(x49gp); - /* x49gp_s3c2410_i2c_init(x49gp); */ - /* x49gp_s3c2410_iis_init(x49gp); */ - x49gp_s3c2410_io_port_init(x49gp); - x49gp_s3c2410_rtc_init(x49gp); - x49gp_s3c2410_adc_init(x49gp); - x49gp_s3c2410_spi_init(x49gp); - x49gp_s3c2410_sdi_init(x49gp); + x49gp_s3c2410_sram_init( x49gp ); + x49gp_s3c2410_memc_init( x49gp ); + /* x49gp_s3c2410_usbhost_init(x49gp); */ + x49gp_s3c2410_intc_init( x49gp ); + /* x49gp_s3c2410_dma_init(x49gp); */ + x49gp_s3c2410_power_init( x49gp ); + x49gp_s3c2410_lcd_init( x49gp ); + x49gp_s3c2410_nand_init( x49gp ); + x49gp_s3c2410_uart_init( x49gp ); + x49gp_s3c2410_timer_init( x49gp ); + x49gp_s3c2410_usbdev_init( x49gp ); + x49gp_s3c2410_watchdog_init( x49gp ); + /* x49gp_s3c2410_i2c_init(x49gp); */ + /* x49gp_s3c2410_iis_init(x49gp); */ + x49gp_s3c2410_io_port_init( x49gp ); + x49gp_s3c2410_rtc_init( x49gp ); + x49gp_s3c2410_adc_init( x49gp ); + x49gp_s3c2410_spi_init( x49gp ); + x49gp_s3c2410_sdi_init( x49gp ); - return 0; + return 0; } -int -s3c2410_exit(x49gp_t *x49gp) -{ - return 0; -} +int s3c2410_exit( x49gp_t* x49gp ) { return 0; } diff --git a/src/s3c2410_adc.c b/src/s3c2410_adc.c index ebd47f3..3fdaf48 100644 --- a/src/s3c2410_adc.c +++ b/src/s3c2410_adc.c @@ -12,250 +12,216 @@ #include "x49gp.h" #include "s3c2410.h" - typedef struct { - uint32_t adccon; - uint32_t adctsc; - uint32_t adcdly; - uint32_t adcdat0; - uint32_t adcdat1; + uint32_t adccon; + uint32_t adctsc; + uint32_t adcdly; + uint32_t adcdat0; + uint32_t adcdat1; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; } s3c2410_adc_t; -static int -s3c2410_adc_data_init(s3c2410_adc_t *adc) +static int s3c2410_adc_data_init( s3c2410_adc_t* adc ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(ADC, ADCCON, 0x00003fc4, adc->adccon), - S3C2410_OFFSET(ADC, ADCTSC, 0x00000058, adc->adctsc), - S3C2410_OFFSET(ADC, ADCDLY, 0x000000ff, adc->adcdly), - S3C2410_OFFSET(ADC, ADCDAT0, 0x3ff, adc->adcdat0), - S3C2410_OFFSET(ADC, ADCDAT1, 0x3ff, adc->adcdat1), - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( ADC, ADCCON, 0x00003fc4, adc->adccon ), S3C2410_OFFSET( ADC, ADCTSC, 0x00000058, adc->adctsc ), + S3C2410_OFFSET( ADC, ADCDLY, 0x000000ff, adc->adcdly ), S3C2410_OFFSET( ADC, ADCDAT0, 0x3ff, adc->adcdat0 ), + S3C2410_OFFSET( ADC, ADCDAT1, 0x3ff, adc->adcdat1 ), + }; - memset(adc, 0, sizeof(s3c2410_adc_t)); + memset( adc, 0, sizeof( s3c2410_adc_t ) ); - adc->regs = malloc(sizeof(regs)); - if (NULL == adc->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + adc->regs = malloc( sizeof( regs ) ); + if ( NULL == adc->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(adc->regs, regs, sizeof(regs)); - adc->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( adc->regs, regs, sizeof( regs ) ); + adc->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static uint32_t -s3c2410_adc_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_adc_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_adc_t *adc = opaque; - s3c2410_offset_t *reg; + s3c2410_adc_t* adc = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(adc, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( adc, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(adc, offset); + reg = S3C2410_OFFSET_ENTRY( adc, offset ); #ifdef DEBUG_S3C2410_ADC - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-adc", S3C2410_ADC_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-adc", S3C2410_ADC_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - switch (offset) { - case S3C2410_ADC_ADCCON: - *(reg->datap) &= ~(0x0001); - *(reg->datap) |= 0x8000; - break; - default: - break; - } + switch ( offset ) { + case S3C2410_ADC_ADCCON: + *( reg->datap ) &= ~( 0x0001 ); + *( reg->datap ) |= 0x8000; + break; + default: + break; + } - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_adc_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_adc_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_adc_t *adc = opaque; - s3c2410_offset_t *reg; + s3c2410_adc_t* adc = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(adc, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( adc, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(adc, offset); + reg = S3C2410_OFFSET_ENTRY( adc, offset ); #ifdef DEBUG_S3C2410_ADC - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-adc", S3C2410_ADC_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-adc", S3C2410_ADC_BASE, reg->name, ( unsigned long )offset, data ); #endif - *(reg->datap) = data; + *( reg->datap ) = data; } -static int -s3c2410_adc_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_adc_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_adc_t *adc = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_adc_t* adc = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < adc->nr_regs; i++) { - reg = &adc->regs[i]; + for ( i = 0; i < adc->nr_regs; i++ ) { + reg = &adc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_adc_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_adc_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_adc_t *adc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_adc_t* adc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < adc->nr_regs; i++) { - reg = &adc->regs[i]; + for ( i = 0; i < adc->nr_regs; i++ ) { + reg = &adc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_adc_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_adc_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_adc_t *adc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_adc_t* adc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < adc->nr_regs; i++) { - reg = &adc->regs[i]; + for ( i = 0; i < adc->nr_regs; i++ ) { + reg = &adc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_adc_readfn[] = -{ - s3c2410_adc_read, - s3c2410_adc_read, - s3c2410_adc_read -}; +static CPUReadMemoryFunc* s3c2410_adc_readfn[] = { s3c2410_adc_read, s3c2410_adc_read, s3c2410_adc_read }; -static CPUWriteMemoryFunc *s3c2410_adc_writefn[] = -{ - s3c2410_adc_write, - s3c2410_adc_write, - s3c2410_adc_write -}; +static CPUWriteMemoryFunc* s3c2410_adc_writefn[] = { s3c2410_adc_write, s3c2410_adc_write, s3c2410_adc_write }; -static int -s3c2410_adc_init(x49gp_module_t *module) +static int s3c2410_adc_init( x49gp_module_t* module ) { - s3c2410_adc_t *adc; - int iotype; + s3c2410_adc_t* adc; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - adc = malloc(sizeof(s3c2410_adc_t)); - if (NULL == adc) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_adc_data_init(adc)) { - free(adc); - return -ENOMEM; - } + adc = malloc( sizeof( s3c2410_adc_t ) ); + if ( NULL == adc ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_adc_data_init( adc ) ) { + free( adc ); + return -ENOMEM; + } - module->user_data = adc; + module->user_data = adc; - iotype = cpu_register_io_memory(s3c2410_adc_readfn, - s3c2410_adc_writefn, adc); + iotype = cpu_register_io_memory( s3c2410_adc_readfn, s3c2410_adc_writefn, adc ); #ifdef DEBUG_S3C2410_ADC - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_ADC_BASE, S3C2410_MAP_SIZE, iotype); + cpu_register_physical_memory( S3C2410_ADC_BASE, S3C2410_MAP_SIZE, iotype ); - return 0; + return 0; } -static int -s3c2410_adc_exit(x49gp_module_t *module) +static int s3c2410_adc_exit( x49gp_module_t* module ) { - s3c2410_adc_t *adc; + s3c2410_adc_t* adc; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - adc = module->user_data; - if (adc->regs) - free(adc->regs); - free(adc); - } + if ( module->user_data ) { + adc = module->user_data; + if ( adc->regs ) + free( adc->regs ); + free( adc ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_adc_init(x49gp_t *x49gp) +int x49gp_s3c2410_adc_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-adc", - s3c2410_adc_init, - s3c2410_adc_exit, - s3c2410_adc_reset, - s3c2410_adc_load, - s3c2410_adc_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-adc", s3c2410_adc_init, s3c2410_adc_exit, s3c2410_adc_reset, s3c2410_adc_load, s3c2410_adc_save, + NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_arm.c b/src/s3c2410_arm.c index 9e4ad2e..4b034dc 100644 --- a/src/s3c2410_arm.c +++ b/src/s3c2410_arm.c @@ -1,7 +1,6 @@ /* $Id: s3c2410_arm.c,v 1.7 2008/12/11 12:18:17 ecd Exp $ */ - #include #include #include @@ -15,260 +14,247 @@ #include "qemu-git/cpu-all.h" -static int -s3c2410_arm_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_arm_load( x49gp_module_t* module, GKeyFile* key ) { - struct CPUARMState *env = module->user_data; - char name[32]; - int error = 0; - int i; + struct CPUARMState* env = module->user_data; + char name[ 32 ]; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - cpu_reset(env); - tlb_flush(env, 1); + cpu_reset( env ); + tlb_flush( env, 1 ); - for (i = 0; i < (sizeof(env->regs) / sizeof(env->regs[0])); i++) { - sprintf(name, "reg-%02u", i); - if (x49gp_module_get_u32(module, key, name, 0, &env->regs[i])) - error = -EAGAIN; - } - if (x49gp_module_get_u32(module, key, "cpsr", 0, &env->uncached_cpsr)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "spsr", 0, &env->spsr)) - error = -EAGAIN; - for (i = 0; i < (sizeof(env->banked_spsr) / sizeof(env->banked_spsr[0])); i++) { - sprintf(name, "banked-spsr-%02u", i); - if (x49gp_module_get_u32(module, key, name, 0, &env->banked_spsr[i])) - error = -EAGAIN; - } - for (i = 0; i < (sizeof(env->banked_r13) / sizeof(env->banked_r13[0])); i++) { - sprintf(name, "banked-r13-%02u", i); - if (x49gp_module_get_u32(module, key, name, 0, &env->banked_r13[i])) - error = -EAGAIN; - } - for (i = 0; i < (sizeof(env->banked_r14) / sizeof(env->banked_r14[0])); i++) { - sprintf(name, "banked-r14-%02u", i); - if (x49gp_module_get_u32(module, key, name, 0, &env->banked_r14[i])) - error = -EAGAIN; - } - for (i = 0; i < (sizeof(env->usr_regs) / sizeof(env->usr_regs[0])); i++) { - sprintf(name, "reg-usr-%02u", i); - if (x49gp_module_get_u32(module, key, name, - 0, &env->usr_regs[i])) - error = -EAGAIN; - } - for (i = 0; i < (sizeof(env->fiq_regs) / sizeof(env->fiq_regs[0])); i++) { - sprintf(name, "reg-fiq-%02u", i); - if (x49gp_module_get_u32(module, key, name, - 0, &env->fiq_regs[i])) - error = -EAGAIN; - } + for ( i = 0; i < ( sizeof( env->regs ) / sizeof( env->regs[ 0 ] ) ); i++ ) { + sprintf( name, "reg-%02u", i ); + if ( x49gp_module_get_u32( module, key, name, 0, &env->regs[ i ] ) ) + error = -EAGAIN; + } + if ( x49gp_module_get_u32( module, key, "cpsr", 0, &env->uncached_cpsr ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "spsr", 0, &env->spsr ) ) + error = -EAGAIN; + for ( i = 0; i < ( sizeof( env->banked_spsr ) / sizeof( env->banked_spsr[ 0 ] ) ); i++ ) { + sprintf( name, "banked-spsr-%02u", i ); + if ( x49gp_module_get_u32( module, key, name, 0, &env->banked_spsr[ i ] ) ) + error = -EAGAIN; + } + for ( i = 0; i < ( sizeof( env->banked_r13 ) / sizeof( env->banked_r13[ 0 ] ) ); i++ ) { + sprintf( name, "banked-r13-%02u", i ); + if ( x49gp_module_get_u32( module, key, name, 0, &env->banked_r13[ i ] ) ) + error = -EAGAIN; + } + for ( i = 0; i < ( sizeof( env->banked_r14 ) / sizeof( env->banked_r14[ 0 ] ) ); i++ ) { + sprintf( name, "banked-r14-%02u", i ); + if ( x49gp_module_get_u32( module, key, name, 0, &env->banked_r14[ i ] ) ) + error = -EAGAIN; + } + for ( i = 0; i < ( sizeof( env->usr_regs ) / sizeof( env->usr_regs[ 0 ] ) ); i++ ) { + sprintf( name, "reg-usr-%02u", i ); + if ( x49gp_module_get_u32( module, key, name, 0, &env->usr_regs[ i ] ) ) + error = -EAGAIN; + } + for ( i = 0; i < ( sizeof( env->fiq_regs ) / sizeof( env->fiq_regs[ 0 ] ) ); i++ ) { + sprintf( name, "reg-fiq-%02u", i ); + if ( x49gp_module_get_u32( module, key, name, 0, &env->fiq_regs[ i ] ) ) + error = -EAGAIN; + } - if (x49gp_module_get_u32(module, key, "CF", 0, &env->CF)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "VF", 0, &env->VF)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "NF", 0, &env->NF)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "ZF", 0, &env->ZF)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "QF", 0, &env->QF)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "thumb", 0, &env->thumb)) - error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "CF", 0, &env->CF ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "VF", 0, &env->VF ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "NF", 0, &env->NF ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "ZF", 0, &env->ZF ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "QF", 0, &env->QF ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "thumb", 0, &env->thumb ) ) + error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c0-cpuid", 0, &env->cp15.c0_cpuid)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c1-sys", 0, &env->cp15.c1_sys)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c1-coproc", 0, &env->cp15.c1_coproc)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c2-base0", 0, &env->cp15.c2_base0)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c2-base1", 0, &env->cp15.c2_base1)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c2-control", 0, &env->cp15.c2_control)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c2-mask", 0, &env->cp15.c2_mask)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c2-base-mask", 0, &env->cp15.c2_base_mask)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c2-data", 0, &env->cp15.c2_data)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c2-insn", 0, &env->cp15.c2_insn)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c3", 0, &env->cp15.c3)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c5-insn", 0, &env->cp15.c5_insn)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c5-data", 0, &env->cp15.c5_data)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c6-insn", 0, &env->cp15.c6_insn)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c6-data", 0, &env->cp15.c6_data)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c9-insn", 0, &env->cp15.c9_insn)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c9-data", 0, &env->cp15.c9_data)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c13-fcse", 0, &env->cp15.c13_fcse)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "cp15-c13-context", 0, &env->cp15.c13_context)) - error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c0-cpuid", 0, &env->cp15.c0_cpuid ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c1-sys", 0, &env->cp15.c1_sys ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c1-coproc", 0, &env->cp15.c1_coproc ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c2-base0", 0, &env->cp15.c2_base0 ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c2-base1", 0, &env->cp15.c2_base1 ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c2-control", 0, &env->cp15.c2_control ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c2-mask", 0, &env->cp15.c2_mask ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c2-base-mask", 0, &env->cp15.c2_base_mask ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c2-data", 0, &env->cp15.c2_data ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c2-insn", 0, &env->cp15.c2_insn ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c3", 0, &env->cp15.c3 ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c5-insn", 0, &env->cp15.c5_insn ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c5-data", 0, &env->cp15.c5_data ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c6-insn", 0, &env->cp15.c6_insn ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c6-data", 0, &env->cp15.c6_data ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c9-insn", 0, &env->cp15.c9_insn ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c9-data", 0, &env->cp15.c9_data ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c13-fcse", 0, &env->cp15.c13_fcse ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "cp15-c13-context", 0, &env->cp15.c13_context ) ) + error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "features", 0, &env->features)) - error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "features", 0, &env->features ) ) + error = -EAGAIN; - if (x49gp_module_get_int(module, key, "exception-index", 0, &env->exception_index)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "interrupt-request", 0, &env->interrupt_request)) - error = -EAGAIN; - if (x49gp_module_get_u32(module, key, "halted", 0, &env->halted)) - error = -EAGAIN; + if ( x49gp_module_get_int( module, key, "exception-index", 0, &env->exception_index ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "interrupt-request", 0, &env->interrupt_request ) ) + error = -EAGAIN; + if ( x49gp_module_get_u32( module, key, "halted", 0, &env->halted ) ) + error = -EAGAIN; - env->exception_index = -1; + env->exception_index = -1; - if (0 == error) { - if (env->halted) - module->x49gp->arm_idle = 1; - } else { - memset(&env->cp15, 0, sizeof(env->cp15)); - } + if ( 0 == error ) { + if ( env->halted ) + module->x49gp->arm_idle = 1; + } else { + memset( &env->cp15, 0, sizeof( env->cp15 ) ); + } -// s3c2410_arm_dump_state(state); + // s3c2410_arm_dump_state(state); - return error; + return error; } -static int -s3c2410_arm_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_arm_save( x49gp_module_t* module, GKeyFile* key ) { - struct CPUARMState *env = module->user_data; - char name[32]; - int i; + struct CPUARMState* env = module->user_data; + char name[ 32 ]; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < (sizeof(env->regs) / sizeof(env->regs[0])); i++) { - sprintf(name, "reg-%02u", i); - x49gp_module_set_u32(module, key, name, env->regs[i]); - } - x49gp_module_set_u32(module, key, "cpsr", env->uncached_cpsr); - x49gp_module_set_u32(module, key, "spsr", env->spsr); - for (i = 0; i < (sizeof(env->banked_spsr) / sizeof(env->banked_spsr[0])); i++) { - sprintf(name, "banked-spsr-%02u", i); - x49gp_module_set_u32(module, key, name, env->banked_spsr[i]); - } - for (i = 0; i < (sizeof(env->banked_r13) / sizeof(env->banked_r13[0])); i++) { - sprintf(name, "banked-r13-%02u", i); - x49gp_module_set_u32(module, key, name, env->banked_r13[i]); - } - for (i = 0; i < (sizeof(env->banked_r14) / sizeof(env->banked_r14[0])); i++) { - sprintf(name, "banked-r14-%02u", i); - x49gp_module_set_u32(module, key, name, env->banked_r14[i]); - } - for (i = 0; i < (sizeof(env->usr_regs) / sizeof(env->usr_regs[0])); i++) { - sprintf(name, "reg-usr-%02u", i); - x49gp_module_set_u32(module, key, name, env->usr_regs[i]); - } - for (i = 0; i < (sizeof(env->fiq_regs) / sizeof(env->fiq_regs[0])); i++) { - sprintf(name, "reg-fiq-%02u", i); - x49gp_module_set_u32(module, key, name, env->fiq_regs[i]); - } + for ( i = 0; i < ( sizeof( env->regs ) / sizeof( env->regs[ 0 ] ) ); i++ ) { + sprintf( name, "reg-%02u", i ); + x49gp_module_set_u32( module, key, name, env->regs[ i ] ); + } + x49gp_module_set_u32( module, key, "cpsr", env->uncached_cpsr ); + x49gp_module_set_u32( module, key, "spsr", env->spsr ); + for ( i = 0; i < ( sizeof( env->banked_spsr ) / sizeof( env->banked_spsr[ 0 ] ) ); i++ ) { + sprintf( name, "banked-spsr-%02u", i ); + x49gp_module_set_u32( module, key, name, env->banked_spsr[ i ] ); + } + for ( i = 0; i < ( sizeof( env->banked_r13 ) / sizeof( env->banked_r13[ 0 ] ) ); i++ ) { + sprintf( name, "banked-r13-%02u", i ); + x49gp_module_set_u32( module, key, name, env->banked_r13[ i ] ); + } + for ( i = 0; i < ( sizeof( env->banked_r14 ) / sizeof( env->banked_r14[ 0 ] ) ); i++ ) { + sprintf( name, "banked-r14-%02u", i ); + x49gp_module_set_u32( module, key, name, env->banked_r14[ i ] ); + } + for ( i = 0; i < ( sizeof( env->usr_regs ) / sizeof( env->usr_regs[ 0 ] ) ); i++ ) { + sprintf( name, "reg-usr-%02u", i ); + x49gp_module_set_u32( module, key, name, env->usr_regs[ i ] ); + } + for ( i = 0; i < ( sizeof( env->fiq_regs ) / sizeof( env->fiq_regs[ 0 ] ) ); i++ ) { + sprintf( name, "reg-fiq-%02u", i ); + x49gp_module_set_u32( module, key, name, env->fiq_regs[ i ] ); + } - x49gp_module_set_u32(module, key, "CF", env->CF); - x49gp_module_set_u32(module, key, "VF", env->VF); - x49gp_module_set_u32(module, key, "NF", env->NF); - x49gp_module_set_u32(module, key, "ZF", env->ZF); - x49gp_module_set_u32(module, key, "QF", env->QF); - x49gp_module_set_int(module, key, "thumb", env->thumb); + x49gp_module_set_u32( module, key, "CF", env->CF ); + x49gp_module_set_u32( module, key, "VF", env->VF ); + x49gp_module_set_u32( module, key, "NF", env->NF ); + x49gp_module_set_u32( module, key, "ZF", env->ZF ); + x49gp_module_set_u32( module, key, "QF", env->QF ); + x49gp_module_set_int( module, key, "thumb", env->thumb ); - x49gp_module_set_u32(module, key, "cp15-c0-cpuid", env->cp15.c0_cpuid); - x49gp_module_set_u32(module, key, "cp15-c1-sys", env->cp15.c1_sys); - x49gp_module_set_u32(module, key, "cp15-c1-coproc", env->cp15.c1_coproc); - x49gp_module_set_u32(module, key, "cp15-c2-base0", env->cp15.c2_base0); - x49gp_module_set_u32(module, key, "cp15-c2-base1", env->cp15.c2_base1); - x49gp_module_set_u32(module, key, "cp15-c2-control", env->cp15.c2_control); - x49gp_module_set_u32(module, key, "cp15-c2-mask", env->cp15.c2_mask); - x49gp_module_set_u32(module, key, "cp15-c2-base-mask", env->cp15.c2_base_mask); - x49gp_module_set_u32(module, key, "cp15-c2-data", env->cp15.c2_data); - x49gp_module_set_u32(module, key, "cp15-c2-insn", env->cp15.c2_insn); - x49gp_module_set_u32(module, key, "cp15-c3", env->cp15.c3); - x49gp_module_set_u32(module, key, "cp15-c5-insn", env->cp15.c5_insn); - x49gp_module_set_u32(module, key, "cp15-c5-data", env->cp15.c5_data); - x49gp_module_set_u32(module, key, "cp15-c6-insn", env->cp15.c6_insn); - x49gp_module_set_u32(module, key, "cp15-c6-data", env->cp15.c6_data); - x49gp_module_set_u32(module, key, "cp15-c9-insn", env->cp15.c9_insn); - x49gp_module_set_u32(module, key, "cp15-c9-data", env->cp15.c9_data); - x49gp_module_set_u32(module, key, "cp15-c13-fcse", env->cp15.c13_fcse); - x49gp_module_set_u32(module, key, "cp15-c13-context", env->cp15.c13_context); + x49gp_module_set_u32( module, key, "cp15-c0-cpuid", env->cp15.c0_cpuid ); + x49gp_module_set_u32( module, key, "cp15-c1-sys", env->cp15.c1_sys ); + x49gp_module_set_u32( module, key, "cp15-c1-coproc", env->cp15.c1_coproc ); + x49gp_module_set_u32( module, key, "cp15-c2-base0", env->cp15.c2_base0 ); + x49gp_module_set_u32( module, key, "cp15-c2-base1", env->cp15.c2_base1 ); + x49gp_module_set_u32( module, key, "cp15-c2-control", env->cp15.c2_control ); + x49gp_module_set_u32( module, key, "cp15-c2-mask", env->cp15.c2_mask ); + x49gp_module_set_u32( module, key, "cp15-c2-base-mask", env->cp15.c2_base_mask ); + x49gp_module_set_u32( module, key, "cp15-c2-data", env->cp15.c2_data ); + x49gp_module_set_u32( module, key, "cp15-c2-insn", env->cp15.c2_insn ); + x49gp_module_set_u32( module, key, "cp15-c3", env->cp15.c3 ); + x49gp_module_set_u32( module, key, "cp15-c5-insn", env->cp15.c5_insn ); + x49gp_module_set_u32( module, key, "cp15-c5-data", env->cp15.c5_data ); + x49gp_module_set_u32( module, key, "cp15-c6-insn", env->cp15.c6_insn ); + x49gp_module_set_u32( module, key, "cp15-c6-data", env->cp15.c6_data ); + x49gp_module_set_u32( module, key, "cp15-c9-insn", env->cp15.c9_insn ); + x49gp_module_set_u32( module, key, "cp15-c9-data", env->cp15.c9_data ); + x49gp_module_set_u32( module, key, "cp15-c13-fcse", env->cp15.c13_fcse ); + x49gp_module_set_u32( module, key, "cp15-c13-context", env->cp15.c13_context ); - x49gp_module_set_u32(module, key, "features", env->features); + x49gp_module_set_u32( module, key, "features", env->features ); - x49gp_module_set_int(module, key, "exception-index", env->exception_index); - x49gp_module_set_int(module, key, "interrupt-request", env->interrupt_request); - x49gp_module_set_int(module, key, "halted", env->halted); + x49gp_module_set_int( module, key, "exception-index", env->exception_index ); + x49gp_module_set_int( module, key, "interrupt-request", env->interrupt_request ); + x49gp_module_set_int( module, key, "halted", env->halted ); - return 0; + return 0; } -static int -s3c2410_arm_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_arm_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - struct CPUARMState *env = module->user_data; + struct CPUARMState* env = module->user_data; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - cpu_reset(env); - tlb_flush(env, 1); + cpu_reset( env ); + tlb_flush( env, 1 ); - return 0; + return 0; } -static int -s3c2410_arm_init(x49gp_module_t *module) +static int s3c2410_arm_init( x49gp_module_t* module ) { - x49gp_t *x49gp = module->x49gp; + x49gp_t* x49gp = module->x49gp; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - module->user_data = x49gp->env; - return 0; + module->user_data = x49gp->env; + return 0; } -static int -s3c2410_arm_exit(x49gp_module_t *module) +static int s3c2410_arm_exit( x49gp_module_t* module ) { #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - return 0; + return 0; } -int -x49gp_s3c2410_arm_init(x49gp_t *x49gp) +int x49gp_s3c2410_arm_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-arm", - s3c2410_arm_init, - s3c2410_arm_exit, - s3c2410_arm_reset, - s3c2410_arm_load, - s3c2410_arm_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-arm", s3c2410_arm_init, s3c2410_arm_exit, s3c2410_arm_reset, s3c2410_arm_load, s3c2410_arm_save, + NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_intc.c b/src/s3c2410_intc.c index e89a913..c29b3f9 100644 --- a/src/s3c2410_intc.c +++ b/src/s3c2410_intc.c @@ -16,729 +16,664 @@ extern int do_trace; typedef struct { - int sel_shift; - uint32_t mode_bit; - int index; - int req[6]; + int sel_shift; + uint32_t mode_bit; + int index; + int req[ 6 ]; } s3c2410_arb_t; typedef struct { - uint32_t sel; - uint32_t mode; + uint32_t sel; + uint32_t mode; } s3c2410_arb_data_t; -static const int s3c2410_arb_order[4][6] = -{ - { 0, 1, 2, 3, 4, 5 }, - { 0, 2, 3, 4, 1, 5 }, - { 0, 3, 4, 1, 2, 5 }, - { 0, 4, 1, 2, 3, 5 } +static const int s3c2410_arb_order[ 4 ][ 6 ] = { + {0, 1, 2, 3, 4, 5}, + {0, 2, 3, 4, 1, 5}, + {0, 3, 4, 1, 2, 5}, + {0, 4, 1, 2, 3, 5} }; -static const s3c2410_arb_t s3c2410_arb_table[] = -{ - [0] = { ARB0_SEL_SHIFT, ARB0_MODE, 0, - { -1, EINT0, EINT1, EINT2, EINT3, -1 } }, - [1] = { ARB1_SEL_SHIFT, ARB1_MODE, 1, - { EINT4_7, EINT8_23, -1, nBATT_FLT, INT_TICK, INT_WDT } }, - [2] = { ARB2_SEL_SHIFT, ARB2_MODE, 2, - { INT_TIMER0, INT_TIMER1, INT_TIMER2, INT_TIMER3, INT_TIMER4, INT_UART2} }, - [3] = { ARB3_SEL_SHIFT, ARB3_MODE, 3, - { INT_LCD, INT_DMA0, INT_DMA1, INT_DMA2, INT_DMA3, INT_SDI } }, - [4] = { ARB4_SEL_SHIFT, ARB4_MODE, 4, - { INT_SPI0, INT_UART1, -1, INT_USBD, INT_USBH, INT_IIC } }, - [5] = { ARB5_SEL_SHIFT, ARB5_MODE, 5, - { -1, INT_UART0, INT_SPI1, INT_RTC, INT_ADC, -1, } }, - [6] = { ARB6_SEL_SHIFT, ARB6_MODE, 6, - { 0, 1, 2, 3, 4, 5 } }, +static const s3c2410_arb_t s3c2410_arb_table[] = { + [0] = {ARB0_SEL_SHIFT, ARB0_MODE, 0, { -1, EINT0, EINT1, EINT2, EINT3, -1 } }, + [1] = {ARB1_SEL_SHIFT, ARB1_MODE, 1, { EINT4_7, EINT8_23, -1, nBATT_FLT, INT_TICK, INT_WDT } }, + [2] = {ARB2_SEL_SHIFT, ARB2_MODE, 2, { INT_TIMER0, INT_TIMER1, INT_TIMER2, INT_TIMER3, INT_TIMER4, INT_UART2 }}, + [3] = {ARB3_SEL_SHIFT, ARB3_MODE, 3, { INT_LCD, INT_DMA0, INT_DMA1, INT_DMA2, INT_DMA3, INT_SDI } }, + [4] = {ARB4_SEL_SHIFT, ARB4_MODE, 4, { INT_SPI0, INT_UART1, -1, INT_USBD, INT_USBH, INT_IIC } }, + [5] = {ARB5_SEL_SHIFT, + ARB5_MODE, 5, + { + -1, + INT_UART0, + INT_SPI1, + INT_RTC, + INT_ADC, + -1, + } }, + [6] = {ARB6_SEL_SHIFT, ARB6_MODE, 6, { 0, 1, 2, 3, 4, 5 } }, }; -#define INTC_NR_ARB (sizeof(s3c2410_arb_table) / sizeof(s3c2410_arb_table[0])) - +#define INTC_NR_ARB ( sizeof( s3c2410_arb_table ) / sizeof( s3c2410_arb_table[ 0 ] ) ) typedef struct { - uint32_t srcpnd; - uint32_t intmod; - uint32_t intmsk; - uint32_t priority; - uint32_t intpnd; - uint32_t intoffset; - uint32_t subsrcpnd; - uint32_t intsubmsk; + uint32_t srcpnd; + uint32_t intmod; + uint32_t intmsk; + uint32_t priority; + uint32_t intpnd; + uint32_t intoffset; + uint32_t subsrcpnd; + uint32_t intsubmsk; - s3c2410_arb_data_t arb_data[INTC_NR_ARB]; + s3c2410_arb_data_t arb_data[ INTC_NR_ARB ]; - x49gp_t *x49gp; + x49gp_t* x49gp; - uint32_t src_pending; - uint32_t subsrc_pending; + uint32_t src_pending; + uint32_t subsrc_pending; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; } s3c2410_intc_t; +static void s3c2410_intc_gen_int( s3c2410_intc_t* intc ); +static void s3c2410_intc_gen_int_from_sub_int( s3c2410_intc_t* intc ); -static void s3c2410_intc_gen_int(s3c2410_intc_t *intc); -static void s3c2410_intc_gen_int_from_sub_int(s3c2410_intc_t *intc); - -static int -s3c2410_intc_data_init(s3c2410_intc_t *intc) +static int s3c2410_intc_data_init( s3c2410_intc_t* intc ) { - int i; + int i; - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(INTC, SRCPND, 0x00000000, intc->srcpnd), - S3C2410_OFFSET(INTC, INTMOD, 0x00000000, intc->intmod), - S3C2410_OFFSET(INTC, INTMSK, 0xffffffff, intc->intmsk), - S3C2410_OFFSET(INTC, PRIORITY, 0x0000007f, intc->priority), - S3C2410_OFFSET(INTC, INTPND, 0x00000000, intc->intpnd), - S3C2410_OFFSET(INTC, INTOFFSET, 0x00000000, intc->intoffset), - S3C2410_OFFSET(INTC, SUBSRCPND, 0x00000000, intc->subsrcpnd), - S3C2410_OFFSET(INTC, INTSUBMSK, 0x000007ff, intc->intsubmsk) - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( INTC, SRCPND, 0x00000000, intc->srcpnd ), S3C2410_OFFSET( INTC, INTMOD, 0x00000000, intc->intmod ), + S3C2410_OFFSET( INTC, INTMSK, 0xffffffff, intc->intmsk ), S3C2410_OFFSET( INTC, PRIORITY, 0x0000007f, intc->priority ), + S3C2410_OFFSET( INTC, INTPND, 0x00000000, intc->intpnd ), S3C2410_OFFSET( INTC, INTOFFSET, 0x00000000, intc->intoffset ), + S3C2410_OFFSET( INTC, SUBSRCPND, 0x00000000, intc->subsrcpnd ), S3C2410_OFFSET( INTC, INTSUBMSK, 0x000007ff, intc->intsubmsk ) }; - memset(intc, 0, sizeof(s3c2410_intc_t)); + memset( intc, 0, sizeof( s3c2410_intc_t ) ); - intc->regs = malloc(sizeof(regs)); - if (NULL == intc->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + intc->regs = malloc( sizeof( regs ) ); + if ( NULL == intc->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(intc->regs, regs, sizeof(regs)); - intc->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( intc->regs, regs, sizeof( regs ) ); + intc->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - for (i = 0; i < INTC_NR_ARB; i++) { - intc->arb_data[i].sel = 0; - intc->arb_data[i].mode = s3c2410_arb_table[i].mode_bit; - } + for ( i = 0; i < INTC_NR_ARB; i++ ) { + intc->arb_data[ i ].sel = 0; + intc->arb_data[ i ].mode = s3c2410_arb_table[ i ].mode_bit; + } - return 0; + return 0; } -static void -srcpnd_put_word(s3c2410_intc_t *intc, uint32_t data) +static void srcpnd_put_word( s3c2410_intc_t* intc, uint32_t data ) { - intc->srcpnd &= ~(data); - intc->srcpnd |= intc->src_pending; + intc->srcpnd &= ~( data ); + intc->srcpnd |= intc->src_pending; - if (intc->src_pending & data) { - s3c2410_intc_gen_int(intc); - } + if ( intc->src_pending & data ) { + s3c2410_intc_gen_int( intc ); + } } -static void -intmod_put_word(s3c2410_intc_t *intc, uint32_t data) +static void intmod_put_word( s3c2410_intc_t* intc, uint32_t data ) { - intc->intmod = data & 0xfeffffbf; + intc->intmod = data & 0xfeffffbf; - s3c2410_intc_gen_int(intc); + s3c2410_intc_gen_int( intc ); } -static void -intmsk_put_word(s3c2410_intc_t *intc, uint32_t data) +static void intmsk_put_word( s3c2410_intc_t* intc, uint32_t data ) { #ifdef DEBUG_X49GP_ENABLE_IRQ - uint32_t change; - int i; + uint32_t change; + int i; - change = intc->intmsk ^ data; + change = intc->intmsk ^ data; #endif - intc->intmsk = data | 0x01000040; + intc->intmsk = data | 0x01000040; #ifdef DEBUG_X49GP_ENABLE_IRQ - for (i = 0; i < 32; i++) { - if ((change & (1 << i)) && !(intc->intmsk & (1 << i))) { - printf("INTC: Enable IRQ %u\n", i); - } - } + for ( i = 0; i < 32; i++ ) { + if ( ( change & ( 1 << i ) ) && !( intc->intmsk & ( 1 << i ) ) ) { + printf( "INTC: Enable IRQ %u\n", i ); + } + } #endif - s3c2410_intc_gen_int(intc); + s3c2410_intc_gen_int( intc ); } -static uint32_t -priority_get_word(s3c2410_intc_t *intc) +static uint32_t priority_get_word( s3c2410_intc_t* intc ) { - const s3c2410_arb_t *arb; - s3c2410_arb_data_t *arb_data; - int i; + const s3c2410_arb_t* arb; + s3c2410_arb_data_t* arb_data; + int i; - intc->priority = 0; + intc->priority = 0; - for (i = 0; i < INTC_NR_ARB; i++) { - arb = &s3c2410_arb_table[i]; - arb_data = &intc->arb_data[i]; + for ( i = 0; i < INTC_NR_ARB; i++ ) { + arb = &s3c2410_arb_table[ i ]; + arb_data = &intc->arb_data[ i ]; - intc->priority |= (arb_data->sel << arb->sel_shift) | - arb_data->mode; - } + intc->priority |= ( arb_data->sel << arb->sel_shift ) | arb_data->mode; + } - return intc->priority; + return intc->priority; } -static void -priority_put_word(s3c2410_intc_t *intc, uint32_t data) +static void priority_put_word( s3c2410_intc_t* intc, uint32_t data ) { - const s3c2410_arb_t *arb; - s3c2410_arb_data_t *arb_data; - int i; + const s3c2410_arb_t* arb; + s3c2410_arb_data_t* arb_data; + int i; - intc->priority = data & 0x001fffff; + intc->priority = data & 0x001fffff; - for (i = 0; i < INTC_NR_ARB; i++) { - arb = &s3c2410_arb_table[i]; - arb_data = &intc->arb_data[i]; + for ( i = 0; i < INTC_NR_ARB; i++ ) { + arb = &s3c2410_arb_table[ i ]; + arb_data = &intc->arb_data[ i ]; - arb_data->sel = (intc->priority >> arb->sel_shift) & ARBx_SEL_MASK; - arb_data->mode = intc->priority & arb->mode_bit; - } + arb_data->sel = ( intc->priority >> arb->sel_shift ) & ARBx_SEL_MASK; + arb_data->mode = intc->priority & arb->mode_bit; + } - s3c2410_intc_gen_int(intc); + s3c2410_intc_gen_int( intc ); } -static void -intpnd_put_word(s3c2410_intc_t *intc, uint32_t data) +static void intpnd_put_word( s3c2410_intc_t* intc, uint32_t data ) { - intc->intpnd &= ~(data); + intc->intpnd &= ~( data ); - s3c2410_intc_gen_int(intc); + s3c2410_intc_gen_int( intc ); } -static void -subsrcpnd_put_word(s3c2410_intc_t *intc, uint32_t data) +static void subsrcpnd_put_word( s3c2410_intc_t* intc, uint32_t data ) { - intc->subsrcpnd &= ~(data); - intc->subsrcpnd |= intc->subsrc_pending; + intc->subsrcpnd &= ~( data ); + intc->subsrcpnd |= intc->subsrc_pending; - if (intc->subsrc_pending & data) { - s3c2410_intc_gen_int_from_sub_int(intc); - } + if ( intc->subsrc_pending & data ) { + s3c2410_intc_gen_int_from_sub_int( intc ); + } } -static void -intsubmsk_put_word(s3c2410_intc_t *intc, uint32_t data) +static void intsubmsk_put_word( s3c2410_intc_t* intc, uint32_t data ) { - intc->intsubmsk = data & 0x000007ff; + intc->intsubmsk = data & 0x000007ff; - s3c2410_intc_gen_int_from_sub_int(intc); + s3c2410_intc_gen_int_from_sub_int( intc ); } -static uint32_t -s3c2410_intc_select_int(s3c2410_intc_t *intc, const s3c2410_arb_t *arb, - uint32_t service, int *offset) +static uint32_t s3c2410_intc_select_int( s3c2410_intc_t* intc, const s3c2410_arb_t* arb, uint32_t service, int* offset ) { - s3c2410_arb_data_t *arb_data = &intc->arb_data[arb->index]; - const int *order; - int i, req; + s3c2410_arb_data_t* arb_data = &intc->arb_data[ arb->index ]; + const int* order; + int i, req; - order = s3c2410_arb_order[arb_data->sel]; + order = s3c2410_arb_order[ arb_data->sel ]; - for (i = 0; i < 6; i++) { - req = order[i]; + for ( i = 0; i < 6; i++ ) { + req = order[ i ]; - if (-1 == arb->req[req]) - continue; + if ( -1 == arb->req[ req ] ) + continue; - if (service & (1 << arb->req[req])) { - if (arb_data->mode) - arb_data->sel = (arb_data->sel + 1) & ARBx_SEL_MASK; - *offset = arb->req[req]; - return (1 << arb->index); - } - } + if ( service & ( 1 << arb->req[ req ] ) ) { + if ( arb_data->mode ) + arb_data->sel = ( arb_data->sel + 1 ) & ARBx_SEL_MASK; + *offset = arb->req[ req ]; + return ( 1 << arb->index ); + } + } - *offset = -1; - return 0; + *offset = -1; + return 0; } -void -s3c2410_FIQ (CPUState *env) +void s3c2410_FIQ( CPUState* env ) { cpu_interrupt( env, CPU_INTERRUPT_FIQ ); } + +void s3c2410_IRQ( CPUState* env ) { cpu_interrupt( env, CPU_INTERRUPT_HARD ); } + +static void s3c2410_intc_gen_int( s3c2410_intc_t* intc ) { - cpu_interrupt(env, CPU_INTERRUPT_FIQ); -} + x49gp_t* x49gp = intc->x49gp; + uint32_t fiq, service; + int offset[ 6 ], index; + const s3c2410_arb_t* arb; + uint32_t request; + int i; -void -s3c2410_IRQ (CPUState *env) -{ - cpu_interrupt(env, CPU_INTERRUPT_HARD); -} - - -static void -s3c2410_intc_gen_int(s3c2410_intc_t *intc) -{ - x49gp_t *x49gp = intc->x49gp; - uint32_t fiq, service; - int offset[6], index; - const s3c2410_arb_t *arb; - uint32_t request; - int i; - - fiq = intc->srcpnd & intc->intmod; + fiq = intc->srcpnd & intc->intmod; #ifdef DEBUG_S3C2410_INTC0 - printf("INTC: FIQ service request: %08x\n", fiq); + printf( "INTC: FIQ service request: %08x\n", fiq ); #endif - if (fiq) { - /* - * Generate FIQ. - */ + if ( fiq ) { + /* + * Generate FIQ. + */ #ifdef DEBUG_S3C2410_INTC - printf("INTC: vector to %08x\n", 0x1c); + printf( "INTC: vector to %08x\n", 0x1c ); #endif - cpu_interrupt(x49gp->env, CPU_INTERRUPT_FIQ); + cpu_interrupt( x49gp->env, CPU_INTERRUPT_FIQ ); - x49gp_set_idle(x49gp, 0); - return; - } else { - cpu_reset_interrupt(x49gp->env, CPU_INTERRUPT_FIQ); - } + x49gp_set_idle( x49gp, 0 ); + return; + } else { + cpu_reset_interrupt( x49gp->env, CPU_INTERRUPT_FIQ ); + } #ifdef DEBUG_S3C2410_INTC0 - printf("INTC: IRQ pending request: %08x\n", intc->intpnd); + printf( "INTC: IRQ pending request: %08x\n", intc->intpnd ); #endif - if (intc->intpnd) { - /* - * Generate IRQ. - */ + if ( intc->intpnd ) { + /* + * Generate IRQ. + */ #ifdef DEBUG_S3C2410_INTC - printf("INTC: vector to %08x\n", 0x18); + printf( "INTC: vector to %08x\n", 0x18 ); #endif - cpu_interrupt(x49gp->env, CPU_INTERRUPT_HARD); + cpu_interrupt( x49gp->env, CPU_INTERRUPT_HARD ); - x49gp_set_idle(x49gp, 0); - return; - } + x49gp_set_idle( x49gp, 0 ); + return; + } #ifdef DEBUG_S3C2410_INTC0 - printf("INTC: srcpnd %08x, intmsk: %08x\n", intc->srcpnd, intc->intmsk); + printf( "INTC: srcpnd %08x, intmsk: %08x\n", intc->srcpnd, intc->intmsk ); #endif - service = intc->srcpnd & ~(intc->intmsk); + service = intc->srcpnd & ~( intc->intmsk ); #ifdef DEBUG_S3C2410_INTC0 - printf("INTC: IRQ service request: %08x\n", service); + printf( "INTC: IRQ service request: %08x\n", service ); #endif - if (0 == service) { - cpu_reset_interrupt(x49gp->env, CPU_INTERRUPT_HARD); - return; - } + if ( 0 == service ) { + cpu_reset_interrupt( x49gp->env, CPU_INTERRUPT_HARD ); + return; + } - request = 0; - for (i = 0; i < 6; i++) { - arb = &s3c2410_arb_table[i]; + request = 0; + for ( i = 0; i < 6; i++ ) { + arb = &s3c2410_arb_table[ i ]; - request |= s3c2410_intc_select_int(intc, arb, service, &offset[i]); + request |= s3c2410_intc_select_int( intc, arb, service, &offset[ i ] ); #ifdef DEBUG_S3C2410_INTC0 - printf("INTC: ARB%u highest %d\n", i, offset[i]); + printf( "INTC: ARB%u highest %d\n", i, offset[ i ] ); #endif - } + } - arb = &s3c2410_arb_table[6]; + arb = &s3c2410_arb_table[ 6 ]; #ifdef DEBUG_S3C2410_INTC0 - printf("INTC: ARB%u request: %08x\n", 6, request); + printf( "INTC: ARB%u request: %08x\n", 6, request ); #endif - if (s3c2410_intc_select_int(intc, arb, request, &index)) { - intc->intoffset = offset[index]; - intc->intpnd |= (1 << intc->intoffset); + if ( s3c2410_intc_select_int( intc, arb, request, &index ) ) { + intc->intoffset = offset[ index ]; + intc->intpnd |= ( 1 << intc->intoffset ); #ifdef DEBUG_S3C2410_INTC - printf("INTC: irq pending: %u (%08x)\n", intc->intoffset, intc->intpnd); + printf( "INTC: irq pending: %u (%08x)\n", intc->intoffset, intc->intpnd ); #endif - /* - * Generate IRQ. - */ + /* + * Generate IRQ. + */ #ifdef DEBUG_S3C2410_INTC - printf("INTC: vector to %08x\n", 0x18); + printf( "INTC: vector to %08x\n", 0x18 ); #endif - cpu_interrupt(x49gp->env, CPU_INTERRUPT_HARD); + cpu_interrupt( x49gp->env, CPU_INTERRUPT_HARD ); - x49gp_set_idle(x49gp, 0); - return; - } + x49gp_set_idle( x49gp, 0 ); + return; + } #ifdef DEBUG_S3C2410_INTC0 - printf("INTC: No irq pending\n"); + printf( "INTC: No irq pending\n" ); #endif - cpu_reset_interrupt(x49gp->env, CPU_INTERRUPT_HARD); + cpu_reset_interrupt( x49gp->env, CPU_INTERRUPT_HARD ); } -void -s3c2410_intc_assert(x49gp_t *x49gp, int irq, int level) +void s3c2410_intc_assert( x49gp_t* x49gp, int irq, int level ) { - s3c2410_intc_t *intc = x49gp->s3c2410_intc; + s3c2410_intc_t* intc = x49gp->s3c2410_intc; - if (irq > 31) - return; + if ( irq > 31 ) + return; #ifdef DEBUG_S3C2410_INTC - printf("INTC: assert irq %u (%08x)\n", irq, 1 << irq); + printf( "INTC: assert irq %u (%08x)\n", irq, 1 << irq ); #endif - if (! (intc->src_pending & (1 << irq))) { - if (level) - intc->src_pending |= (1 << irq); - intc->srcpnd |= (1 << irq); + if ( !( intc->src_pending & ( 1 << irq ) ) ) { + if ( level ) + intc->src_pending |= ( 1 << irq ); + intc->srcpnd |= ( 1 << irq ); - s3c2410_intc_gen_int(intc); - } + s3c2410_intc_gen_int( intc ); + } - if (x49gp->arm_idle == 2) { - if (irq == EINT0 || irq == INT_RTC) - x49gp_set_idle(x49gp, 0); - } + if ( x49gp->arm_idle == 2 ) { + if ( irq == EINT0 || irq == INT_RTC ) + x49gp_set_idle( x49gp, 0 ); + } } -void -s3c2410_intc_deassert(x49gp_t *x49gp, int irq) +void s3c2410_intc_deassert( x49gp_t* x49gp, int irq ) { - s3c2410_intc_t *intc = x49gp->s3c2410_intc; + s3c2410_intc_t* intc = x49gp->s3c2410_intc; - if (irq > 31) - return; + if ( irq > 31 ) + return; #ifdef DEBUG_S3C2410_INTC - printf("INTC: deassert irq %u (%08x)\n", irq, 1 << irq); + printf( "INTC: deassert irq %u (%08x)\n", irq, 1 << irq ); #endif - intc->src_pending &= ~(1 << irq); + intc->src_pending &= ~( 1 << irq ); } -static void -s3c2410_intc_gen_int_from_sub_int(s3c2410_intc_t *intc) +static void s3c2410_intc_gen_int_from_sub_int( s3c2410_intc_t* intc ) { - x49gp_t *x49gp = intc->x49gp; - uint32_t service; + x49gp_t* x49gp = intc->x49gp; + uint32_t service; - service = intc->subsrcpnd & ~(intc->intsubmsk); + service = intc->subsrcpnd & ~( intc->intsubmsk ); #ifdef DEBUG_S3C2410_INTC - printf("INTC: subirq service request: %08x\n", service); + printf( "INTC: subirq service request: %08x\n", service ); #endif - if (service & ((1 << SUB_INT_ERR0) | (1 << SUB_INT_TXD0) | (1 << SUB_INT_RXD0))) { - s3c2410_intc_assert(x49gp, INT_UART0, 1); - } else { - s3c2410_intc_deassert(x49gp, INT_UART0); - } + if ( service & ( ( 1 << SUB_INT_ERR0 ) | ( 1 << SUB_INT_TXD0 ) | ( 1 << SUB_INT_RXD0 ) ) ) { + s3c2410_intc_assert( x49gp, INT_UART0, 1 ); + } else { + s3c2410_intc_deassert( x49gp, INT_UART0 ); + } - if (service & ((1 << SUB_INT_ERR1) | (1 << SUB_INT_TXD1) | (1 << SUB_INT_RXD1))) { - s3c2410_intc_assert(x49gp, INT_UART1, 1); - } else { - s3c2410_intc_deassert(x49gp, INT_UART1); - } + if ( service & ( ( 1 << SUB_INT_ERR1 ) | ( 1 << SUB_INT_TXD1 ) | ( 1 << SUB_INT_RXD1 ) ) ) { + s3c2410_intc_assert( x49gp, INT_UART1, 1 ); + } else { + s3c2410_intc_deassert( x49gp, INT_UART1 ); + } - if (service & ((1 << SUB_INT_ERR2) | (1 << SUB_INT_TXD2) | (1 << SUB_INT_RXD2))) { - s3c2410_intc_assert(x49gp, INT_UART2, 1); - } else { - s3c2410_intc_deassert(x49gp, INT_UART2); - } + if ( service & ( ( 1 << SUB_INT_ERR2 ) | ( 1 << SUB_INT_TXD2 ) | ( 1 << SUB_INT_RXD2 ) ) ) { + s3c2410_intc_assert( x49gp, INT_UART2, 1 ); + } else { + s3c2410_intc_deassert( x49gp, INT_UART2 ); + } - if (service & ((1 << SUB_INT_ADC) | (1 << SUB_INT_TC))) { - s3c2410_intc_assert(x49gp, INT_ADC, 1); - } else { - s3c2410_intc_deassert(x49gp, INT_ADC); - } + if ( service & ( ( 1 << SUB_INT_ADC ) | ( 1 << SUB_INT_TC ) ) ) { + s3c2410_intc_assert( x49gp, INT_ADC, 1 ); + } else { + s3c2410_intc_deassert( x49gp, INT_ADC ); + } - intc->subsrcpnd = intc->subsrc_pending; + intc->subsrcpnd = intc->subsrc_pending; } -void -s3c2410_intc_sub_assert(x49gp_t *x49gp, int sub_irq, int level) +void s3c2410_intc_sub_assert( x49gp_t* x49gp, int sub_irq, int level ) { - s3c2410_intc_t *intc = x49gp->s3c2410_intc; + s3c2410_intc_t* intc = x49gp->s3c2410_intc; - if (sub_irq > 31) - return; + if ( sub_irq > 31 ) + return; #ifdef DEBUG_S3C2410_INTC - printf("INTC: assert subirq %u (%08x)\n", sub_irq, 1 << sub_irq); + printf( "INTC: assert subirq %u (%08x)\n", sub_irq, 1 << sub_irq ); #endif - if (! (intc->subsrc_pending & (1 << sub_irq))) { - if (level) - intc->subsrc_pending |= (1 << sub_irq); - intc->subsrcpnd |= (1 << sub_irq); + if ( !( intc->subsrc_pending & ( 1 << sub_irq ) ) ) { + if ( level ) + intc->subsrc_pending |= ( 1 << sub_irq ); + intc->subsrcpnd |= ( 1 << sub_irq ); - s3c2410_intc_gen_int_from_sub_int(intc); - } + s3c2410_intc_gen_int_from_sub_int( intc ); + } } -void -s3c2410_intc_sub_deassert(x49gp_t *x49gp, int sub_irq) +void s3c2410_intc_sub_deassert( x49gp_t* x49gp, int sub_irq ) { - s3c2410_intc_t *intc = x49gp->s3c2410_intc; + s3c2410_intc_t* intc = x49gp->s3c2410_intc; - if (sub_irq > 31) - return; + if ( sub_irq > 31 ) + return; #ifdef DEBUG_S3C2410_INTC - printf("INTC: deassert subirq %u (%08x)\n", sub_irq, 1 << sub_irq); + printf( "INTC: deassert subirq %u (%08x)\n", sub_irq, 1 << sub_irq ); #endif - intc->subsrc_pending &= ~(1 << sub_irq); + intc->subsrc_pending &= ~( 1 << sub_irq ); } -static uint32_t -s3c2410_intc_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_intc_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_intc_t *intc = opaque; - s3c2410_offset_t *reg; - uint32_t data; + s3c2410_intc_t* intc = opaque; + s3c2410_offset_t* reg; + uint32_t data; - if (! S3C2410_OFFSET_OK(intc, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( intc, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(intc, offset); + reg = S3C2410_OFFSET_ENTRY( intc, offset ); - switch (offset) { - case S3C2410_INTC_PRIORITY: - data = priority_get_word(intc); - break; - default: - data = *(reg->datap); - break; - } + switch ( offset ) { + case S3C2410_INTC_PRIORITY: + data = priority_get_word( intc ); + break; + default: + data = *( reg->datap ); + break; + } #ifdef DEBUG_S3C2410_INTC - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-intc", S3C2410_INTC_BASE, - reg->name, (unsigned long) offset, data); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-intc", S3C2410_INTC_BASE, reg->name, ( unsigned long )offset, data ); #endif - return data; + return data; } -static void -s3c2410_intc_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_intc_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { #ifdef DEBUG_S3C2410_INTC - s3c2410_offset_t *reg; + s3c2410_offset_t* reg; #endif - s3c2410_intc_t *intc = opaque; + s3c2410_intc_t* intc = opaque; - if (! S3C2410_OFFSET_OK(intc, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( intc, offset ) ) { + return; + } #ifdef DEBUG_S3C2410_INTC - reg = S3C2410_OFFSET_ENTRY(intc, offset); + reg = S3C2410_OFFSET_ENTRY( intc, offset ); - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-intc", S3C2410_INTC_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-intc", S3C2410_INTC_BASE, reg->name, ( unsigned long )offset, data ); #endif - switch (offset) { - case S3C2410_INTC_SRCPND: - srcpnd_put_word(intc, data); - break; - case S3C2410_INTC_INTMOD: - intmod_put_word(intc, data); - break; - case S3C2410_INTC_INTMSK: - intmsk_put_word(intc, data); - break; - case S3C2410_INTC_PRIORITY: - priority_put_word(intc, data); - break; - case S3C2410_INTC_INTPND: - intpnd_put_word(intc, data); - break; - case S3C2410_INTC_SUBSRCPND: - subsrcpnd_put_word(intc, data); - break; - case S3C2410_INTC_INTSUBMSK: - intsubmsk_put_word(intc, data); - break; - default: - break; - } + switch ( offset ) { + case S3C2410_INTC_SRCPND: + srcpnd_put_word( intc, data ); + break; + case S3C2410_INTC_INTMOD: + intmod_put_word( intc, data ); + break; + case S3C2410_INTC_INTMSK: + intmsk_put_word( intc, data ); + break; + case S3C2410_INTC_PRIORITY: + priority_put_word( intc, data ); + break; + case S3C2410_INTC_INTPND: + intpnd_put_word( intc, data ); + break; + case S3C2410_INTC_SUBSRCPND: + subsrcpnd_put_word( intc, data ); + break; + case S3C2410_INTC_INTSUBMSK: + intsubmsk_put_word( intc, data ); + break; + default: + break; + } } -static int -s3c2410_intc_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_intc_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_intc_t *intc = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_intc_t* intc = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < intc->nr_regs; i++) { - reg = &intc->regs[i]; + for ( i = 0; i < intc->nr_regs; i++ ) { + reg = &intc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - intc->src_pending = intc->srcpnd; - intc->subsrc_pending = intc->subsrcpnd; - intc->srcpnd = 0; - intc->subsrcpnd = 0; + intc->src_pending = intc->srcpnd; + intc->subsrc_pending = intc->subsrcpnd; + intc->srcpnd = 0; + intc->subsrcpnd = 0; - priority_put_word(intc, intc->priority); - intsubmsk_put_word(intc, intc->intsubmsk); - intmsk_put_word(intc, intc->intmsk); + priority_put_word( intc, intc->priority ); + intsubmsk_put_word( intc, intc->intsubmsk ); + intmsk_put_word( intc, intc->intmsk ); - return error; + return error; } -static int -s3c2410_intc_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_intc_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_intc_t *intc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_intc_t* intc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - intc->srcpnd = intc->src_pending; - intc->subsrcpnd = intc->subsrc_pending; + intc->srcpnd = intc->src_pending; + intc->subsrcpnd = intc->subsrc_pending; - for (i = 0; i < intc->nr_regs; i++) { - reg = &intc->regs[i]; + for ( i = 0; i < intc->nr_regs; i++ ) { + reg = &intc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_intc_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_intc_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_intc_t *intc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_intc_t* intc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (reset == X49GP_RESET_POWER_OFF) { - return 0; - } + if ( reset == X49GP_RESET_POWER_OFF ) { + return 0; + } - for (i = 0; i < intc->nr_regs; i++) { - reg = &intc->regs[i]; + for ( i = 0; i < intc->nr_regs; i++ ) { + reg = &intc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_intc_readfn[] = -{ - s3c2410_intc_read, - s3c2410_intc_read, - s3c2410_intc_read -}; +static CPUReadMemoryFunc* s3c2410_intc_readfn[] = { s3c2410_intc_read, s3c2410_intc_read, s3c2410_intc_read }; -static CPUWriteMemoryFunc *s3c2410_intc_writefn[] = -{ - s3c2410_intc_write, - s3c2410_intc_write, - s3c2410_intc_write -}; +static CPUWriteMemoryFunc* s3c2410_intc_writefn[] = { s3c2410_intc_write, s3c2410_intc_write, s3c2410_intc_write }; -static int -s3c2410_intc_init(x49gp_module_t *module) +static int s3c2410_intc_init( x49gp_module_t* module ) { - s3c2410_intc_t *intc; - int iotype; + s3c2410_intc_t* intc; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - intc = malloc(sizeof(s3c2410_intc_t)); - if (NULL == intc) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_intc_data_init(intc)) { - free(intc); - return -ENOMEM; - } + intc = malloc( sizeof( s3c2410_intc_t ) ); + if ( NULL == intc ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_intc_data_init( intc ) ) { + free( intc ); + return -ENOMEM; + } - module->user_data = intc; + module->user_data = intc; - intc->x49gp = module->x49gp; - intc->x49gp->s3c2410_intc = intc; + intc->x49gp = module->x49gp; + intc->x49gp->s3c2410_intc = intc; - iotype = cpu_register_io_memory(s3c2410_intc_readfn, - s3c2410_intc_writefn, intc); + iotype = cpu_register_io_memory( s3c2410_intc_readfn, s3c2410_intc_writefn, intc ); #ifdef DEBUG_S3C2410_INTC - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_INTC_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_INTC_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_intc_exit(x49gp_module_t *module) +static int s3c2410_intc_exit( x49gp_module_t* module ) { - s3c2410_intc_t *intc; + s3c2410_intc_t* intc; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - intc = module->user_data; - if (intc->regs) - free(intc->regs); - free(intc); - } + if ( module->user_data ) { + intc = module->user_data; + if ( intc->regs ) + free( intc->regs ); + free( intc ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_intc_init(x49gp_t *x49gp) +int x49gp_s3c2410_intc_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-intc", - s3c2410_intc_init, - s3c2410_intc_exit, - s3c2410_intc_reset, - s3c2410_intc_load, - s3c2410_intc_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-intc", s3c2410_intc_init, s3c2410_intc_exit, s3c2410_intc_reset, s3c2410_intc_load, + s3c2410_intc_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_intc.h b/src/s3c2410_intc.h index b3c5ffd..48239be 100644 --- a/src/s3c2410_intc.h +++ b/src/s3c2410_intc.h @@ -4,69 +4,69 @@ #ifndef _X49GP_S3C2410_INTC_H #define _X49GP_S3C2410_INTC_H 1 -#define INT_ADC 31 -#define INT_RTC 30 -#define INT_SPI1 29 -#define INT_UART0 28 -#define INT_IIC 27 -#define INT_USBH 26 -#define INT_USBD 25 -#define INT_UART1 23 -#define INT_SPI0 22 -#define INT_SDI 21 -#define INT_DMA3 20 -#define INT_DMA2 19 -#define INT_DMA1 18 -#define INT_DMA0 17 -#define INT_LCD 16 -#define INT_UART2 15 -#define INT_TIMER4 14 -#define INT_TIMER3 13 -#define INT_TIMER2 12 -#define INT_TIMER1 11 -#define INT_TIMER0 10 -#define INT_WDT 9 -#define INT_TICK 8 -#define nBATT_FLT 7 -#define EINT8_23 5 -#define EINT4_7 4 -#define EINT3 3 -#define EINT2 2 -#define EINT1 1 -#define EINT0 0 +#define INT_ADC 31 +#define INT_RTC 30 +#define INT_SPI1 29 +#define INT_UART0 28 +#define INT_IIC 27 +#define INT_USBH 26 +#define INT_USBD 25 +#define INT_UART1 23 +#define INT_SPI0 22 +#define INT_SDI 21 +#define INT_DMA3 20 +#define INT_DMA2 19 +#define INT_DMA1 18 +#define INT_DMA0 17 +#define INT_LCD 16 +#define INT_UART2 15 +#define INT_TIMER4 14 +#define INT_TIMER3 13 +#define INT_TIMER2 12 +#define INT_TIMER1 11 +#define INT_TIMER0 10 +#define INT_WDT 9 +#define INT_TICK 8 +#define nBATT_FLT 7 +#define EINT8_23 5 +#define EINT4_7 4 +#define EINT3 3 +#define EINT2 2 +#define EINT1 1 +#define EINT0 0 -#define SUB_INT_ADC 10 -#define SUB_INT_TC 9 -#define SUB_INT_ERR2 8 -#define SUB_INT_TXD2 7 -#define SUB_INT_RXD2 6 -#define SUB_INT_ERR1 5 -#define SUB_INT_TXD1 4 -#define SUB_INT_RXD1 3 -#define SUB_INT_ERR0 2 -#define SUB_INT_TXD0 1 -#define SUB_INT_RXD0 0 +#define SUB_INT_ADC 10 +#define SUB_INT_TC 9 +#define SUB_INT_ERR2 8 +#define SUB_INT_TXD2 7 +#define SUB_INT_RXD2 6 +#define SUB_INT_ERR1 5 +#define SUB_INT_TXD1 4 +#define SUB_INT_RXD1 3 +#define SUB_INT_ERR0 2 +#define SUB_INT_TXD0 1 +#define SUB_INT_RXD0 0 -#define ARB0_MODE (1 << 0) -#define ARB1_MODE (1 << 1) -#define ARB2_MODE (1 << 2) -#define ARB3_MODE (1 << 3) -#define ARB4_MODE (1 << 4) -#define ARB5_MODE (1 << 5) -#define ARB6_MODE (1 << 6) -#define ARB0_SEL_SHIFT 7 -#define ARB1_SEL_SHIFT 9 -#define ARB2_SEL_SHIFT 11 -#define ARB3_SEL_SHIFT 13 -#define ARB4_SEL_SHIFT 15 -#define ARB5_SEL_SHIFT 17 -#define ARB6_SEL_SHIFT 19 -#define ARBx_SEL_MASK 3 +#define ARB0_MODE ( 1 << 0 ) +#define ARB1_MODE ( 1 << 1 ) +#define ARB2_MODE ( 1 << 2 ) +#define ARB3_MODE ( 1 << 3 ) +#define ARB4_MODE ( 1 << 4 ) +#define ARB5_MODE ( 1 << 5 ) +#define ARB6_MODE ( 1 << 6 ) +#define ARB0_SEL_SHIFT 7 +#define ARB1_SEL_SHIFT 9 +#define ARB2_SEL_SHIFT 11 +#define ARB3_SEL_SHIFT 13 +#define ARB4_SEL_SHIFT 15 +#define ARB5_SEL_SHIFT 17 +#define ARB6_SEL_SHIFT 19 +#define ARBx_SEL_MASK 3 -void s3c2410_intc_sub_assert(x49gp_t *x49gp, int sub_irq, int level); -void s3c2410_intc_sub_deassert(x49gp_t *x49gp, int sub_irq); +void s3c2410_intc_sub_assert( x49gp_t* x49gp, int sub_irq, int level ); +void s3c2410_intc_sub_deassert( x49gp_t* x49gp, int sub_irq ); -void s3c2410_intc_assert(x49gp_t *x49gp, int irq, int level); -void s3c2410_intc_deassert(x49gp_t *x49gp, int irq); +void s3c2410_intc_assert( x49gp_t* x49gp, int irq, int level ); +void s3c2410_intc_deassert( x49gp_t* x49gp, int irq ); #endif /* !(_X49GP_S3C2410_INTC_H) */ diff --git a/src/s3c2410_io_port.c b/src/s3c2410_io_port.c index 5fee298..1904b05 100644 --- a/src/s3c2410_io_port.c +++ b/src/s3c2410_io_port.c @@ -12,689 +12,631 @@ #include "s3c2410_intc.h" #include "byteorder.h" - typedef struct { - uint32_t gpacon; - uint32_t gpadat; + uint32_t gpacon; + uint32_t gpadat; - uint32_t gpbcon; - uint32_t gpbdat; - uint32_t gpbup; + uint32_t gpbcon; + uint32_t gpbdat; + uint32_t gpbup; - uint32_t gpccon; - uint32_t gpcdat; - uint32_t gpcup; + uint32_t gpccon; + uint32_t gpcdat; + uint32_t gpcup; - uint32_t gpdcon; - uint32_t gpddat; - uint32_t gpdup; + uint32_t gpdcon; + uint32_t gpddat; + uint32_t gpdup; - uint32_t gpecon; - uint32_t gpedat; - uint32_t gpeup; + uint32_t gpecon; + uint32_t gpedat; + uint32_t gpeup; - uint32_t gpfcon; - uint32_t gpfdat; - uint32_t gpfup; + uint32_t gpfcon; + uint32_t gpfdat; + uint32_t gpfup; - uint32_t gpgcon; - uint32_t gpgdat; - uint32_t gpgup; + uint32_t gpgcon; + uint32_t gpgdat; + uint32_t gpgup; - uint32_t gphcon; - uint32_t gphdat; - uint32_t gphup; + uint32_t gphcon; + uint32_t gphdat; + uint32_t gphup; - uint32_t misccr; - uint32_t dclkcon; + uint32_t misccr; + uint32_t dclkcon; - uint32_t extint0; - uint32_t extint1; - uint32_t extint2; - uint32_t eintflt0; - uint32_t eintflt1; - uint32_t eintflt2; - uint32_t eintflt3; - uint32_t eintmask; - uint32_t eintpend; + uint32_t extint0; + uint32_t extint1; + uint32_t extint2; + uint32_t eintflt0; + uint32_t eintflt1; + uint32_t eintflt2; + uint32_t eintflt3; + uint32_t eintmask; + uint32_t eintpend; - uint32_t gstatus0; - uint32_t gstatus1; - uint32_t gstatus2; - uint32_t gstatus3; - uint32_t gstatus4; + uint32_t gstatus0; + uint32_t gstatus1; + uint32_t gstatus2; + uint32_t gstatus3; + uint32_t gstatus4; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; } s3c2410_io_port_t; -static int -s3c2410_io_port_data_init(s3c2410_io_port_t *io) +static int s3c2410_io_port_data_init( s3c2410_io_port_t* io ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(IO_PORT, GPACON, 0x007fffff, io->gpacon), - S3C2410_OFFSET(IO_PORT, GPADAT, 0x00000000, io->gpadat), + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( IO_PORT, GPACON, 0x007fffff, io->gpacon ), S3C2410_OFFSET( IO_PORT, GPADAT, 0x00000000, io->gpadat ), - S3C2410_OFFSET(IO_PORT, GPBCON, 0x00000000, io->gpbcon), - S3C2410_OFFSET(IO_PORT, GPBDAT, 0x00000000, io->gpbdat), - S3C2410_OFFSET(IO_PORT, GPBUP, 0x00000000, io->gpbup), + S3C2410_OFFSET( IO_PORT, GPBCON, 0x00000000, io->gpbcon ), S3C2410_OFFSET( IO_PORT, GPBDAT, 0x00000000, io->gpbdat ), + S3C2410_OFFSET( IO_PORT, GPBUP, 0x00000000, io->gpbup ), - S3C2410_OFFSET(IO_PORT, GPCCON, 0x00000000, io->gpccon), - S3C2410_OFFSET(IO_PORT, GPCDAT, 0x00000000, io->gpcdat), - S3C2410_OFFSET(IO_PORT, GPCUP, 0x00000000, io->gpcup), + S3C2410_OFFSET( IO_PORT, GPCCON, 0x00000000, io->gpccon ), S3C2410_OFFSET( IO_PORT, GPCDAT, 0x00000000, io->gpcdat ), + S3C2410_OFFSET( IO_PORT, GPCUP, 0x00000000, io->gpcup ), - S3C2410_OFFSET(IO_PORT, GPDCON, 0x00000000, io->gpdcon), - S3C2410_OFFSET(IO_PORT, GPDDAT, 0x0000038c, io->gpddat), - S3C2410_OFFSET(IO_PORT, GPDUP, 0x0000f000, io->gpdup), + S3C2410_OFFSET( IO_PORT, GPDCON, 0x00000000, io->gpdcon ), S3C2410_OFFSET( IO_PORT, GPDDAT, 0x0000038c, io->gpddat ), + S3C2410_OFFSET( IO_PORT, GPDUP, 0x0000f000, io->gpdup ), - S3C2410_OFFSET(IO_PORT, GPECON, 0x00000000, io->gpecon), - S3C2410_OFFSET(IO_PORT, GPEDAT, 0x0000c7c0, io->gpedat), - S3C2410_OFFSET(IO_PORT, GPEUP, 0x00000000, io->gpeup), + S3C2410_OFFSET( IO_PORT, GPECON, 0x00000000, io->gpecon ), S3C2410_OFFSET( IO_PORT, GPEDAT, 0x0000c7c0, io->gpedat ), + S3C2410_OFFSET( IO_PORT, GPEUP, 0x00000000, io->gpeup ), - S3C2410_OFFSET(IO_PORT, GPFCON, 0x00000000, io->gpfcon), - S3C2410_OFFSET(IO_PORT, GPFDAT, 0x00000008, io->gpfdat), - S3C2410_OFFSET(IO_PORT, GPFUP, 0x00000000, io->gpfup), + S3C2410_OFFSET( IO_PORT, GPFCON, 0x00000000, io->gpfcon ), S3C2410_OFFSET( IO_PORT, GPFDAT, 0x00000008, io->gpfdat ), + S3C2410_OFFSET( IO_PORT, GPFUP, 0x00000000, io->gpfup ), - S3C2410_OFFSET(IO_PORT, GPGCON, 0x00000000, io->gpgcon), - S3C2410_OFFSET(IO_PORT, GPGDAT, 0x0000fffe, io->gpgdat), - S3C2410_OFFSET(IO_PORT, GPGUP, 0x0000f800, io->gpgup), + S3C2410_OFFSET( IO_PORT, GPGCON, 0x00000000, io->gpgcon ), S3C2410_OFFSET( IO_PORT, GPGDAT, 0x0000fffe, io->gpgdat ), + S3C2410_OFFSET( IO_PORT, GPGUP, 0x0000f800, io->gpgup ), - S3C2410_OFFSET(IO_PORT, GPHCON, 0x00000000, io->gphcon), - S3C2410_OFFSET(IO_PORT, GPHDAT, 0x00000000, io->gphdat), - S3C2410_OFFSET(IO_PORT, GPHUP, 0x00000000, io->gphup), + S3C2410_OFFSET( IO_PORT, GPHCON, 0x00000000, io->gphcon ), S3C2410_OFFSET( IO_PORT, GPHDAT, 0x00000000, io->gphdat ), + S3C2410_OFFSET( IO_PORT, GPHUP, 0x00000000, io->gphup ), - S3C2410_OFFSET(IO_PORT, MISCCR, 0x00010330, io->misccr), - S3C2410_OFFSET(IO_PORT, DCLKCON, 0x00000000, io->dclkcon), + S3C2410_OFFSET( IO_PORT, MISCCR, 0x00010330, io->misccr ), S3C2410_OFFSET( IO_PORT, DCLKCON, 0x00000000, io->dclkcon ), - S3C2410_OFFSET(IO_PORT, EXTINT0, 0x00000000, io->extint0), - S3C2410_OFFSET(IO_PORT, EXTINT1, 0x00000000, io->extint1), - S3C2410_OFFSET(IO_PORT, EXTINT2, 0x00000000, io->extint2), - S3C2410_OFFSET(IO_PORT, EINTFLT0, 0x00000000, io->eintflt0), - S3C2410_OFFSET(IO_PORT, EINTFLT1, 0x00000000, io->eintflt1), - S3C2410_OFFSET(IO_PORT, EINTFLT2, 0x00000000, io->eintflt2), - S3C2410_OFFSET(IO_PORT, EINTFLT3, 0x00000000, io->eintflt3), - S3C2410_OFFSET(IO_PORT, EINTMASK, 0x00fffff0, io->eintmask), - S3C2410_OFFSET(IO_PORT, EINTPEND, 0x00000000, io->eintpend), + S3C2410_OFFSET( IO_PORT, EXTINT0, 0x00000000, io->extint0 ), S3C2410_OFFSET( IO_PORT, EXTINT1, 0x00000000, io->extint1 ), + S3C2410_OFFSET( IO_PORT, EXTINT2, 0x00000000, io->extint2 ), S3C2410_OFFSET( IO_PORT, EINTFLT0, 0x00000000, io->eintflt0 ), + S3C2410_OFFSET( IO_PORT, EINTFLT1, 0x00000000, io->eintflt1 ), S3C2410_OFFSET( IO_PORT, EINTFLT2, 0x00000000, io->eintflt2 ), + S3C2410_OFFSET( IO_PORT, EINTFLT3, 0x00000000, io->eintflt3 ), S3C2410_OFFSET( IO_PORT, EINTMASK, 0x00fffff0, io->eintmask ), + S3C2410_OFFSET( IO_PORT, EINTPEND, 0x00000000, io->eintpend ), - S3C2410_OFFSET(IO_PORT, GSTATUS0, 0x00000001, io->gstatus0), - S3C2410_OFFSET(IO_PORT, GSTATUS1, 0x32410002, io->gstatus1), - S3C2410_OFFSET(IO_PORT, GSTATUS2, 0x00000001, io->gstatus2), - S3C2410_OFFSET(IO_PORT, GSTATUS3, 0x00000000, io->gstatus3), - S3C2410_OFFSET(IO_PORT, GSTATUS4, 0x00000000, io->gstatus4) - }; + S3C2410_OFFSET( IO_PORT, GSTATUS0, 0x00000001, io->gstatus0 ), S3C2410_OFFSET( IO_PORT, GSTATUS1, 0x32410002, io->gstatus1 ), + S3C2410_OFFSET( IO_PORT, GSTATUS2, 0x00000001, io->gstatus2 ), S3C2410_OFFSET( IO_PORT, GSTATUS3, 0x00000000, io->gstatus3 ), + S3C2410_OFFSET( IO_PORT, GSTATUS4, 0x00000000, io->gstatus4 ) }; - memset(io, 0, sizeof(s3c2410_io_port_t)); + memset( io, 0, sizeof( s3c2410_io_port_t ) ); - io->regs = malloc(sizeof(regs)); - if (NULL == io->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + io->regs = malloc( sizeof( regs ) ); + if ( NULL == io->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(io->regs, regs, sizeof(regs)); - io->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( io->regs, regs, sizeof( regs ) ); + io->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static uint32_t -s3c2410_scan_keys(x49gp_t *x49gp, uint32_t gpgcon, uint32_t gpgdat) +static uint32_t s3c2410_scan_keys( x49gp_t* x49gp, uint32_t gpgcon, uint32_t gpgdat ) { - uint32_t result; - int col, row; - - result = 0xfffe | (gpgdat & 1); + uint32_t result; + int col, row; - for (col = 0; col < 8; col++) { - switch ((gpgcon >> (2 * (col + 8))) & 3) { - case 0: /* Input */ - case 2: /* Interrupt */ - case 3: /* Reserved */ - break; - case 1: /* Output */ - result &= ~(1 << (col + 8)); - result |= gpgdat & (1 << (col + 8)); + result = 0xfffe | ( gpgdat & 1 ); - if (0 == (gpgdat & (1 << (col + 8)))) { - result &= ~(x49gp->keybycol[col]); - } - break; - } - } + for ( col = 0; col < 8; col++ ) { + switch ( ( gpgcon >> ( 2 * ( col + 8 ) ) ) & 3 ) { + case 0: /* Input */ + case 2: /* Interrupt */ + case 3: /* Reserved */ + break; + case 1: /* Output */ + result &= ~( 1 << ( col + 8 ) ); + result |= gpgdat & ( 1 << ( col + 8 ) ); - for (row = 1; row < 8; row++) { - switch ((gpgcon >> (2 * row)) & 3) { - case 0: /* Input */ - case 2: /* Interrupt */ - case 3: /* Reserved */ - break; - case 1: /* Output */ - result &= ~(1 << row); - result |= gpgdat & (1 << row); + if ( 0 == ( gpgdat & ( 1 << ( col + 8 ) ) ) ) { + result &= ~( x49gp->keybycol[ col ] ); + } + break; + } + } - if (0 == (gpgdat & (1 << row))) { - result &= ~(x49gp->keybyrow[row] << 8); - } - break; - } - } + for ( row = 1; row < 8; row++ ) { + switch ( ( gpgcon >> ( 2 * row ) ) & 3 ) { + case 0: /* Input */ + case 2: /* Interrupt */ + case 3: /* Reserved */ + break; + case 1: /* Output */ + result &= ~( 1 << row ); + result |= gpgdat & ( 1 << row ); - return result; + if ( 0 == ( gpgdat & ( 1 << row ) ) ) { + result &= ~( x49gp->keybyrow[ row ] << 8 ); + } + break; + } + } + + return result; } -static uint32_t -s3c2410_io_port_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_io_port_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_io_port_t *io = opaque; - s3c2410_offset_t *reg; + s3c2410_io_port_t* io = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(io, offset)) { - fprintf(stderr, "%s:%u: offset %08lx not OK\n", __FUNCTION__, __LINE__, (unsigned long) offset); - abort(); - return ~(0); - } + if ( !S3C2410_OFFSET_OK( io, offset ) ) { + fprintf( stderr, "%s:%u: offset %08lx not OK\n", __FUNCTION__, __LINE__, ( unsigned long )offset ); + abort(); + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(io, offset); + reg = S3C2410_OFFSET_ENTRY( io, offset ); - switch (offset) { - case S3C2410_IO_PORT_MISCCR: -// if (io->x49gp->arm->NresetSig != LOW) { - *(reg->datap) |= 0x00010000; -// } - break; + switch ( offset ) { + case S3C2410_IO_PORT_MISCCR: + // if (io->x49gp->arm->NresetSig != LOW) { + *( reg->datap ) |= 0x00010000; + // } + break; - case S3C2410_IO_PORT_GPCDAT: - if (0 == ((io->gpccon >> 30) & 3)) { - *(reg->datap) |= 0x8000; - } - if (0 == ((io->gpccon >> 28) & 3)) { - *(reg->datap) |= 0x4000; - } - if (0 == ((io->gpccon >> 26) & 3)) { - *(reg->datap) |= 0x2000; - } - if (0 == ((io->gpccon >> 24) & 3)) { - *(reg->datap) |= 0x1000; - } - break; + case S3C2410_IO_PORT_GPCDAT: + if ( 0 == ( ( io->gpccon >> 30 ) & 3 ) ) { + *( reg->datap ) |= 0x8000; + } + if ( 0 == ( ( io->gpccon >> 28 ) & 3 ) ) { + *( reg->datap ) |= 0x4000; + } + if ( 0 == ( ( io->gpccon >> 26 ) & 3 ) ) { + *( reg->datap ) |= 0x2000; + } + if ( 0 == ( ( io->gpccon >> 24 ) & 3 ) ) { + *( reg->datap ) |= 0x1000; + } + break; - case S3C2410_IO_PORT_GPDDAT: - if (0 == ((io->gpdcon >> 6) & 3)) { - *(reg->datap) |= 0x0008; - } - break; + case S3C2410_IO_PORT_GPDDAT: + if ( 0 == ( ( io->gpdcon >> 6 ) & 3 ) ) { + *( reg->datap ) |= 0x0008; + } + break; - case S3C2410_IO_PORT_GPEDAT: - if (0 == ((io->gpecon >> 30) & 3)) { - *(reg->datap) |= 0x8000; - } - if (0 == ((io->gpecon >> 28) & 3)) { - *(reg->datap) |= 0x4000; - } - break; + case S3C2410_IO_PORT_GPEDAT: + if ( 0 == ( ( io->gpecon >> 30 ) & 3 ) ) { + *( reg->datap ) |= 0x8000; + } + if ( 0 == ( ( io->gpecon >> 28 ) & 3 ) ) { + *( reg->datap ) |= 0x4000; + } + break; - case S3C2410_IO_PORT_GPFDAT: + case S3C2410_IO_PORT_GPFDAT: #if 1 - if (1 != ((io->gpfcon >> 6) & 3)) { - *(reg->datap) |= 0x0008; - } + if ( 1 != ( ( io->gpfcon >> 6 ) & 3 ) ) { + *( reg->datap ) |= 0x0008; + } #endif - break; + break; - case S3C2410_IO_PORT_GPGDAT: - return s3c2410_scan_keys(io->x49gp, io->gpgcon, io->gpgdat); + case S3C2410_IO_PORT_GPGDAT: + return s3c2410_scan_keys( io->x49gp, io->gpgcon, io->gpgdat ); - case S3C2410_IO_PORT_GPHDAT: - if (0 == ((io->gphcon >> 14) & 3)) { - *(reg->datap) |= 0x80; - } - if (0 == ((io->gphcon >> 12) & 3)) { - *(reg->datap) &= ~(0x40); - } - break; - } + case S3C2410_IO_PORT_GPHDAT: + if ( 0 == ( ( io->gphcon >> 14 ) & 3 ) ) { + *( reg->datap ) |= 0x80; + } + if ( 0 == ( ( io->gphcon >> 12 ) & 3 ) ) { + *( reg->datap ) &= ~( 0x40 ); + } + break; + } #ifdef DEBUG_S3C2410_IO_PORT - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-io-port", S3C2410_IO_PORT_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-io-port", S3C2410_IO_PORT_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_io_port_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_io_port_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_io_port_t *io = opaque; - s3c2410_offset_t *reg; - uint32_t change; -static uint32_t lcd_data = 0; + s3c2410_io_port_t* io = opaque; + s3c2410_offset_t* reg; + uint32_t change; + static uint32_t lcd_data = 0; - if (! S3C2410_OFFSET_OK(io, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( io, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(io, offset); + reg = S3C2410_OFFSET_ENTRY( io, offset ); #ifdef DEBUG_S3C2410_IO_PORT - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-io-port", S3C2410_IO_PORT_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-io-port", S3C2410_IO_PORT_BASE, reg->name, ( unsigned long )offset, data ); #endif - switch (offset) { - case S3C2410_IO_PORT_GPDDAT: - change = *(reg->datap) ^ data; - *(reg->datap) = data; + switch ( offset ) { + case S3C2410_IO_PORT_GPDDAT: + change = *( reg->datap ) ^ data; + *( reg->datap ) = data; - if (!(data & 0x200) && (change & 0x200)) { - lcd_data = 0; - } + if ( !( data & 0x200 ) && ( change & 0x200 ) ) { + lcd_data = 0; + } - if (!(data & 0x200) && (data & 0x2000) && (change & 0x2000)) { + if ( !( data & 0x200 ) && ( data & 0x2000 ) && ( change & 0x2000 ) ) { #ifdef DEBUG_S3C2410_IO_PORT - printf("IO_PORT GPDDAT: clk0 rise: data %u\n", - (data >> 12) & 1); + printf( "IO_PORT GPDDAT: clk0 rise: data %u\n", ( data >> 12 ) & 1 ); #endif - lcd_data <<= 1; - lcd_data |= (data >> 12) & 1; - } + lcd_data <<= 1; + lcd_data |= ( data >> 12 ) & 1; + } - if ((data & 0x200) && (change & 0x200)) { + if ( ( data & 0x200 ) && ( change & 0x200 ) ) { #ifdef DEBUG_S3C2410_IO_PORT - printf("IO_PORT GPDDAT: cs0 rise: data %04x\n", - lcd_data); + printf( "IO_PORT GPDDAT: cs0 rise: data %04x\n", lcd_data ); #endif - } + } - break; + break; - case S3C2410_IO_PORT_MISCCR: - *(reg->datap) = data; - if (!(*(reg->datap) & 0x00010000)) { - *(reg->datap) = 0x10330; -// if (io->x49gp->arm->NresetSig != LOW) { -// io->x49gp->arm->NresetSig = LOW; -// io->x49gp->arm->Exception++; -// } - } - break; + case S3C2410_IO_PORT_MISCCR: + *( reg->datap ) = data; + if ( !( *( reg->datap ) & 0x00010000 ) ) { + *( reg->datap ) = 0x10330; + // if (io->x49gp->arm->NresetSig != LOW) { + // io->x49gp->arm->NresetSig = LOW; + // io->x49gp->arm->Exception++; + // } + } + break; - case S3C2410_IO_PORT_GSTATUS0: - case S3C2410_IO_PORT_GSTATUS1: - /* read only */ - break; + case S3C2410_IO_PORT_GSTATUS0: + case S3C2410_IO_PORT_GSTATUS1: + /* read only */ + break; - case S3C2410_IO_PORT_GSTATUS2: - *(reg->datap) &= ~(data & 7); - break; + case S3C2410_IO_PORT_GSTATUS2: + *( reg->datap ) &= ~( data & 7 ); + break; - case S3C2410_IO_PORT_EINTPEND: - *(reg->datap) &= ~(data); + case S3C2410_IO_PORT_EINTPEND: + *( reg->datap ) &= ~( data ); - if (0 == (*(reg->datap) & 0x000000f0)) - s3c2410_intc_deassert(io->x49gp, EINT4_7); - if (0 == (*(reg->datap) & 0x00ffff00)) - s3c2410_intc_deassert(io->x49gp, EINT8_23); - break; + if ( 0 == ( *( reg->datap ) & 0x000000f0 ) ) + s3c2410_intc_deassert( io->x49gp, EINT4_7 ); + if ( 0 == ( *( reg->datap ) & 0x00ffff00 ) ) + s3c2410_intc_deassert( io->x49gp, EINT8_23 ); + break; - default: - *(reg->datap) = data; - break; - } + default: + *( reg->datap ) = data; + break; + } } -void -s3c2410_io_port_g_update(x49gp_t *x49gp, int column, int row, unsigned char columnbit, unsigned char rowbit, uint32_t new_state) +void s3c2410_io_port_g_update( x49gp_t* x49gp, int column, int row, unsigned char columnbit, unsigned char rowbit, uint32_t new_state ) { - s3c2410_io_port_t *io = x49gp->s3c2410_io_port; + s3c2410_io_port_t* io = x49gp->s3c2410_io_port; uint32_t oldvalue, newvalue, change; int n; - oldvalue = s3c2410_scan_keys(x49gp, io->gpgcon, io->gpgdat); + oldvalue = s3c2410_scan_keys( x49gp, io->gpgcon, io->gpgdat ); - if (new_state) { - x49gp->keybycol[column] |= rowbit; - x49gp->keybyrow[row] |= columnbit; + if ( new_state ) { + x49gp->keybycol[ column ] |= rowbit; + x49gp->keybyrow[ row ] |= columnbit; - } else { - x49gp->keybycol[column] &= ~rowbit; - x49gp->keybyrow[row] &= ~columnbit; - } + } else { + x49gp->keybycol[ column ] &= ~rowbit; + x49gp->keybyrow[ row ] &= ~columnbit; + } - newvalue = s3c2410_scan_keys(x49gp, io->gpgcon, io->gpgdat); - change=newvalue^oldvalue; + newvalue = s3c2410_scan_keys( x49gp, io->gpgcon, io->gpgdat ); + change = newvalue ^ oldvalue; + for ( n = 0; n < 15; ++n ) { - for(n=0;n<15;++n) { + switch ( ( io->gpgcon >> ( 2 * n ) ) & 3 ) { - switch ((io->gpgcon >> (2 * n)) & 3) { - - case 2: /* Interrupt */ - { - switch (n+8<=15 ? - (io->extint1 >> (4 * n)) & 7 : // EINT 8-15 - (io->extint2 >> (4 * (n-8))) & 7 // EINT 16-23 - ) { - case 0: /* Low Level */ - if (!(newvalue & (1 << n))) - { - io->eintpend |= 1 << (n + 8); - if (io->eintpend & ~(io->eintmask)) - s3c2410_intc_assert(x49gp, EINT8_23, 1); - } - break; - case 1: /* High Level */ - if (newvalue & (1 << n)) { - io->eintpend |= 1 << (n + 8); - if (io->eintpend & ~(io->eintmask)) - s3c2410_intc_assert(x49gp, EINT8_23, 1); + case 2: /* Interrupt */ + { + switch ( n + 8 <= 15 ? ( io->extint1 >> ( 4 * n ) ) & 7 : // EINT 8-15 + ( io->extint2 >> ( 4 * ( n - 8 ) ) ) & 7 // EINT 16-23 + ) { + case 0: /* Low Level */ + if ( !( newvalue & ( 1 << n ) ) ) { + io->eintpend |= 1 << ( n + 8 ); + if ( io->eintpend & ~( io->eintmask ) ) + s3c2410_intc_assert( x49gp, EINT8_23, 1 ); + } + break; + case 1: /* High Level */ + if ( newvalue & ( 1 << n ) ) { + io->eintpend |= 1 << ( n + 8 ); + if ( io->eintpend & ~( io->eintmask ) ) + s3c2410_intc_assert( x49gp, EINT8_23, 1 ); + } + break; + case 2: /* Falling Edge */ + case 3: + if ( ( change & ( 1 << n ) ) && !( newvalue & ( 1 << n ) ) ) { + io->eintpend |= 1 << ( n + 8 ); + if ( io->eintpend & ~( io->eintmask ) ) + s3c2410_intc_assert( x49gp, EINT8_23, 1 ); + } + break; + case 4: /* Rising Edge */ + case 5: + if ( ( change & ( 1 << n ) ) && ( newvalue & ( 1 << n ) ) ) { + io->eintpend |= 1 << ( n + 8 ); + if ( io->eintpend & ~( io->eintmask ) ) + s3c2410_intc_assert( x49gp, EINT8_23, 1 ); + } + break; + case 6: /* Any Edge */ + case 7: + if ( change & ( 1 << n ) ) { + io->eintpend |= 1 << ( n + 8 ); + if ( io->eintpend & ~( io->eintmask ) ) + s3c2410_intc_assert( x49gp, EINT8_23, 1 ); + } + break; + } } - break; - case 2: /* Falling Edge */ - case 3: - if ((change & (1 << n)) && !(newvalue & (1 << n))) { - io->eintpend |= 1 << (n + 8); - if (io->eintpend & ~(io->eintmask)) - s3c2410_intc_assert(x49gp, EINT8_23, 1); - } - break; - case 4: /* Rising Edge */ - case 5: - if ((change & (1 << n)) && (newvalue & (1 << n))) { - io->eintpend |= 1 << (n + 8); - if (io->eintpend & ~(io->eintmask)) - s3c2410_intc_assert(x49gp, EINT8_23, 1); - } - break; - case 6: /* Any Edge */ - case 7: - if (change & (1 << n)) { - io->eintpend |= 1 << (n + 8); - if (io->eintpend & ~(io->eintmask)) - s3c2410_intc_assert(x49gp, EINT8_23, 1); - } - break; - + break; + case 0: /* Input */ + case 1: /* Output */ + case 3: /* Reserved */ + break; } } - break; - case 0: /* Input */ - case 1: /* Output */ - case 3: /* Reserved */ - break; - } + return; +} + +void s3c2410_io_port_f_set_bit( x49gp_t* x49gp, int n, uint32_t value ) +{ + s3c2410_io_port_t* io = x49gp->s3c2410_io_port; + uint32_t change; + int pending, level; + + if ( n > 7 ) + return; + + // g_mutex_lock(x49gp->memlock); + + change = 0; + switch ( ( io->gpfcon >> ( 2 * n ) ) & 3 ) { + case 0: /* Input */ + io->gpfdat &= ~( 1 << n ); + io->gpfdat |= ( value << n ); + goto out; + + case 2: /* Interrupt */ + change = io->gpfdat ^ ( value << n ); + io->gpfdat &= ~( 1 << n ); + io->gpfdat |= ( value << n ); + break; + + case 1: /* Output */ + case 3: /* Reserved */ + goto out; } - return; -} + pending = -1; + level = 0; + switch ( ( io->extint0 >> ( 4 * n ) ) & 7 ) { + case 0: /* Low Level */ + if ( !( io->gpfdat & ( 1 << n ) ) ) + pending = n; + level = 1; + break; -void -s3c2410_io_port_f_set_bit(x49gp_t *x49gp, int n, uint32_t value) -{ - s3c2410_io_port_t *io = x49gp->s3c2410_io_port; - uint32_t change; - int pending, level; + case 1: /* High Level */ + if ( io->gpfdat & ( 1 << n ) ) + pending = n; + level = 1; + break; - if (n > 7) - return; + case 2: /* Falling Edge */ + case 3: + if ( ( change & ( 1 << n ) ) && !( io->gpfdat & ( 1 << n ) ) ) + pending = n; + break; -// g_mutex_lock(x49gp->memlock); + case 4: /* Rising Edge */ + case 5: + if ( ( change & ( 1 << n ) ) && ( io->gpfdat & ( 1 << n ) ) ) + pending = n; + break; - change = 0; - switch ((io->gpfcon >> (2 * n)) & 3) { - case 0: /* Input */ - io->gpfdat &= ~(1 << n); - io->gpfdat |= (value << n); - goto out; + case 6: /* Any Edge */ + case 7: + if ( change & ( 1 << n ) ) + pending = n; + break; + } - case 2: /* Interrupt */ - change = io->gpfdat ^ (value << n); - io->gpfdat &= ~(1 << n); - io->gpfdat |= (value << n); - break; + if ( -1 == pending ) + goto out; - case 1: /* Output */ - case 3: /* Reserved */ - goto out; - } - - pending = -1; - level = 0; - switch ((io->extint0 >> (4 * n)) & 7) { - case 0: /* Low Level */ - if (!(io->gpfdat & (1 << n))) - pending = n; - level = 1; - break; - - case 1: /* High Level */ - if (io->gpfdat & (1 << n)) - pending = n; - level = 1; - break; - - case 2: /* Falling Edge */ - case 3: - if ((change & (1 << n)) && !(io->gpfdat & (1 << n))) - pending = n; - break; - - case 4: /* Rising Edge */ - case 5: - if ((change & (1 << n)) && (io->gpfdat & (1 << n))) - pending = n; - break; - - case 6: /* Any Edge */ - case 7: - if (change & (1 << n)) - pending = n; - break; - } - - if (-1 == pending) - goto out; - - switch (n) { - case 0: - s3c2410_intc_assert(x49gp, EINT0, level); - break; - case 1: - s3c2410_intc_assert(x49gp, EINT1, level); - break; - case 2: - s3c2410_intc_assert(x49gp, EINT2, level); - break; - case 3: - s3c2410_intc_assert(x49gp, EINT3, level); - break; - case 4: - case 5: - case 6: - case 7: - io->eintpend |= (1 << n); - if (io->eintpend & ~(io->eintmask)) - s3c2410_intc_assert(x49gp, EINT4_7, 1); - break; - } + switch ( n ) { + case 0: + s3c2410_intc_assert( x49gp, EINT0, level ); + break; + case 1: + s3c2410_intc_assert( x49gp, EINT1, level ); + break; + case 2: + s3c2410_intc_assert( x49gp, EINT2, level ); + break; + case 3: + s3c2410_intc_assert( x49gp, EINT3, level ); + break; + case 4: + case 5: + case 6: + case 7: + io->eintpend |= ( 1 << n ); + if ( io->eintpend & ~( io->eintmask ) ) + s3c2410_intc_assert( x49gp, EINT4_7, 1 ); + break; + } out: -// g_mutex_unlock(x49gp->memlock); + // g_mutex_unlock(x49gp->memlock); - return; + return; } -static int -s3c2410_io_port_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_io_port_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_io_port_t *io = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_io_port_t* io = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < io->nr_regs; i++) { - reg = &io->regs[i]; + for ( i = 0; i < io->nr_regs; i++ ) { + reg = &io->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_io_port_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_io_port_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_io_port_t *io = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_io_port_t* io = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < io->nr_regs; i++) { - reg = &io->regs[i]; + for ( i = 0; i < io->nr_regs; i++ ) { + reg = &io->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_io_port_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_io_port_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_io_port_t *io = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_io_port_t* io = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (reset == X49GP_RESET_POWER_OFF) { - io->gstatus2 = 2; - return 0; - } + if ( reset == X49GP_RESET_POWER_OFF ) { + io->gstatus2 = 2; + return 0; + } - for (i = 0; i < io->nr_regs; i++) { - reg = &io->regs[i]; + for ( i = 0; i < io->nr_regs; i++ ) { + reg = &io->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - if (reset == X49GP_RESET_WATCHDOG) { - io->gstatus2 = 4; - } + if ( reset == X49GP_RESET_WATCHDOG ) { + io->gstatus2 = 4; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_io_port_readfn[] = -{ - s3c2410_io_port_read, - s3c2410_io_port_read, - s3c2410_io_port_read -}; +static CPUReadMemoryFunc* s3c2410_io_port_readfn[] = { s3c2410_io_port_read, s3c2410_io_port_read, s3c2410_io_port_read }; -static CPUWriteMemoryFunc *s3c2410_io_port_writefn[] = -{ - s3c2410_io_port_write, - s3c2410_io_port_write, - s3c2410_io_port_write -}; +static CPUWriteMemoryFunc* s3c2410_io_port_writefn[] = { s3c2410_io_port_write, s3c2410_io_port_write, s3c2410_io_port_write }; -static int -s3c2410_io_port_init(x49gp_module_t *module) +static int s3c2410_io_port_init( x49gp_module_t* module ) { - s3c2410_io_port_t *io; - int iotype; + s3c2410_io_port_t* io; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - io = malloc(sizeof(s3c2410_io_port_t)); - if (NULL == io) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_io_port_data_init(io)) { - free(io); - return -ENOMEM; - } + io = malloc( sizeof( s3c2410_io_port_t ) ); + if ( NULL == io ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_io_port_data_init( io ) ) { + free( io ); + return -ENOMEM; + } - module->user_data = io; - module->x49gp->s3c2410_io_port = io; - io->x49gp = module->x49gp; + module->user_data = io; + module->x49gp->s3c2410_io_port = io; + io->x49gp = module->x49gp; - iotype = cpu_register_io_memory(s3c2410_io_port_readfn, - s3c2410_io_port_writefn, io); + iotype = cpu_register_io_memory( s3c2410_io_port_readfn, s3c2410_io_port_writefn, io ); #ifdef DEBUG_S3C2410_IO_PORT - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_IO_PORT_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_IO_PORT_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_io_port_exit(x49gp_module_t *module) +static int s3c2410_io_port_exit( x49gp_module_t* module ) { - s3c2410_io_port_t *io; + s3c2410_io_port_t* io; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - io = module->user_data; - if (io->regs) - free(io->regs); - free(io); - } + if ( module->user_data ) { + io = module->user_data; + if ( io->regs ) + free( io->regs ); + free( io ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_io_port_init(x49gp_t *x49gp) +int x49gp_s3c2410_io_port_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-io-port", - s3c2410_io_port_init, - s3c2410_io_port_exit, - s3c2410_io_port_reset, - s3c2410_io_port_load, - s3c2410_io_port_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-io-port", s3c2410_io_port_init, s3c2410_io_port_exit, s3c2410_io_port_reset, + s3c2410_io_port_load, s3c2410_io_port_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_lcd.c b/src/s3c2410_lcd.c index 3ac63c6..f84d755 100644 --- a/src/s3c2410_lcd.c +++ b/src/s3c2410_lcd.c @@ -15,402 +15,348 @@ #include "x49gp_ui.h" #include "s3c2410.h" - typedef struct { - uint32_t lcdcon1; - uint32_t lcdcon2; - uint32_t lcdcon3; - uint32_t lcdcon4; - uint32_t lcdcon5; - uint32_t lcdsaddr1; - uint32_t lcdsaddr2; - uint32_t lcdsaddr3; - uint32_t redlut; - uint32_t greenlut; - uint32_t bluelut; - uint32_t dithmode; - uint32_t tpal; - uint32_t lcdintpnd; - uint32_t lcdsrcpnd; - uint32_t lcdintmsk; - uint32_t lpcsel; - uint32_t __unknown_68; + uint32_t lcdcon1; + uint32_t lcdcon2; + uint32_t lcdcon3; + uint32_t lcdcon4; + uint32_t lcdcon5; + uint32_t lcdsaddr1; + uint32_t lcdsaddr2; + uint32_t lcdsaddr3; + uint32_t redlut; + uint32_t greenlut; + uint32_t bluelut; + uint32_t dithmode; + uint32_t tpal; + uint32_t lcdintpnd; + uint32_t lcdsrcpnd; + uint32_t lcdintmsk; + uint32_t lpcsel; + uint32_t __unknown_68; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; } s3c2410_lcd_t; -static int -s3c2410_lcd_data_init(s3c2410_lcd_t *lcd) +static int s3c2410_lcd_data_init( s3c2410_lcd_t* lcd ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(LCD, LCDCON1, 0x00000000, lcd->lcdcon1), - S3C2410_OFFSET(LCD, LCDCON2, 0x00000000, lcd->lcdcon2), - S3C2410_OFFSET(LCD, LCDCON3, 0x00000000, lcd->lcdcon3), - S3C2410_OFFSET(LCD, LCDCON4, 0x00000000, lcd->lcdcon4), - S3C2410_OFFSET(LCD, LCDCON5, 0x00000000, lcd->lcdcon5), - S3C2410_OFFSET(LCD, LCDSADDR1, 0x00000000, lcd->lcdsaddr1), - S3C2410_OFFSET(LCD, LCDSADDR2, 0x00000000, lcd->lcdsaddr2), - S3C2410_OFFSET(LCD, LCDSADDR3, 0x00000000, lcd->lcdsaddr3), - S3C2410_OFFSET(LCD, REDLUT, 0x00000000, lcd->redlut), - S3C2410_OFFSET(LCD, GREENLUT, 0x00000000, lcd->greenlut), - S3C2410_OFFSET(LCD, BLUELUT, 0x00000000, lcd->bluelut), - S3C2410_OFFSET(LCD, DITHMODE, 0x00000000, lcd->dithmode), - S3C2410_OFFSET(LCD, TPAL, 0x00000000, lcd->tpal), - S3C2410_OFFSET(LCD, LCDINTPND, 0x00000000, lcd->lcdintpnd), - S3C2410_OFFSET(LCD, LCDSRCPND, 0x00000000, lcd->lcdsrcpnd), - S3C2410_OFFSET(LCD, LCDINTMSK, 0x00000003, lcd->lcdintmsk), - S3C2410_OFFSET(LCD, LPCSEL, 0x00000004, lcd->lpcsel), - S3C2410_OFFSET(LCD, UNKNOWN_68, 0x00000000, lcd->__unknown_68) - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( LCD, LCDCON1, 0x00000000, lcd->lcdcon1 ), S3C2410_OFFSET( LCD, LCDCON2, 0x00000000, lcd->lcdcon2 ), + S3C2410_OFFSET( LCD, LCDCON3, 0x00000000, lcd->lcdcon3 ), S3C2410_OFFSET( LCD, LCDCON4, 0x00000000, lcd->lcdcon4 ), + S3C2410_OFFSET( LCD, LCDCON5, 0x00000000, lcd->lcdcon5 ), S3C2410_OFFSET( LCD, LCDSADDR1, 0x00000000, lcd->lcdsaddr1 ), + S3C2410_OFFSET( LCD, LCDSADDR2, 0x00000000, lcd->lcdsaddr2 ), S3C2410_OFFSET( LCD, LCDSADDR3, 0x00000000, lcd->lcdsaddr3 ), + S3C2410_OFFSET( LCD, REDLUT, 0x00000000, lcd->redlut ), S3C2410_OFFSET( LCD, GREENLUT, 0x00000000, lcd->greenlut ), + S3C2410_OFFSET( LCD, BLUELUT, 0x00000000, lcd->bluelut ), S3C2410_OFFSET( LCD, DITHMODE, 0x00000000, lcd->dithmode ), + S3C2410_OFFSET( LCD, TPAL, 0x00000000, lcd->tpal ), S3C2410_OFFSET( LCD, LCDINTPND, 0x00000000, lcd->lcdintpnd ), + S3C2410_OFFSET( LCD, LCDSRCPND, 0x00000000, lcd->lcdsrcpnd ), S3C2410_OFFSET( LCD, LCDINTMSK, 0x00000003, lcd->lcdintmsk ), + S3C2410_OFFSET( LCD, LPCSEL, 0x00000004, lcd->lpcsel ), S3C2410_OFFSET( LCD, UNKNOWN_68, 0x00000000, lcd->__unknown_68 ) }; - memset(lcd, 0, sizeof(s3c2410_lcd_t)); + memset( lcd, 0, sizeof( s3c2410_lcd_t ) ); - lcd->regs = malloc(sizeof(regs)); - if (NULL == lcd->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + lcd->regs = malloc( sizeof( regs ) ); + if ( NULL == lcd->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(lcd->regs, regs, sizeof(regs)); - lcd->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( lcd->regs, regs, sizeof( regs ) ); + lcd->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -void -x49gp_schedule_lcd_update(x49gp_t *x49gp) +void x49gp_schedule_lcd_update( x49gp_t* x49gp ) { - if (! x49gp_timer_pending(x49gp->lcd_timer)) { - x49gp_mod_timer(x49gp->lcd_timer, - x49gp_get_clock() + X49GP_LCD_REFRESH_INTERVAL); - } + if ( !x49gp_timer_pending( x49gp->lcd_timer ) ) { + x49gp_mod_timer( x49gp->lcd_timer, x49gp_get_clock() + X49GP_LCD_REFRESH_INTERVAL ); + } } -static int -x49gp_get_pixel_color(s3c2410_lcd_t *lcd, int x, int y) +static int x49gp_get_pixel_color( s3c2410_lcd_t* lcd, int x, int y ) { - uint32_t bank, addr, data, offset, pixel_offset; - int bits_per_pixel = lcd->lcdcon5 > 2 ? 1 : 4 >> lcd->lcdcon5; + uint32_t bank, addr, data, offset, pixel_offset; + int bits_per_pixel = lcd->lcdcon5 > 2 ? 1 : 4 >> lcd->lcdcon5; - bank = (lcd->lcdsaddr1 << 1) & 0x7fc00000; - addr = bank | ((lcd->lcdsaddr1 << 1) & 0x003ffffe); + bank = ( lcd->lcdsaddr1 << 1 ) & 0x7fc00000; + addr = bank | ( ( lcd->lcdsaddr1 << 1 ) & 0x003ffffe ); - pixel_offset = (160 * y + x) * bits_per_pixel; - offset = (pixel_offset >> 3) & 0xfffffffc; + pixel_offset = ( 160 * y + x ) * bits_per_pixel; + offset = ( pixel_offset >> 3 ) & 0xfffffffc; - data = ldl_phys(addr + offset); - data >>= pixel_offset & 31; - data &= (1 << bits_per_pixel) - 1; + data = ldl_phys( addr + offset ); + data >>= pixel_offset & 31; + data &= ( 1 << bits_per_pixel ) - 1; - switch (bits_per_pixel) { - case 1: - return 15 * data; - case 2: - return 15 & (lcd->bluelut >> (4 * data)); - default: - return data; - } + switch ( bits_per_pixel ) { + case 1: + return 15 * data; + case 2: + return 15 & ( lcd->bluelut >> ( 4 * data ) ); + default: + return data; + } } -void -x49gp_lcd_update(x49gp_t *x49gp) +void x49gp_lcd_update( x49gp_t* x49gp ) { - x49gp_ui_t *ui = x49gp->ui; - s3c2410_lcd_t *lcd = x49gp->s3c2410_lcd; - GdkRectangle rect; - GdkGC *gc; - int color, x, y; + x49gp_ui_t* ui = x49gp->ui; + s3c2410_lcd_t* lcd = x49gp->s3c2410_lcd; + GdkRectangle rect; + GdkGC* gc; + int color, x, y; - if (!(lcd->lcdcon1 & 1)) { - gdk_draw_drawable(ui->lcd_pixmap, ui->window->style->bg_gc[0], - ui->bg_pixmap, - ui->lcd_x_offset, ui->lcd_y_offset, - 0, 0, ui->lcd_width, ui->lcd_height); - goto done; - } + if ( !( lcd->lcdcon1 & 1 ) ) { + gdk_draw_drawable( ui->lcd_pixmap, ui->window->style->bg_gc[ 0 ], ui->bg_pixmap, ui->lcd_x_offset, ui->lcd_y_offset, 0, 0, + ui->lcd_width, ui->lcd_height ); + goto done; + } - gdk_draw_drawable(ui->lcd_pixmap, ui->window->style->bg_gc[0], - ui->bg_pixmap, - ui->lcd_x_offset, ui->lcd_y_offset, - 0, 0, ui->lcd_width, ui->lcd_height); + gdk_draw_drawable( ui->lcd_pixmap, ui->window->style->bg_gc[ 0 ], ui->bg_pixmap, ui->lcd_x_offset, ui->lcd_y_offset, 0, 0, + ui->lcd_width, ui->lcd_height ); - color = x49gp_get_pixel_color(lcd, 131, 0); - gdk_gc_set_rgb_fg_color(ui->ann_io_gc, &(ui->colors[UI_COLOR_GRAYSCALE_0 + color])); - gdk_draw_rectangle(ui->lcd_pixmap, ui->ann_io_gc, TRUE, 236, 0, 15, 12); + color = x49gp_get_pixel_color( lcd, 131, 0 ); + gdk_gc_set_rgb_fg_color( ui->ann_io_gc, &( ui->colors[ UI_COLOR_GRAYSCALE_0 + color ] ) ); + gdk_draw_rectangle( ui->lcd_pixmap, ui->ann_io_gc, TRUE, 236, 0, 15, 12 ); - color = x49gp_get_pixel_color(lcd, 131, 1); - gdk_gc_set_rgb_fg_color(ui->ann_left_gc, &(ui->colors[UI_COLOR_GRAYSCALE_0 + color])); - gdk_draw_rectangle(ui->lcd_pixmap, ui->ann_left_gc, TRUE, 11, 0, 15, 12); + color = x49gp_get_pixel_color( lcd, 131, 1 ); + gdk_gc_set_rgb_fg_color( ui->ann_left_gc, &( ui->colors[ UI_COLOR_GRAYSCALE_0 + color ] ) ); + gdk_draw_rectangle( ui->lcd_pixmap, ui->ann_left_gc, TRUE, 11, 0, 15, 12 ); - color = x49gp_get_pixel_color(lcd, 131, 2); - gdk_gc_set_rgb_fg_color(ui->ann_right_gc, &(ui->colors[UI_COLOR_GRAYSCALE_0 + color])); - gdk_draw_rectangle(ui->lcd_pixmap, ui->ann_right_gc, TRUE, 56, 0, 15, 12); + color = x49gp_get_pixel_color( lcd, 131, 2 ); + gdk_gc_set_rgb_fg_color( ui->ann_right_gc, &( ui->colors[ UI_COLOR_GRAYSCALE_0 + color ] ) ); + gdk_draw_rectangle( ui->lcd_pixmap, ui->ann_right_gc, TRUE, 56, 0, 15, 12 ); - color = x49gp_get_pixel_color(lcd, 131, 3); - gdk_gc_set_rgb_fg_color(ui->ann_alpha_gc, &(ui->colors[UI_COLOR_GRAYSCALE_0 + color])); - gdk_draw_rectangle(ui->lcd_pixmap, ui->ann_alpha_gc, TRUE, 101, 0, 15, 12); + color = x49gp_get_pixel_color( lcd, 131, 3 ); + gdk_gc_set_rgb_fg_color( ui->ann_alpha_gc, &( ui->colors[ UI_COLOR_GRAYSCALE_0 + color ] ) ); + gdk_draw_rectangle( ui->lcd_pixmap, ui->ann_alpha_gc, TRUE, 101, 0, 15, 12 ); - color = x49gp_get_pixel_color(lcd, 131, 4); - gdk_gc_set_rgb_fg_color(ui->ann_battery_gc, &(ui->colors[UI_COLOR_GRAYSCALE_0 + color])); - gdk_draw_rectangle(ui->lcd_pixmap, ui->ann_battery_gc, TRUE, 146, 0, 15, 12); + color = x49gp_get_pixel_color( lcd, 131, 4 ); + gdk_gc_set_rgb_fg_color( ui->ann_battery_gc, &( ui->colors[ UI_COLOR_GRAYSCALE_0 + color ] ) ); + gdk_draw_rectangle( ui->lcd_pixmap, ui->ann_battery_gc, TRUE, 146, 0, 15, 12 ); - color = x49gp_get_pixel_color(lcd, 131, 5); - gdk_gc_set_rgb_fg_color(ui->ann_busy_gc, &(ui->colors[UI_COLOR_GRAYSCALE_0 + color])); - gdk_draw_rectangle(ui->lcd_pixmap, ui->ann_busy_gc, TRUE, 191, 0, 15, 12); + color = x49gp_get_pixel_color( lcd, 131, 5 ); + gdk_gc_set_rgb_fg_color( ui->ann_busy_gc, &( ui->colors[ UI_COLOR_GRAYSCALE_0 + color ] ) ); + gdk_draw_rectangle( ui->lcd_pixmap, ui->ann_busy_gc, TRUE, 191, 0, 15, 12 ); - gc = gdk_gc_new(ui->lcd_canvas->window); + gc = gdk_gc_new( ui->lcd_canvas->window ); - for (y = 0; y < (ui->lcd_height - ui->lcd_top_margin) / 2; y++) { - for (x = 0; x < ui->lcd_width / 2; x++) { - color = x49gp_get_pixel_color(lcd, x, y); - gdk_gc_set_rgb_fg_color(gc, &(ui->colors[UI_COLOR_GRAYSCALE_0 + color])); - gdk_draw_rectangle(ui->lcd_pixmap, gc, TRUE, - 2 * x, 2 * y + ui->lcd_top_margin, 2, 2); - } - } + for ( y = 0; y < ( ui->lcd_height - ui->lcd_top_margin ) / 2; y++ ) { + for ( x = 0; x < ui->lcd_width / 2; x++ ) { + color = x49gp_get_pixel_color( lcd, x, y ); + gdk_gc_set_rgb_fg_color( gc, &( ui->colors[ UI_COLOR_GRAYSCALE_0 + color ] ) ); + gdk_draw_rectangle( ui->lcd_pixmap, gc, TRUE, 2 * x, 2 * y + ui->lcd_top_margin, 2, 2 ); + } + } - g_object_unref(gc); + g_object_unref( gc ); done: - rect.x = 0; - rect.y = 0; - rect.width = ui->lcd_width; - rect.height = ui->lcd_height; + rect.x = 0; + rect.y = 0; + rect.width = ui->lcd_width; + rect.height = ui->lcd_height; - gdk_window_invalidate_rect(ui->lcd_canvas->window, &rect, FALSE); + gdk_window_invalidate_rect( ui->lcd_canvas->window, &rect, FALSE ); } - -static uint32_t -s3c2410_lcd_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_lcd_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_lcd_t *lcd = opaque; - s3c2410_offset_t *reg; - uint32_t linecnt; + s3c2410_lcd_t* lcd = opaque; + s3c2410_offset_t* reg; + uint32_t linecnt; - if (! S3C2410_OFFSET_OK(lcd, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( lcd, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(lcd, offset); + reg = S3C2410_OFFSET_ENTRY( lcd, offset ); - switch (offset) { - case S3C2410_LCD_LCDCON1: - linecnt = (lcd->lcdcon1 >> 18) & 0x3ff; - if (linecnt > 0) { - linecnt--; - } else { - linecnt = (lcd->lcdcon2 >> 14) & 0x3ff; - } + switch ( offset ) { + case S3C2410_LCD_LCDCON1: + linecnt = ( lcd->lcdcon1 >> 18 ) & 0x3ff; + if ( linecnt > 0 ) { + linecnt--; + } else { + linecnt = ( lcd->lcdcon2 >> 14 ) & 0x3ff; + } - lcd->lcdcon1 &= ~(0x3ff << 18); - lcd->lcdcon1 |= (linecnt << 18); - } + lcd->lcdcon1 &= ~( 0x3ff << 18 ); + lcd->lcdcon1 |= ( linecnt << 18 ); + } #ifdef DEBUG_S3C2410_LCD - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-lcd", S3C2410_LCD_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-lcd", S3C2410_LCD_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_lcd_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_lcd_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_lcd_t *lcd = opaque; - x49gp_t *x49gp = lcd->x49gp; - s3c2410_offset_t *reg; + s3c2410_lcd_t* lcd = opaque; + x49gp_t* x49gp = lcd->x49gp; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(lcd, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( lcd, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(lcd, offset); + reg = S3C2410_OFFSET_ENTRY( lcd, offset ); #ifdef DEBUG_S3C2410_LCD - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-lcd", S3C2410_LCD_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-lcd", S3C2410_LCD_BASE, reg->name, ( unsigned long )offset, data ); #endif - switch (offset) { - case S3C2410_LCD_LCDCON1: - if ((lcd->lcdcon1 ^ data) & 1) { - x49gp_schedule_lcd_update(x49gp); - } + switch ( offset ) { + case S3C2410_LCD_LCDCON1: + if ( ( lcd->lcdcon1 ^ data ) & 1 ) { + x49gp_schedule_lcd_update( x49gp ); + } - lcd->lcdcon1 = (lcd->lcdcon1 & (0x3ff << 18)) | - (data & ~(0x3ff << 18)); - break; - default: - *(reg->datap) = data; - break; - } + lcd->lcdcon1 = ( lcd->lcdcon1 & ( 0x3ff << 18 ) ) | ( data & ~( 0x3ff << 18 ) ); + break; + default: + *( reg->datap ) = data; + break; + } } - -static int -s3c2410_lcd_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_lcd_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_lcd_t *lcd = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_lcd_t* lcd = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < lcd->nr_regs; i++) { - reg = &lcd->regs[i]; + for ( i = 0; i < lcd->nr_regs; i++ ) { + reg = &lcd->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_lcd_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_lcd_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_lcd_t *lcd = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_lcd_t* lcd = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < lcd->nr_regs; i++) { - reg = &lcd->regs[i]; + for ( i = 0; i < lcd->nr_regs; i++ ) { + reg = &lcd->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_lcd_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_lcd_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_lcd_t *lcd = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_lcd_t* lcd = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < lcd->nr_regs; i++) { - reg = &lcd->regs[i]; + for ( i = 0; i < lcd->nr_regs; i++ ) { + reg = &lcd->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_lcd_readfn[] = -{ - s3c2410_lcd_read, - s3c2410_lcd_read, - s3c2410_lcd_read -}; +static CPUReadMemoryFunc* s3c2410_lcd_readfn[] = { s3c2410_lcd_read, s3c2410_lcd_read, s3c2410_lcd_read }; -static CPUWriteMemoryFunc *s3c2410_lcd_writefn[] = -{ - s3c2410_lcd_write, - s3c2410_lcd_write, - s3c2410_lcd_write -}; +static CPUWriteMemoryFunc* s3c2410_lcd_writefn[] = { s3c2410_lcd_write, s3c2410_lcd_write, s3c2410_lcd_write }; -static int -s3c2410_lcd_init(x49gp_module_t *module) +static int s3c2410_lcd_init( x49gp_module_t* module ) { - s3c2410_lcd_t *lcd; - int iotype; + s3c2410_lcd_t* lcd; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - lcd = malloc(sizeof(s3c2410_lcd_t)); - if (NULL == lcd) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_lcd_data_init(lcd)) { - free(lcd); - return -ENOMEM; - } + lcd = malloc( sizeof( s3c2410_lcd_t ) ); + if ( NULL == lcd ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_lcd_data_init( lcd ) ) { + free( lcd ); + return -ENOMEM; + } - module->user_data = lcd; - module->x49gp->s3c2410_lcd = lcd; - lcd->x49gp = module->x49gp; + module->user_data = lcd; + module->x49gp->s3c2410_lcd = lcd; + lcd->x49gp = module->x49gp; - iotype = cpu_register_io_memory(s3c2410_lcd_readfn, - s3c2410_lcd_writefn, lcd); + iotype = cpu_register_io_memory( s3c2410_lcd_readfn, s3c2410_lcd_writefn, lcd ); #ifdef DEBUG_S3C2410_LCD - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_LCD_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_LCD_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_lcd_exit(x49gp_module_t *module) +static int s3c2410_lcd_exit( x49gp_module_t* module ) { - s3c2410_lcd_t *lcd; + s3c2410_lcd_t* lcd; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - lcd = module->user_data; - if (lcd->regs) - free(lcd->regs); - free(lcd); - } + if ( module->user_data ) { + lcd = module->user_data; + if ( lcd->regs ) + free( lcd->regs ); + free( lcd ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_lcd_init(x49gp_t *x49gp) +int x49gp_s3c2410_lcd_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-lcd", - s3c2410_lcd_init, - s3c2410_lcd_exit, - s3c2410_lcd_reset, - s3c2410_lcd_load, - s3c2410_lcd_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-lcd", s3c2410_lcd_init, s3c2410_lcd_exit, s3c2410_lcd_reset, s3c2410_lcd_load, s3c2410_lcd_save, + NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_memc.c b/src/s3c2410_memc.c index 95c89e6..d0ce3cc 100644 --- a/src/s3c2410_memc.c +++ b/src/s3c2410_memc.c @@ -12,263 +12,231 @@ #include "x49gp.h" #include "s3c2410.h" - typedef struct { - uint32_t bwscon; - uint32_t bankcon0; - uint32_t bankcon1; - uint32_t bankcon2; - uint32_t bankcon3; - uint32_t bankcon4; - uint32_t bankcon5; - uint32_t bankcon6; - uint32_t bankcon7; - uint32_t refresh; - uint32_t banksize; - uint32_t mrsrb6; - uint32_t mrsrb7; + uint32_t bwscon; + uint32_t bankcon0; + uint32_t bankcon1; + uint32_t bankcon2; + uint32_t bankcon3; + uint32_t bankcon4; + uint32_t bankcon5; + uint32_t bankcon6; + uint32_t bankcon7; + uint32_t refresh; + uint32_t banksize; + uint32_t mrsrb6; + uint32_t mrsrb7; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; } s3c2410_memc_t; -static int -s3c2410_memc_data_init(s3c2410_memc_t *memc) +static int s3c2410_memc_data_init( s3c2410_memc_t* memc ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(MEMC, BWSCON, 0x00000000, memc->bwscon), - S3C2410_OFFSET(MEMC, BANKCON0, 0x00000700, memc->bankcon0), - S3C2410_OFFSET(MEMC, BANKCON1, 0x00000700, memc->bankcon1), - S3C2410_OFFSET(MEMC, BANKCON2, 0x00000700, memc->bankcon2), - S3C2410_OFFSET(MEMC, BANKCON3, 0x00000700, memc->bankcon3), - S3C2410_OFFSET(MEMC, BANKCON4, 0x00000700, memc->bankcon4), - S3C2410_OFFSET(MEMC, BANKCON5, 0x00000700, memc->bankcon5), - S3C2410_OFFSET(MEMC, BANKCON6, 0x00018008, memc->bankcon6), - S3C2410_OFFSET(MEMC, BANKCON7, 0x00018008, memc->bankcon7), - S3C2410_OFFSET(MEMC, REFRESH, 0x00ac0000, memc->refresh), - S3C2410_OFFSET(MEMC, BANKSIZE, 0x00000000, memc->banksize), - S3C2410_OFFSET(MEMC, MRSRB6, 0, memc->mrsrb6), - S3C2410_OFFSET(MEMC, MRSRB7, 0, memc->mrsrb7), - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( MEMC, BWSCON, 0x00000000, memc->bwscon ), + S3C2410_OFFSET( MEMC, BANKCON0, 0x00000700, memc->bankcon0 ), + S3C2410_OFFSET( MEMC, BANKCON1, 0x00000700, memc->bankcon1 ), + S3C2410_OFFSET( MEMC, BANKCON2, 0x00000700, memc->bankcon2 ), + S3C2410_OFFSET( MEMC, BANKCON3, 0x00000700, memc->bankcon3 ), + S3C2410_OFFSET( MEMC, BANKCON4, 0x00000700, memc->bankcon4 ), + S3C2410_OFFSET( MEMC, BANKCON5, 0x00000700, memc->bankcon5 ), + S3C2410_OFFSET( MEMC, BANKCON6, 0x00018008, memc->bankcon6 ), + S3C2410_OFFSET( MEMC, BANKCON7, 0x00018008, memc->bankcon7 ), + S3C2410_OFFSET( MEMC, REFRESH, 0x00ac0000, memc->refresh ), + S3C2410_OFFSET( MEMC, BANKSIZE, 0x00000000, memc->banksize ), + S3C2410_OFFSET( MEMC, MRSRB6, 0, memc->mrsrb6 ), + S3C2410_OFFSET( MEMC, MRSRB7, 0, memc->mrsrb7 ), + }; - memset(memc, 0, sizeof(s3c2410_memc_t)); + memset( memc, 0, sizeof( s3c2410_memc_t ) ); - memc->regs = malloc(sizeof(regs)); - if (NULL == memc->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + memc->regs = malloc( sizeof( regs ) ); + if ( NULL == memc->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(memc->regs, regs, sizeof(regs)); - memc->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( memc->regs, regs, sizeof( regs ) ); + memc->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static uint32_t -s3c2410_memc_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_memc_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_memc_t *memc = opaque; - s3c2410_offset_t *reg; + s3c2410_memc_t* memc = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(memc, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( memc, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(memc, offset); + reg = S3C2410_OFFSET_ENTRY( memc, offset ); #ifdef DEBUG_S3C2410_MEMC - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-memc", S3C2410_MEMC_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-memc", S3C2410_MEMC_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_memc_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_memc_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_memc_t *memc = opaque; - s3c2410_offset_t *reg; + s3c2410_memc_t* memc = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(memc, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( memc, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(memc, offset); + reg = S3C2410_OFFSET_ENTRY( memc, offset ); #ifdef DEBUG_S3C2410_MEMC - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-memc", S3C2410_MEMC_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-memc", S3C2410_MEMC_BASE, reg->name, ( unsigned long )offset, data ); #endif - *(reg->datap) = data; + *( reg->datap ) = data; #ifdef DEBUG_S3C2410_MEMC - printf("%s:%u: env %p\n", __FUNCTION__, __LINE__, memc->x49gp->env); + printf( "%s:%u: env %p\n", __FUNCTION__, __LINE__, memc->x49gp->env ); #endif } -static int -s3c2410_memc_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_memc_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_memc_t *memc = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_memc_t* memc = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < memc->nr_regs; i++) { - reg = &memc->regs[i]; + for ( i = 0; i < memc->nr_regs; i++ ) { + reg = &memc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_memc_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_memc_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_memc_t *memc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_memc_t* memc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < memc->nr_regs; i++) { - reg = &memc->regs[i]; + for ( i = 0; i < memc->nr_regs; i++ ) { + reg = &memc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_memc_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_memc_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_memc_t *memc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_memc_t* memc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < memc->nr_regs; i++) { - reg = &memc->regs[i]; + for ( i = 0; i < memc->nr_regs; i++ ) { + reg = &memc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_memc_readfn[] = -{ - s3c2410_memc_read, - s3c2410_memc_read, - s3c2410_memc_read -}; +static CPUReadMemoryFunc* s3c2410_memc_readfn[] = { s3c2410_memc_read, s3c2410_memc_read, s3c2410_memc_read }; -static CPUWriteMemoryFunc *s3c2410_memc_writefn[] = -{ - s3c2410_memc_write, - s3c2410_memc_write, - s3c2410_memc_write -}; +static CPUWriteMemoryFunc* s3c2410_memc_writefn[] = { s3c2410_memc_write, s3c2410_memc_write, s3c2410_memc_write }; -static int -s3c2410_memc_init(x49gp_module_t *module) +static int s3c2410_memc_init( x49gp_module_t* module ) { - s3c2410_memc_t *memc; - int iotype; + s3c2410_memc_t* memc; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - memc = malloc(sizeof(s3c2410_memc_t)); - if (NULL == memc) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_memc_data_init(memc)) { - free(memc); - return -ENOMEM; - } + memc = malloc( sizeof( s3c2410_memc_t ) ); + if ( NULL == memc ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_memc_data_init( memc ) ) { + free( memc ); + return -ENOMEM; + } - module->user_data = memc; - memc->x49gp = module->x49gp; + module->user_data = memc; + memc->x49gp = module->x49gp; - iotype = cpu_register_io_memory(s3c2410_memc_readfn, - s3c2410_memc_writefn, memc); + iotype = cpu_register_io_memory( s3c2410_memc_readfn, s3c2410_memc_writefn, memc ); #ifdef DEBUG_S3C2410_MEMC - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_MEMC_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_MEMC_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_memc_exit(x49gp_module_t *module) +static int s3c2410_memc_exit( x49gp_module_t* module ) { - s3c2410_memc_t *memc; + s3c2410_memc_t* memc; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - memc = module->user_data; - if (memc->regs) - free(memc->regs); - free(memc); - } + if ( module->user_data ) { + memc = module->user_data; + if ( memc->regs ) + free( memc->regs ); + free( memc ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_memc_init(x49gp_t *x49gp) +int x49gp_s3c2410_memc_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-memc", - s3c2410_memc_init, - s3c2410_memc_exit, - s3c2410_memc_reset, - s3c2410_memc_load, - s3c2410_memc_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-memc", s3c2410_memc_init, s3c2410_memc_exit, s3c2410_memc_reset, s3c2410_memc_load, + s3c2410_memc_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_mmu.h b/src/s3c2410_mmu.h index 561e2da..3f380c1 100644 --- a/src/s3c2410_mmu.h +++ b/src/s3c2410_mmu.h @@ -4,34 +4,34 @@ #ifndef _S3C2410_MMU_H #define _S3C2410_MMU_H 1 -#define S3C2410_MMU_TLB_SIZE 64 -#define S3C2410_MMU_TLB_MASK (S3C2410_MMU_TLB_SIZE - 1) +#define S3C2410_MMU_TLB_SIZE 64 +#define S3C2410_MMU_TLB_MASK ( S3C2410_MMU_TLB_SIZE - 1 ) typedef struct { - uint32_t mva; - uint32_t mask; - uint32_t pa; - uint32_t dac; - int valid; + uint32_t mva; + uint32_t mask; + uint32_t pa; + uint32_t dac; + int valid; } TLB_entry_t; typedef struct { - int victim; - int base; - int index0; - int index1; - unsigned long hit0; - unsigned long hit1; - unsigned long search; - unsigned long nsearch; - unsigned long walk; - TLB_entry_t data[S3C2410_MMU_TLB_SIZE]; + int victim; + int base; + int index0; + int index1; + unsigned long hit0; + unsigned long hit1; + unsigned long search; + unsigned long nsearch; + unsigned long walk; + TLB_entry_t data[ S3C2410_MMU_TLB_SIZE ]; } TLB_t; typedef struct { - uint32_t MMUReg[16]; - TLB_t iTLB; - TLB_t dTLB; + uint32_t MMUReg[ 16 ]; + TLB_t iTLB; + TLB_t dTLB; } s3c2410_mmu_t; #endif /* !(_S3C2410_MMU_H) */ diff --git a/src/s3c2410_nand.c b/src/s3c2410_nand.c index 63f3ffb..c6fa757 100644 --- a/src/s3c2410_nand.c +++ b/src/s3c2410_nand.c @@ -12,244 +12,206 @@ #include "x49gp.h" #include "s3c2410.h" - typedef struct { - uint32_t nfconf; - uint32_t nfcmd; - uint32_t nfaddr; - uint32_t nfdata; - uint32_t nfstat; - uint32_t nfecc; + uint32_t nfconf; + uint32_t nfcmd; + uint32_t nfaddr; + uint32_t nfdata; + uint32_t nfstat; + uint32_t nfecc; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; } s3c2410_nand_t; - -static int -s3c2410_nand_data_init(s3c2410_nand_t *nand) +static int s3c2410_nand_data_init( s3c2410_nand_t* nand ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(NAND, NFCONF, 0x00000000, nand->nfconf), - S3C2410_OFFSET(NAND, NFCMD, 0x00000000, nand->nfcmd), - S3C2410_OFFSET(NAND, NFADDR, 0x00000000, nand->nfaddr), - S3C2410_OFFSET(NAND, NFDATA, 0x00000000, nand->nfdata), - S3C2410_OFFSET(NAND, NFSTAT, 0x00000000, nand->nfstat), - S3C2410_OFFSET(NAND, NFECC, 0x00000000, nand->nfecc), - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( NAND, NFCONF, 0x00000000, nand->nfconf ), S3C2410_OFFSET( NAND, NFCMD, 0x00000000, nand->nfcmd ), + S3C2410_OFFSET( NAND, NFADDR, 0x00000000, nand->nfaddr ), S3C2410_OFFSET( NAND, NFDATA, 0x00000000, nand->nfdata ), + S3C2410_OFFSET( NAND, NFSTAT, 0x00000000, nand->nfstat ), S3C2410_OFFSET( NAND, NFECC, 0x00000000, nand->nfecc ), + }; - memset(nand, 0, sizeof(s3c2410_nand_t)); + memset( nand, 0, sizeof( s3c2410_nand_t ) ); - nand->regs = malloc(sizeof(regs)); - if (NULL == nand->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + nand->regs = malloc( sizeof( regs ) ); + if ( NULL == nand->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(nand->regs, regs, sizeof(regs)); - nand->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( nand->regs, regs, sizeof( regs ) ); + nand->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -uint32_t -s3c2410_nand_read(void *opaque, target_phys_addr_t offset) +uint32_t s3c2410_nand_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_nand_t *nand = opaque; - s3c2410_offset_t *reg; + s3c2410_nand_t* nand = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(nand, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( nand, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(nand, offset); + reg = S3C2410_OFFSET_ENTRY( nand, offset ); #ifdef DEBUG_S3C2410_NAND - printf("read %s [%08x] %s [%08x] data %08x\n", - "s3c2410-nand", S3C2410_NAND_BASE, - reg->name, offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08x] data %08x\n", "s3c2410-nand", S3C2410_NAND_BASE, reg->name, offset, *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -void -s3c2410_nand_write(void *opaque, target_phys_addr_t offset, uint32_t data) +void s3c2410_nand_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_nand_t *nand = opaque; - s3c2410_offset_t *reg; + s3c2410_nand_t* nand = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(nand, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( nand, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(nand, offset); + reg = S3C2410_OFFSET_ENTRY( nand, offset ); #ifdef DEBUG_S3C2410_NAND - printf("write %s [%08x] %s [%08x] data %08x\n", - "s3c2410-nand", S3C2410_NAND_BASE, - reg->name, offset, data); + printf( "write %s [%08x] %s [%08x] data %08x\n", "s3c2410-nand", S3C2410_NAND_BASE, reg->name, offset, data ); #endif - *(reg->datap) = data; + *( reg->datap ) = data; } - -static int -s3c2410_nand_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_nand_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_nand_t *nand = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_nand_t* nand = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < nand->nr_regs; i++) { - reg = &nand->regs[i]; + for ( i = 0; i < nand->nr_regs; i++ ) { + reg = &nand->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_nand_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_nand_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_nand_t *nand = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_nand_t* nand = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < nand->nr_regs; i++) { - reg = &nand->regs[i]; + for ( i = 0; i < nand->nr_regs; i++ ) { + reg = &nand->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_nand_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_nand_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_nand_t *nand = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_nand_t* nand = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < nand->nr_regs; i++) { - reg = &nand->regs[i]; + for ( i = 0; i < nand->nr_regs; i++ ) { + reg = &nand->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_nand_readfn[] = -{ - s3c2410_nand_read, - s3c2410_nand_read, - s3c2410_nand_read -}; +static CPUReadMemoryFunc* s3c2410_nand_readfn[] = { s3c2410_nand_read, s3c2410_nand_read, s3c2410_nand_read }; -static CPUWriteMemoryFunc *s3c2410_nand_writefn[] = -{ - s3c2410_nand_write, - s3c2410_nand_write, - s3c2410_nand_write -}; +static CPUWriteMemoryFunc* s3c2410_nand_writefn[] = { s3c2410_nand_write, s3c2410_nand_write, s3c2410_nand_write }; -static int -s3c2410_nand_init(x49gp_module_t *module) +static int s3c2410_nand_init( x49gp_module_t* module ) { - s3c2410_nand_t *nand; - int iotype; + s3c2410_nand_t* nand; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - nand = malloc(sizeof(s3c2410_nand_t)); - if (NULL == nand) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_nand_data_init(nand)) { - free(nand); - return -ENOMEM; - } + nand = malloc( sizeof( s3c2410_nand_t ) ); + if ( NULL == nand ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_nand_data_init( nand ) ) { + free( nand ); + return -ENOMEM; + } - module->user_data = nand; + module->user_data = nand; - iotype = cpu_register_io_memory(s3c2410_nand_readfn, - s3c2410_nand_writefn, nand); + iotype = cpu_register_io_memory( s3c2410_nand_readfn, s3c2410_nand_writefn, nand ); #ifdef DEBUG_S3C2410_NAND - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_NAND_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_NAND_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_nand_exit(x49gp_module_t *module) +static int s3c2410_nand_exit( x49gp_module_t* module ) { - s3c2410_nand_t *nand; + s3c2410_nand_t* nand; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - nand = module->user_data; - if (nand->regs) - free(nand->regs); - free(nand); - } + if ( module->user_data ) { + nand = module->user_data; + if ( nand->regs ) + free( nand->regs ); + free( nand ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_nand_init(x49gp_t *x49gp) +int x49gp_s3c2410_nand_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-nand", - s3c2410_nand_init, - s3c2410_nand_exit, - s3c2410_nand_reset, - s3c2410_nand_load, - s3c2410_nand_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-nand", s3c2410_nand_init, s3c2410_nand_exit, s3c2410_nand_reset, s3c2410_nand_load, + s3c2410_nand_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_power.c b/src/s3c2410_power.c index 2081b6c..6d53128 100644 --- a/src/s3c2410_power.c +++ b/src/s3c2410_power.c @@ -16,351 +16,311 @@ static const uint32_t EXTCLK = 12000000; typedef struct { - uint32_t locktime; - uint32_t mpllcon; - uint32_t upllcon; - uint32_t clkcon; - uint32_t clkslow; - uint32_t clkdivn; + uint32_t locktime; + uint32_t mpllcon; + uint32_t upllcon; + uint32_t clkcon; + uint32_t clkslow; + uint32_t clkdivn; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; } s3c2410_power_t; -static int -s3c2410_power_data_init(s3c2410_power_t *power) +static int s3c2410_power_data_init( s3c2410_power_t* power ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(POWER, LOCKTIME, 0x00ffffff, power->locktime), - S3C2410_OFFSET(POWER, MPLLCON, 0x0005c080, power->mpllcon), - S3C2410_OFFSET(POWER, UPLLCON, 0x00028080, power->upllcon), - S3C2410_OFFSET(POWER, CLKCON, 0x0007fff0, power->clkcon), - S3C2410_OFFSET(POWER, CLKSLOW, 0x00000004, power->clkslow), - S3C2410_OFFSET(POWER, CLKDIVN, 0x00000000, power->clkdivn) - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( POWER, LOCKTIME, 0x00ffffff, power->locktime ), S3C2410_OFFSET( POWER, MPLLCON, 0x0005c080, power->mpllcon ), + S3C2410_OFFSET( POWER, UPLLCON, 0x00028080, power->upllcon ), S3C2410_OFFSET( POWER, CLKCON, 0x0007fff0, power->clkcon ), + S3C2410_OFFSET( POWER, CLKSLOW, 0x00000004, power->clkslow ), S3C2410_OFFSET( POWER, CLKDIVN, 0x00000000, power->clkdivn ) }; - memset(power, 0, sizeof(s3c2410_power_t)); + memset( power, 0, sizeof( s3c2410_power_t ) ); - power->regs = malloc(sizeof(regs)); - if (NULL == power->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + power->regs = malloc( sizeof( regs ) ); + if ( NULL == power->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(power->regs, regs, sizeof(regs)); - power->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( power->regs, regs, sizeof( regs ) ); + power->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static uint32_t -s3c2410_power_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_power_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_power_t *power = opaque; - s3c2410_offset_t *reg; + s3c2410_power_t* power = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(power, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( power, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(power, offset); + reg = S3C2410_OFFSET_ENTRY( power, offset ); #ifdef DEBUG_S3C2410_POWER - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-power", S3C2410_POWER_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-power", S3C2410_POWER_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_power_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_power_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_power_t *power = opaque; - x49gp_t *x49gp = power->x49gp; - s3c2410_offset_t *reg; - uint32_t mMdiv, mPdiv, mSdiv; - uint32_t uMdiv, uPdiv, uSdiv; - uint32_t slow_bit, slow_val; + s3c2410_power_t* power = opaque; + x49gp_t* x49gp = power->x49gp; + s3c2410_offset_t* reg; + uint32_t mMdiv, mPdiv, mSdiv; + uint32_t uMdiv, uPdiv, uSdiv; + uint32_t slow_bit, slow_val; - if (! S3C2410_OFFSET_OK(power, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( power, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(power, offset); + reg = S3C2410_OFFSET_ENTRY( power, offset ); #ifdef DEBUG_S3C2410_POWER - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-power", S3C2410_POWER_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-power", S3C2410_POWER_BASE, reg->name, ( unsigned long )offset, data ); #endif - switch (offset) { - case S3C2410_POWER_CLKCON: - if (data & CLKCON_POWER_OFF) { - *(reg->datap) = 0x7fff0; + switch ( offset ) { + case S3C2410_POWER_CLKCON: + if ( data & CLKCON_POWER_OFF ) { + *( reg->datap ) = 0x7fff0; #ifdef DEBUG_S3C2410_POWER - printf("POWER: enter POWER_OFF\n"); + printf( "POWER: enter POWER_OFF\n" ); #endif - x49gp_modules_reset(x49gp, X49GP_RESET_POWER_OFF); + x49gp_modules_reset( x49gp, X49GP_RESET_POWER_OFF ); -// if (x49gp->arm->NresetSig != LOW) { -// x49gp->arm->NresetSig = LOW; -// x49gp->arm->Exception++; -// } - x49gp_set_idle(x49gp, X49GP_ARM_OFF); - return; - } + // if (x49gp->arm->NresetSig != LOW) { + // x49gp->arm->NresetSig = LOW; + // x49gp->arm->Exception++; + // } + x49gp_set_idle( x49gp, X49GP_ARM_OFF ); + return; + } - if (!(power->clkcon & CLKCON_IDLE) && (data & CLKCON_IDLE)) { - *(reg->datap) = data; + if ( !( power->clkcon & CLKCON_IDLE ) && ( data & CLKCON_IDLE ) ) { + *( reg->datap ) = data; #ifdef DEBUG_S3C2410_POWER - printf("POWER: enter IDLE\n"); + printf( "POWER: enter IDLE\n" ); #endif - x49gp_set_idle(x49gp, X49GP_ARM_SLEEP); - return; - } + x49gp_set_idle( x49gp, X49GP_ARM_SLEEP ); + return; + } - *(reg->datap) = data; - return; + *( reg->datap ) = data; + return; - case S3C2410_POWER_LOCKTIME: - *(reg->datap) = data; - return; + case S3C2410_POWER_LOCKTIME: + *( reg->datap ) = data; + return; - default: - *(reg->datap) = data; - break; - } + default: + *( reg->datap ) = data; + break; + } - mMdiv = (power->mpllcon >> 12) & 0xff; - mPdiv = (power->mpllcon >> 4) & 0x3f; - mSdiv = (power->mpllcon >> 0) & 0x03; - x49gp->MCLK = (((u64) EXTCLK) * ((u64) (mMdiv + 8))) / ((u64) ((mPdiv + 2) * (1 << mSdiv))); + mMdiv = ( power->mpllcon >> 12 ) & 0xff; + mPdiv = ( power->mpllcon >> 4 ) & 0x3f; + mSdiv = ( power->mpllcon >> 0 ) & 0x03; + x49gp->MCLK = ( ( ( u64 )EXTCLK ) * ( ( u64 )( mMdiv + 8 ) ) ) / ( ( u64 )( ( mPdiv + 2 ) * ( 1 << mSdiv ) ) ); - uMdiv = (power->upllcon >> 12) & 0xff; - uPdiv = (power->upllcon >> 4) & 0x3f; - uSdiv = (power->upllcon >> 0) & 0x03; - x49gp->UCLK = (((u64) EXTCLK) * ((u64) (uMdiv + 8))) / ((u64) ((uPdiv + 2) * (1 << uSdiv))); + uMdiv = ( power->upllcon >> 12 ) & 0xff; + uPdiv = ( power->upllcon >> 4 ) & 0x3f; + uSdiv = ( power->upllcon >> 0 ) & 0x03; + x49gp->UCLK = ( ( ( u64 )EXTCLK ) * ( ( u64 )( uMdiv + 8 ) ) ) / ( ( u64 )( ( uPdiv + 2 ) * ( 1 << uSdiv ) ) ); - slow_bit = (power->clkslow & 0x10); - if (slow_bit) { - slow_val = (power->clkslow >> 0) & 0x07; - if (0 == slow_val) - x49gp->FCLK = EXTCLK; - else - x49gp->FCLK = EXTCLK / (2 * slow_val); - } else { - x49gp->FCLK = x49gp->MCLK; - } + slow_bit = ( power->clkslow & 0x10 ); + if ( slow_bit ) { + slow_val = ( power->clkslow >> 0 ) & 0x07; + if ( 0 == slow_val ) + x49gp->FCLK = EXTCLK; + else + x49gp->FCLK = EXTCLK / ( 2 * slow_val ); + } else { + x49gp->FCLK = x49gp->MCLK; + } - if (power->clkdivn & 4) { - x49gp->HCLK = x49gp->FCLK / 4; - x49gp->PCLK = x49gp->FCLK / 4; - x49gp->PCLK_ratio = 4; - } else { - switch (power->clkdivn & 3) { - case 0: - x49gp->HCLK = x49gp->FCLK; - x49gp->PCLK = x49gp->HCLK; - x49gp->PCLK_ratio = 1; - break; - case 1: - x49gp->HCLK = x49gp->FCLK; - x49gp->PCLK = x49gp->HCLK / 2; - x49gp->PCLK_ratio = 2; - break; - case 2: - x49gp->HCLK = x49gp->FCLK / 2; - x49gp->PCLK = x49gp->HCLK; - x49gp->PCLK_ratio = 2; - break; - case 3: - x49gp->HCLK = x49gp->FCLK / 2; - x49gp->PCLK = x49gp->HCLK / 2; - x49gp->PCLK_ratio = 4; - break; - } - } + if ( power->clkdivn & 4 ) { + x49gp->HCLK = x49gp->FCLK / 4; + x49gp->PCLK = x49gp->FCLK / 4; + x49gp->PCLK_ratio = 4; + } else { + switch ( power->clkdivn & 3 ) { + case 0: + x49gp->HCLK = x49gp->FCLK; + x49gp->PCLK = x49gp->HCLK; + x49gp->PCLK_ratio = 1; + break; + case 1: + x49gp->HCLK = x49gp->FCLK; + x49gp->PCLK = x49gp->HCLK / 2; + x49gp->PCLK_ratio = 2; + break; + case 2: + x49gp->HCLK = x49gp->FCLK / 2; + x49gp->PCLK = x49gp->HCLK; + x49gp->PCLK_ratio = 2; + break; + case 3: + x49gp->HCLK = x49gp->FCLK / 2; + x49gp->PCLK = x49gp->HCLK / 2; + x49gp->PCLK_ratio = 4; + break; + } + } #ifdef DEBUG_S3C2410_POWER - printf("%s: EXTCLK %u, mdiv %u, pdiv %u, sdiv %u: MCLK %u\n", - __FUNCTION__, EXTCLK, mMdiv, mPdiv, mSdiv, x49gp->MCLK); - printf("%s: EXTCLK %u, mdiv %u, pdiv %u, sdiv %u: UCLK %u\n", - __FUNCTION__, EXTCLK, uMdiv, uPdiv, uSdiv, x49gp->UCLK); - printf("%s: FCLK %s: %u\n", - __FUNCTION__, slow_bit ? "(slow)" : "", x49gp->FCLK); - printf("%s: HCLK %u, PCLK %u\n", - __FUNCTION__, x49gp->HCLK, x49gp->PCLK); + printf( "%s: EXTCLK %u, mdiv %u, pdiv %u, sdiv %u: MCLK %u\n", __FUNCTION__, EXTCLK, mMdiv, mPdiv, mSdiv, x49gp->MCLK ); + printf( "%s: EXTCLK %u, mdiv %u, pdiv %u, sdiv %u: UCLK %u\n", __FUNCTION__, EXTCLK, uMdiv, uPdiv, uSdiv, x49gp->UCLK ); + printf( "%s: FCLK %s: %u\n", __FUNCTION__, slow_bit ? "(slow)" : "", x49gp->FCLK ); + printf( "%s: HCLK %u, PCLK %u\n", __FUNCTION__, x49gp->HCLK, x49gp->PCLK ); #endif } -static int -s3c2410_power_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_power_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_power_t *power = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_power_t* power = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < power->nr_regs; i++) { - reg = &power->regs[i]; + for ( i = 0; i < power->nr_regs; i++ ) { + reg = &power->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - if (error) { - return error; - } + if ( error ) { + return error; + } - s3c2410_power_write(power, S3C2410_POWER_BASE | S3C2410_POWER_CLKDIVN, - power->clkdivn); - return 0; + s3c2410_power_write( power, S3C2410_POWER_BASE | S3C2410_POWER_CLKDIVN, power->clkdivn ); + return 0; } -static int -s3c2410_power_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_power_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_power_t *power = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_power_t* power = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < power->nr_regs; i++) { - reg = &power->regs[i]; + for ( i = 0; i < power->nr_regs; i++ ) { + reg = &power->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_power_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_power_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_power_t *power = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_power_t* power = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < power->nr_regs; i++) { - reg = &power->regs[i]; + for ( i = 0; i < power->nr_regs; i++ ) { + reg = &power->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_power_readfn[] = -{ - s3c2410_power_read, - s3c2410_power_read, - s3c2410_power_read -}; +static CPUReadMemoryFunc* s3c2410_power_readfn[] = { s3c2410_power_read, s3c2410_power_read, s3c2410_power_read }; -static CPUWriteMemoryFunc *s3c2410_power_writefn[] = -{ - s3c2410_power_write, - s3c2410_power_write, - s3c2410_power_write -}; +static CPUWriteMemoryFunc* s3c2410_power_writefn[] = { s3c2410_power_write, s3c2410_power_write, s3c2410_power_write }; -static int -s3c2410_power_init(x49gp_module_t *module) +static int s3c2410_power_init( x49gp_module_t* module ) { - s3c2410_power_t *power; - int iotype; + s3c2410_power_t* power; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - power = malloc(sizeof(s3c2410_power_t)); - if (NULL == power) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_power_data_init(power)) { - free(power); - return -ENOMEM; - } + power = malloc( sizeof( s3c2410_power_t ) ); + if ( NULL == power ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_power_data_init( power ) ) { + free( power ); + return -ENOMEM; + } - module->user_data = power; - power->x49gp = module->x49gp; + module->user_data = power; + power->x49gp = module->x49gp; - iotype = cpu_register_io_memory(s3c2410_power_readfn, - s3c2410_power_writefn, power); + iotype = cpu_register_io_memory( s3c2410_power_readfn, s3c2410_power_writefn, power ); #ifdef DEBUG_S3C2410_POWER - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_POWER_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_POWER_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_power_exit(x49gp_module_t *module) +static int s3c2410_power_exit( x49gp_module_t* module ) { - s3c2410_power_t *power; + s3c2410_power_t* power; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - power = module->user_data; - if (power->regs) - free(power->regs); - free(power); - } + if ( module->user_data ) { + power = module->user_data; + if ( power->regs ) + free( power->regs ); + free( power ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_power_init(x49gp_t *x49gp) +int x49gp_s3c2410_power_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-power", - s3c2410_power_init, - s3c2410_power_exit, - s3c2410_power_reset, - s3c2410_power_load, - s3c2410_power_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-power", s3c2410_power_init, s3c2410_power_exit, s3c2410_power_reset, s3c2410_power_load, + s3c2410_power_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_power.h b/src/s3c2410_power.h index 88c3a57..1901c43 100644 --- a/src/s3c2410_power.h +++ b/src/s3c2410_power.h @@ -4,24 +4,24 @@ #ifndef _S3C2410_POWER_H #define _S3C2410_POWER_H 1 -#define CLKCON_SPI 0x00040000 -#define CLKCON_IIS 0x00020000 -#define CLKCON_IIC 0x00010000 -#define CLKCON_ADC 0x00008000 -#define CLKCON_RTC 0x00004000 -#define CLKCON_GPIO 0x00002000 -#define CLKCON_UART2 0x00001000 -#define CLKCON_UART1 0x00000800 -#define CLKCON_UART0 0x00000400 -#define CLKCON_SDI 0x00000200 -#define CLKCON_TIMER 0x00000100 -#define CLKCON_USBD 0x00000080 -#define CLKCON_USBH 0x00000040 -#define CLKCON_LCD 0x00000020 -#define CLKCON_NAND 0x00000010 -#define CLKCON_POWER_OFF 0x00000008 -#define CLKCON_IDLE 0x00000004 -#define CLKCON_SM_BIT 0x00000001 +#define CLKCON_SPI 0x00040000 +#define CLKCON_IIS 0x00020000 +#define CLKCON_IIC 0x00010000 +#define CLKCON_ADC 0x00008000 +#define CLKCON_RTC 0x00004000 +#define CLKCON_GPIO 0x00002000 +#define CLKCON_UART2 0x00001000 +#define CLKCON_UART1 0x00000800 +#define CLKCON_UART0 0x00000400 +#define CLKCON_SDI 0x00000200 +#define CLKCON_TIMER 0x00000100 +#define CLKCON_USBD 0x00000080 +#define CLKCON_USBH 0x00000040 +#define CLKCON_LCD 0x00000020 +#define CLKCON_NAND 0x00000010 +#define CLKCON_POWER_OFF 0x00000008 +#define CLKCON_IDLE 0x00000004 +#define CLKCON_SM_BIT 0x00000001 extern int s3c2410_idle; diff --git a/src/s3c2410_rtc.c b/src/s3c2410_rtc.c index a6c8918..2c4bf68 100644 --- a/src/s3c2410_rtc.c +++ b/src/s3c2410_rtc.c @@ -14,490 +14,435 @@ #include "s3c2410.h" #include "s3c2410_intc.h" - typedef struct { - uint32_t rtccon; - uint32_t ticnt; - uint32_t rtcalm; - uint32_t almsec; - uint32_t almmin; - uint32_t almhour; - uint32_t almdate; - uint32_t almmon; - uint32_t almyear; - uint32_t rtcrst; - uint32_t bcdsec; - uint32_t bcdmin; - uint32_t bcdhour; - uint32_t bcddate; - uint32_t bcdday; - uint32_t bcdmon; - uint32_t bcdyear; + uint32_t rtccon; + uint32_t ticnt; + uint32_t rtcalm; + uint32_t almsec; + uint32_t almmin; + uint32_t almhour; + uint32_t almdate; + uint32_t almmon; + uint32_t almyear; + uint32_t rtcrst; + uint32_t bcdsec; + uint32_t bcdmin; + uint32_t bcdhour; + uint32_t bcddate; + uint32_t bcdday; + uint32_t bcdmon; + uint32_t bcdyear; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; - x49gp_timer_t *tick_timer; - x49gp_timer_t *alarm_timer; - int64_t interval; /* us */ - int64_t expires; /* us */ + x49gp_t* x49gp; + x49gp_timer_t* tick_timer; + x49gp_timer_t* alarm_timer; + int64_t interval; /* us */ + int64_t expires; /* us */ } s3c2410_rtc_t; - -static int -s3c2410_rtc_data_init(s3c2410_rtc_t *rtc) +static int s3c2410_rtc_data_init( s3c2410_rtc_t* rtc ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(RTC, RTCCON, 0x00, rtc->rtccon), - S3C2410_OFFSET(RTC, TICNT, 0x00, rtc->ticnt), - S3C2410_OFFSET(RTC, RTCALM, 0x00, rtc->rtcalm), - S3C2410_OFFSET(RTC, ALMSEC, 0x00, rtc->almsec), - S3C2410_OFFSET(RTC, ALMMIN, 0x00, rtc->almmin), - S3C2410_OFFSET(RTC, ALMHOUR, 0x00, rtc->almhour), - S3C2410_OFFSET(RTC, ALMDATE, 0x01, rtc->almdate), - S3C2410_OFFSET(RTC, ALMMON, 0x01, rtc->almmon), - S3C2410_OFFSET(RTC, ALMYEAR, 0x00, rtc->almyear), - S3C2410_OFFSET(RTC, RTCRST, 0x00, rtc->rtcrst), - S3C2410_OFFSET(RTC, BCDSEC, 0, rtc->bcdsec), - S3C2410_OFFSET(RTC, BCDMIN, 0, rtc->bcdmin), - S3C2410_OFFSET(RTC, BCDHOUR, 0, rtc->bcdhour), - S3C2410_OFFSET(RTC, BCDDATE, 0, rtc->bcddate), - S3C2410_OFFSET(RTC, BCDDAY, 0, rtc->bcdday), - S3C2410_OFFSET(RTC, BCDMON, 0, rtc->bcdmon), - S3C2410_OFFSET(RTC, BCDYEAR, 0, rtc->bcdyear) - }; + s3c2410_offset_t regs[] = { S3C2410_OFFSET( RTC, RTCCON, 0x00, rtc->rtccon ), S3C2410_OFFSET( RTC, TICNT, 0x00, rtc->ticnt ), + S3C2410_OFFSET( RTC, RTCALM, 0x00, rtc->rtcalm ), S3C2410_OFFSET( RTC, ALMSEC, 0x00, rtc->almsec ), + S3C2410_OFFSET( RTC, ALMMIN, 0x00, rtc->almmin ), S3C2410_OFFSET( RTC, ALMHOUR, 0x00, rtc->almhour ), + S3C2410_OFFSET( RTC, ALMDATE, 0x01, rtc->almdate ), S3C2410_OFFSET( RTC, ALMMON, 0x01, rtc->almmon ), + S3C2410_OFFSET( RTC, ALMYEAR, 0x00, rtc->almyear ), S3C2410_OFFSET( RTC, RTCRST, 0x00, rtc->rtcrst ), + S3C2410_OFFSET( RTC, BCDSEC, 0, rtc->bcdsec ), S3C2410_OFFSET( RTC, BCDMIN, 0, rtc->bcdmin ), + S3C2410_OFFSET( RTC, BCDHOUR, 0, rtc->bcdhour ), S3C2410_OFFSET( RTC, BCDDATE, 0, rtc->bcddate ), + S3C2410_OFFSET( RTC, BCDDAY, 0, rtc->bcdday ), S3C2410_OFFSET( RTC, BCDMON, 0, rtc->bcdmon ), + S3C2410_OFFSET( RTC, BCDYEAR, 0, rtc->bcdyear ) }; - memset(rtc, 0, sizeof(s3c2410_rtc_t)); + memset( rtc, 0, sizeof( s3c2410_rtc_t ) ); - rtc->regs = malloc(sizeof(regs)); - if (NULL == rtc->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + rtc->regs = malloc( sizeof( regs ) ); + if ( NULL == rtc->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(rtc->regs, regs, sizeof(regs)); - rtc->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( rtc->regs, regs, sizeof( regs ) ); + rtc->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static __inline__ uint32_t bin2bcd(uint32_t bin) +static __inline__ uint32_t bin2bcd( uint32_t bin ) { return ( ( bin / 10 ) << 4 ) | ( bin % 10 ); } + +static __inline__ uint32_t bcd2bin( uint32_t bcd ) { return ( ( bcd >> 4 ) * 10 ) + ( bcd & 0x0f ); } + +static void s3c2410_rtc_timeout( void* user_data ) { - return ((bin / 10) << 4) | (bin % 10); + s3c2410_rtc_t* rtc = user_data; + x49gp_t* x49gp = rtc->x49gp; + int64_t now, us; + + if ( !( rtc->ticnt & 0x80 ) ) { + return; + } + +#ifdef DEBUG_S3C2410_RTC + printf( "RTC: assert TICK interrupt\n" ); +#endif + + s3c2410_intc_assert( x49gp, INT_TICK, 0 ); + + now = x49gp_get_clock(); + while ( rtc->expires <= now ) { + rtc->expires += rtc->interval; + } + + us = rtc->expires - now; + if ( us < 1000 ) + us = 1000; + +#ifdef DEBUG_S3C2410_RTC + printf( "RTC: restart TICK timer (%llu us)\n", ( unsigned long long )us ); +#endif + + x49gp_mod_timer( rtc->tick_timer, rtc->expires ); } -static __inline__ uint32_t bcd2bin(uint32_t bcd) +static int s3c2410_rtc_set_ticnt( s3c2410_rtc_t* rtc ) { - return ((bcd >> 4) * 10) + (bcd & 0x0f); + int64_t now, us; + + if ( x49gp_timer_pending( rtc->tick_timer ) ) { + x49gp_del_timer( rtc->tick_timer ); +#ifdef DEBUG_S3C2410_RTC + printf( "RTC: stop TICK timer\n" ); +#endif + } + + if ( !( rtc->ticnt & 0x80 ) ) { + return 0; + } + + us = ( ( ( rtc->ticnt & 0x7f ) + 1 ) * 1000000 ) / 128; + + rtc->interval = us; + if ( rtc->interval < 1000 ) + rtc->interval = 1000; + + now = x49gp_get_clock(); + rtc->expires = now + rtc->interval; + + us = rtc->expires - now; + if ( us < 1000 ) + us = 1000; + +#ifdef DEBUG_S3C2410_RTC + printf( "RTC: start TICK timer (%lld us)\n", ( unsigned long long )us ); +#endif + + x49gp_mod_timer( rtc->tick_timer, rtc->expires ); + return 0; } -static void -s3c2410_rtc_timeout(void * user_data) +static void s3c2410_rtc_alarm( void* user_data ) { - s3c2410_rtc_t *rtc = user_data; - x49gp_t *x49gp = rtc->x49gp; - int64_t now, us; + s3c2410_rtc_t* rtc = user_data; + x49gp_t* x49gp = rtc->x49gp; + struct tm* tm; + struct timeval tv; + int64_t now, us; + int match = 1; - if (!(rtc->ticnt & 0x80)) { - return; - } + if ( !( rtc->rtcalm & 0x40 ) ) { + return; + } + + gettimeofday( &tv, NULL ); + tm = localtime( &tv.tv_sec ); + + now = x49gp_get_clock(); + us = 1000000LL - tv.tv_usec; + + if ( match && ( rtc->rtcalm & 0x01 ) ) { + if ( tm->tm_sec != bcd2bin( rtc->almsec ) ) + match = 0; + } + if ( match && ( rtc->rtcalm & 0x02 ) ) { + if ( tm->tm_min != bcd2bin( rtc->almmin ) ) + match = 0; + } + if ( match && ( rtc->rtcalm & 0x04 ) ) { + if ( tm->tm_hour != bcd2bin( rtc->almhour ) ) + match = 0; + } + if ( match && ( rtc->rtcalm & 0x08 ) ) { + if ( tm->tm_mday != bcd2bin( rtc->almdate ) ) + match = 0; + } + if ( match && ( rtc->rtcalm & 0x10 ) ) { + if ( ( tm->tm_mon + 1 ) != bcd2bin( rtc->almmon ) ) + match = 0; + } + if ( match && ( rtc->rtcalm & 0x20 ) ) { + if ( ( tm->tm_year % 100 ) != bcd2bin( rtc->almyear ) ) + match = 0; + } + + if ( match ) { +#ifdef DEBUG_S3C2410_RTC + printf( "RTC: assert ALARM interrupt\n" ); +#endif + s3c2410_intc_assert( x49gp, INT_RTC, 0 ); + } #ifdef DEBUG_S3C2410_RTC - printf("RTC: assert TICK interrupt\n"); + printf( "RTC: reload ALARM timer (%lld us)\n", ( unsigned long long )us ); #endif - s3c2410_intc_assert(x49gp, INT_TICK, 0); - - now = x49gp_get_clock(); - while (rtc->expires <= now) { - rtc->expires += rtc->interval; - } - - us = rtc->expires - now; - if (us < 1000) - us = 1000; - -#ifdef DEBUG_S3C2410_RTC - printf("RTC: restart TICK timer (%llu us)\n", (unsigned long long) us); -#endif - - x49gp_mod_timer(rtc->tick_timer, rtc->expires); + x49gp_mod_timer( rtc->alarm_timer, now + us ); } -static int -s3c2410_rtc_set_ticnt(s3c2410_rtc_t *rtc) +static int s3c2410_rtc_set_rtcalm( s3c2410_rtc_t* rtc ) { - int64_t now, us; + struct timeval tv; + int64_t now, us; - if (x49gp_timer_pending(rtc->tick_timer)) { - x49gp_del_timer(rtc->tick_timer); -#ifdef DEBUG_S3C2410_RTC - printf("RTC: stop TICK timer\n"); -#endif - } + if ( !( rtc->rtcalm & 0x40 ) ) { + x49gp_del_timer( rtc->alarm_timer ); + return 0; + } - if (!(rtc->ticnt & 0x80)) { - return 0; - } + gettimeofday( &tv, NULL ); - us = (((rtc->ticnt & 0x7f) + 1) * 1000000) / 128; - - rtc->interval = us; - if (rtc->interval < 1000) - rtc->interval = 1000; - - now = x49gp_get_clock(); - rtc->expires = now + rtc->interval; - - us = rtc->expires - now; - if (us < 1000) - us = 1000; + now = x49gp_get_clock(); + us = 1000000LL - tv.tv_usec; #ifdef DEBUG_S3C2410_RTC - printf("RTC: start TICK timer (%lld us)\n", (unsigned long long) us); + printf( "RTC: start ALARM timer (%lld us)\n", ( unsigned long long )us ); #endif - x49gp_mod_timer(rtc->tick_timer, rtc->expires); - return 0; + x49gp_mod_timer( rtc->alarm_timer, now + us ); + return 0; } -static void -s3c2410_rtc_alarm(void * user_data) +static uint32_t s3c2410_rtc_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_rtc_t *rtc = user_data; - x49gp_t *x49gp = rtc->x49gp; - struct tm *tm; - struct timeval tv; - int64_t now, us; - int match = 1; + s3c2410_rtc_t* rtc = opaque; + s3c2410_offset_t* reg; - if (!(rtc->rtcalm & 0x40)) { - return; - } + if ( !S3C2410_OFFSET_OK( rtc, offset ) ) { + return ~( 0 ); + } - gettimeofday(&tv, NULL); - tm = localtime(&tv.tv_sec); + reg = S3C2410_OFFSET_ENTRY( rtc, offset ); - now = x49gp_get_clock(); - us = 1000000LL - tv.tv_usec; + if ( S3C2410_RTC_BCDSEC <= offset && offset <= S3C2410_RTC_BCDYEAR ) { + struct tm* tm; + struct timeval tv; - if (match && (rtc->rtcalm & 0x01)) { - if (tm->tm_sec != bcd2bin(rtc->almsec)) - match = 0; - } - if (match && (rtc->rtcalm & 0x02)) { - if (tm->tm_min != bcd2bin(rtc->almmin)) - match = 0; - } - if (match && (rtc->rtcalm & 0x04)) { - if (tm->tm_hour != bcd2bin(rtc->almhour)) - match = 0; - } - if (match && (rtc->rtcalm & 0x08)) { - if (tm->tm_mday != bcd2bin(rtc->almdate)) - match = 0; - } - if (match && (rtc->rtcalm & 0x10)) { - if ((tm->tm_mon + 1) != bcd2bin(rtc->almmon)) - match = 0; - } - if (match && (rtc->rtcalm & 0x20)) { - if ((tm->tm_year % 100) != bcd2bin(rtc->almyear)) - match = 0; - } + gettimeofday( &tv, NULL ); + tm = localtime( &tv.tv_sec ); - if (match) { -#ifdef DEBUG_S3C2410_RTC - printf("RTC: assert ALARM interrupt\n"); -#endif - s3c2410_intc_assert(x49gp, INT_RTC, 0); - } + switch ( offset ) { + case S3C2410_RTC_BCDSEC: + *( reg->datap ) = bin2bcd( tm->tm_sec ); + break; + case S3C2410_RTC_BCDMIN: + *( reg->datap ) = bin2bcd( tm->tm_min ); + break; + case S3C2410_RTC_BCDHOUR: + *( reg->datap ) = bin2bcd( tm->tm_hour ); + break; + case S3C2410_RTC_BCDDATE: + *( reg->datap ) = bin2bcd( tm->tm_mday ); + break; + case S3C2410_RTC_BCDDAY: + *( reg->datap ) = bin2bcd( tm->tm_wday + 1 ); + break; + case S3C2410_RTC_BCDMON: + *( reg->datap ) = bin2bcd( tm->tm_mon + 1 ); + break; + case S3C2410_RTC_BCDYEAR: + *( reg->datap ) = bin2bcd( tm->tm_year % 100 ); + break; + } + } #ifdef DEBUG_S3C2410_RTC - printf("RTC: reload ALARM timer (%lld us)\n", (unsigned long long) us); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-rtc", S3C2410_RTC_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - x49gp_mod_timer(rtc->alarm_timer, now + us); + return *( reg->datap ); } -static int -s3c2410_rtc_set_rtcalm(s3c2410_rtc_t *rtc) +static void s3c2410_rtc_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - struct timeval tv; - int64_t now, us; + s3c2410_rtc_t* rtc = opaque; + s3c2410_offset_t* reg; - if (!(rtc->rtcalm & 0x40)) { - x49gp_del_timer(rtc->alarm_timer); - return 0; - } + if ( !S3C2410_OFFSET_OK( rtc, offset ) ) { + return; + } - gettimeofday(&tv, NULL); - - now = x49gp_get_clock(); - us = 1000000LL - tv.tv_usec; + reg = S3C2410_OFFSET_ENTRY( rtc, offset ); #ifdef DEBUG_S3C2410_RTC - printf("RTC: start ALARM timer (%lld us)\n", (unsigned long long) us); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-rtc", S3C2410_RTC_BASE, reg->name, ( unsigned long )offset, data ); #endif - x49gp_mod_timer(rtc->alarm_timer, now + us); - return 0; + switch ( offset ) { + case S3C2410_RTC_TICNT: + *( reg->datap ) = data; + s3c2410_rtc_set_ticnt( rtc ); + break; + case S3C2410_RTC_RTCALM: + *( reg->datap ) = data; + s3c2410_rtc_set_rtcalm( rtc ); + break; + default: + *( reg->datap ) = data; + break; + } } -static uint32_t -s3c2410_rtc_read(void *opaque, target_phys_addr_t offset) +static int s3c2410_rtc_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_rtc_t *rtc = opaque; - s3c2410_offset_t *reg; - - if (! S3C2410_OFFSET_OK(rtc, offset)) { - return ~(0); - } - - reg = S3C2410_OFFSET_ENTRY(rtc, offset); - - if (S3C2410_RTC_BCDSEC <= offset && offset <= S3C2410_RTC_BCDYEAR) { - struct tm *tm; - struct timeval tv; - - gettimeofday(&tv, NULL); - tm = localtime(&tv.tv_sec); - - switch (offset) { - case S3C2410_RTC_BCDSEC: - *(reg->datap) = bin2bcd(tm->tm_sec); - break; - case S3C2410_RTC_BCDMIN: - *(reg->datap) = bin2bcd(tm->tm_min); - break; - case S3C2410_RTC_BCDHOUR: - *(reg->datap) = bin2bcd(tm->tm_hour); - break; - case S3C2410_RTC_BCDDATE: - *(reg->datap) = bin2bcd(tm->tm_mday); - break; - case S3C2410_RTC_BCDDAY: - *(reg->datap) = bin2bcd(tm->tm_wday + 1); - break; - case S3C2410_RTC_BCDMON: - *(reg->datap) = bin2bcd(tm->tm_mon + 1); - break; - case S3C2410_RTC_BCDYEAR: - *(reg->datap) = bin2bcd(tm->tm_year % 100); - break; - } - } - -#ifdef DEBUG_S3C2410_RTC - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-rtc", S3C2410_RTC_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); -#endif - - return *(reg->datap); -} - -static void -s3c2410_rtc_write(void *opaque, target_phys_addr_t offset, uint32_t data) -{ - s3c2410_rtc_t *rtc = opaque; - s3c2410_offset_t *reg; - - if (! S3C2410_OFFSET_OK(rtc, offset)) { - return; - } - - reg = S3C2410_OFFSET_ENTRY(rtc, offset); - -#ifdef DEBUG_S3C2410_RTC - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-rtc", S3C2410_RTC_BASE, - reg->name, (unsigned long) offset, data); -#endif - - switch (offset) { - case S3C2410_RTC_TICNT: - *(reg->datap) = data; - s3c2410_rtc_set_ticnt(rtc); - break; - case S3C2410_RTC_RTCALM: - *(reg->datap) = data; - s3c2410_rtc_set_rtcalm(rtc); - break; - default: - *(reg->datap) = data; - break; - } -} - -static int -s3c2410_rtc_load(x49gp_module_t *module, GKeyFile *key) -{ - s3c2410_rtc_t *rtc = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_rtc_t* rtc = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < rtc->nr_regs; i++) { - reg = &rtc->regs[i]; + for ( i = 0; i < rtc->nr_regs; i++ ) { + reg = &rtc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - s3c2410_rtc_set_ticnt(rtc); - s3c2410_rtc_set_rtcalm(rtc); + s3c2410_rtc_set_ticnt( rtc ); + s3c2410_rtc_set_rtcalm( rtc ); - return error; + return error; } -static int -s3c2410_rtc_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_rtc_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_rtc_t *rtc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_rtc_t* rtc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < rtc->nr_regs; i++) { - reg = &rtc->regs[i]; + for ( i = 0; i < rtc->nr_regs; i++ ) { + reg = &rtc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_rtc_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_rtc_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_rtc_t *rtc = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_rtc_t* rtc = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < (S3C2410_RTC_RTCALM >> 2); i++) { - reg = &rtc->regs[i]; + for ( i = 0; i < ( S3C2410_RTC_RTCALM >> 2 ); i++ ) { + reg = &rtc->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - s3c2410_rtc_set_ticnt(rtc); + s3c2410_rtc_set_ticnt( rtc ); - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_rtc_readfn[] = -{ - s3c2410_rtc_read, - s3c2410_rtc_read, - s3c2410_rtc_read -}; +static CPUReadMemoryFunc* s3c2410_rtc_readfn[] = { s3c2410_rtc_read, s3c2410_rtc_read, s3c2410_rtc_read }; -static CPUWriteMemoryFunc *s3c2410_rtc_writefn[] = -{ - s3c2410_rtc_write, - s3c2410_rtc_write, - s3c2410_rtc_write -}; +static CPUWriteMemoryFunc* s3c2410_rtc_writefn[] = { s3c2410_rtc_write, s3c2410_rtc_write, s3c2410_rtc_write }; -static int -s3c2410_rtc_init(x49gp_module_t *module) +static int s3c2410_rtc_init( x49gp_module_t* module ) { - s3c2410_rtc_t *rtc; - int iotype; + s3c2410_rtc_t* rtc; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - rtc = malloc(sizeof(s3c2410_rtc_t)); - if (NULL == rtc) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->x49gp->progname, __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_rtc_data_init(rtc)) { - free(rtc); - return -ENOMEM; - } + rtc = malloc( sizeof( s3c2410_rtc_t ) ); + if ( NULL == rtc ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->x49gp->progname, __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_rtc_data_init( rtc ) ) { + free( rtc ); + return -ENOMEM; + } - module->user_data = rtc; - rtc->x49gp = module->x49gp; + module->user_data = rtc; + rtc->x49gp = module->x49gp; - rtc->tick_timer = x49gp_new_timer(X49GP_TIMER_REALTIME, - s3c2410_rtc_timeout, rtc); - rtc->alarm_timer = x49gp_new_timer(X49GP_TIMER_REALTIME, - s3c2410_rtc_alarm, rtc); + rtc->tick_timer = x49gp_new_timer( X49GP_TIMER_REALTIME, s3c2410_rtc_timeout, rtc ); + rtc->alarm_timer = x49gp_new_timer( X49GP_TIMER_REALTIME, s3c2410_rtc_alarm, rtc ); - iotype = cpu_register_io_memory(s3c2410_rtc_readfn, - s3c2410_rtc_writefn, rtc); + iotype = cpu_register_io_memory( s3c2410_rtc_readfn, s3c2410_rtc_writefn, rtc ); #ifdef DEBUG_S3C2410_RTC - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_RTC_BASE, S3C2410_MAP_SIZE, iotype); + cpu_register_physical_memory( S3C2410_RTC_BASE, S3C2410_MAP_SIZE, iotype ); - return 0; + return 0; } -static int -s3c2410_rtc_exit(x49gp_module_t *module) +static int s3c2410_rtc_exit( x49gp_module_t* module ) { - s3c2410_rtc_t *rtc; + s3c2410_rtc_t* rtc; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - rtc = module->user_data; - if (rtc->regs) - free(rtc->regs); - free(rtc); - } + if ( module->user_data ) { + rtc = module->user_data; + if ( rtc->regs ) + free( rtc->regs ); + free( rtc ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_rtc_init(x49gp_t *x49gp) +int x49gp_s3c2410_rtc_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-rtc", - s3c2410_rtc_init, - s3c2410_rtc_exit, - s3c2410_rtc_reset, - s3c2410_rtc_load, - s3c2410_rtc_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-rtc", s3c2410_rtc_init, s3c2410_rtc_exit, s3c2410_rtc_reset, s3c2410_rtc_load, s3c2410_rtc_save, + NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_sdi.c b/src/s3c2410_sdi.c index 7f7e3dc..a0881f1 100644 --- a/src/s3c2410_sdi.c +++ b/src/s3c2410_sdi.c @@ -15,844 +15,796 @@ #include "block.h" -//#define DEBUG_S3C2410_SDI 1 - +// #define DEBUG_S3C2410_SDI 1 typedef struct { - uint32_t sdicon; - uint32_t sdipre; - uint32_t sdicarg; - uint32_t sdiccon; - uint32_t sdicsta; - uint32_t sdirsp0; - uint32_t sdirsp1; - uint32_t sdirsp2; - uint32_t sdirsp3; - uint32_t sdidtimer; - uint32_t sdibsize; - uint32_t sdidcon; - uint32_t sdidcnt; - uint32_t sdidsta; - uint32_t sdifsta; - uint32_t sdidat; - uint32_t sdiimsk; + uint32_t sdicon; + uint32_t sdipre; + uint32_t sdicarg; + uint32_t sdiccon; + uint32_t sdicsta; + uint32_t sdirsp0; + uint32_t sdirsp1; + uint32_t sdirsp2; + uint32_t sdirsp3; + uint32_t sdidtimer; + uint32_t sdibsize; + uint32_t sdidcon; + uint32_t sdidcnt; + uint32_t sdidsta; + uint32_t sdifsta; + uint32_t sdidat; + uint32_t sdiimsk; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; - char *filename; - BlockDriverState *bs; - int fd; + char* filename; + BlockDriverState* bs; + int fd; - unsigned char *read_data; - uint32_t nr_read_data; - uint32_t read_offset; - unsigned int read_index; + unsigned char* read_data; + uint32_t nr_read_data; + uint32_t read_offset; + unsigned int read_index; - unsigned char *write_data; - uint32_t nr_write_data; - uint32_t write_offset; - unsigned int write_index; + unsigned char* write_data; + uint32_t nr_write_data; + uint32_t write_offset; + unsigned int write_index; - int multiple_block; - int acmd; - int card_state; + int multiple_block; + int acmd; + int card_state; } s3c2410_sdi_t; - -static int -s3c2410_sdi_data_init(s3c2410_sdi_t *sdi) +static int s3c2410_sdi_data_init( s3c2410_sdi_t* sdi ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(SDI, SDICON, 0x00000000, sdi->sdicon), - S3C2410_OFFSET(SDI, SDIPRE, 0x00000000, sdi->sdipre), - S3C2410_OFFSET(SDI, SDICARG, 0x00000000, sdi->sdicarg), - S3C2410_OFFSET(SDI, SDICCON, 0x00000000, sdi->sdiccon), - S3C2410_OFFSET(SDI, SDICSTA, 0x00000000, sdi->sdicsta), - S3C2410_OFFSET(SDI, SDIRSP0, 0x00000000, sdi->sdirsp0), - S3C2410_OFFSET(SDI, SDIRSP1, 0x00000000, sdi->sdirsp1), - S3C2410_OFFSET(SDI, SDIRSP2, 0x00000000, sdi->sdirsp2), - S3C2410_OFFSET(SDI, SDIRSP3, 0x00000000, sdi->sdirsp3), - S3C2410_OFFSET(SDI, SDIDTIMER, 0x00002000, sdi->sdidtimer), - S3C2410_OFFSET(SDI, SDIBSIZE, 0x00000000, sdi->sdibsize), - S3C2410_OFFSET(SDI, SDIDCON, 0x00000000, sdi->sdidcon), - S3C2410_OFFSET(SDI, SDIDCNT, 0x00000000, sdi->sdidcnt), - S3C2410_OFFSET(SDI, SDIDSTA, 0x00000000, sdi->sdidsta), - S3C2410_OFFSET(SDI, SDIFSTA, 0x00000000, sdi->sdifsta), - S3C2410_OFFSET(SDI, SDIDAT, 0x00000000, sdi->sdidat), - S3C2410_OFFSET(SDI, SDIIMSK, 0x00000000, sdi->sdiimsk) - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( SDI, SDICON, 0x00000000, sdi->sdicon ), S3C2410_OFFSET( SDI, SDIPRE, 0x00000000, sdi->sdipre ), + S3C2410_OFFSET( SDI, SDICARG, 0x00000000, sdi->sdicarg ), S3C2410_OFFSET( SDI, SDICCON, 0x00000000, sdi->sdiccon ), + S3C2410_OFFSET( SDI, SDICSTA, 0x00000000, sdi->sdicsta ), S3C2410_OFFSET( SDI, SDIRSP0, 0x00000000, sdi->sdirsp0 ), + S3C2410_OFFSET( SDI, SDIRSP1, 0x00000000, sdi->sdirsp1 ), S3C2410_OFFSET( SDI, SDIRSP2, 0x00000000, sdi->sdirsp2 ), + S3C2410_OFFSET( SDI, SDIRSP3, 0x00000000, sdi->sdirsp3 ), S3C2410_OFFSET( SDI, SDIDTIMER, 0x00002000, sdi->sdidtimer ), + S3C2410_OFFSET( SDI, SDIBSIZE, 0x00000000, sdi->sdibsize ), S3C2410_OFFSET( SDI, SDIDCON, 0x00000000, sdi->sdidcon ), + S3C2410_OFFSET( SDI, SDIDCNT, 0x00000000, sdi->sdidcnt ), S3C2410_OFFSET( SDI, SDIDSTA, 0x00000000, sdi->sdidsta ), + S3C2410_OFFSET( SDI, SDIFSTA, 0x00000000, sdi->sdifsta ), S3C2410_OFFSET( SDI, SDIDAT, 0x00000000, sdi->sdidat ), + S3C2410_OFFSET( SDI, SDIIMSK, 0x00000000, sdi->sdiimsk ) }; - memset(sdi, 0, sizeof(s3c2410_sdi_t)); + memset( sdi, 0, sizeof( s3c2410_sdi_t ) ); - sdi->regs = malloc(sizeof(regs)); - if (NULL == sdi->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + sdi->regs = malloc( sizeof( regs ) ); + if ( NULL == sdi->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(sdi->regs, regs, sizeof(regs)); - sdi->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( sdi->regs, regs, sizeof( regs ) ); + sdi->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static int -sdcard_read(s3c2410_sdi_t *sdi) +static int sdcard_read( s3c2410_sdi_t* sdi ) { - uint32_t offset; - uint32_t size; - int error; + uint32_t offset; + uint32_t size; + int error; - offset = sdi->read_offset; - size = sdi->nr_read_data; + offset = sdi->read_offset; + size = sdi->nr_read_data; #ifdef DEBUG_S3C2410_SDI - printf("SDI: card read %4u at %08x\n", size, offset); + printf( "SDI: card read %4u at %08x\n", size, offset ); #endif - sdi->nr_read_data = 0; - sdi->read_index = 0; + sdi->nr_read_data = 0; + sdi->read_index = 0; - if ((sdi->fd < 0) && (sdi->bs == NULL)) { - return -ENODEV; - } + if ( ( sdi->fd < 0 ) && ( sdi->bs == NULL ) ) { + return -ENODEV; + } - sdi->read_data = malloc(size); - if (NULL == sdi->read_data) { - return -ENOMEM; - } + sdi->read_data = malloc( size ); + if ( NULL == sdi->read_data ) { + return -ENOMEM; + } - if (sdi->bs) { - sdi->nr_read_data = bdrv_pread(sdi->bs, offset, sdi->read_data, size); - } else { - if (sdi->read_offset != lseek(sdi->fd, offset, SEEK_SET)) { - error = errno; - sdi->nr_read_data = 0; - free(sdi->read_data); - sdi->read_data = NULL; - return -error; - } + if ( sdi->bs ) { + sdi->nr_read_data = bdrv_pread( sdi->bs, offset, sdi->read_data, size ); + } else { + if ( sdi->read_offset != lseek( sdi->fd, offset, SEEK_SET ) ) { + error = errno; + sdi->nr_read_data = 0; + free( sdi->read_data ); + sdi->read_data = NULL; + return -error; + } - sdi->nr_read_data = read(sdi->fd, sdi->read_data, size); - } + sdi->nr_read_data = read( sdi->fd, sdi->read_data, size ); + } - if (sdi->nr_read_data != size) { - error = errno; - sdi->nr_read_data = 0; - free(sdi->read_data); - sdi->read_data = NULL; - return -error; - } + if ( sdi->nr_read_data != size ) { + error = errno; + sdi->nr_read_data = 0; + free( sdi->read_data ); + sdi->read_data = NULL; + return -error; + } - return 0; + return 0; } -static int -sdcard_write_prepare(s3c2410_sdi_t *sdi) +static int sdcard_write_prepare( s3c2410_sdi_t* sdi ) { - sdi->write_index = 0; + sdi->write_index = 0; - if ((sdi->fd < 0) && (sdi->bs == NULL)) { - sdi->nr_write_data = 0; - return -ENODEV; - } + if ( ( sdi->fd < 0 ) && ( sdi->bs == NULL ) ) { + sdi->nr_write_data = 0; + return -ENODEV; + } - sdi->write_data = malloc(sdi->nr_write_data); - if (NULL == sdi->write_data) { - sdi->nr_write_data = 0; - return -ENOMEM; - } + sdi->write_data = malloc( sdi->nr_write_data ); + if ( NULL == sdi->write_data ) { + sdi->nr_write_data = 0; + return -ENOMEM; + } - return 0; + return 0; } -static int -sdcard_write(s3c2410_sdi_t *sdi) +static int sdcard_write( s3c2410_sdi_t* sdi ) { - uint32_t offset; - uint32_t size; - int error; + uint32_t offset; + uint32_t size; + int error; - offset = sdi->write_offset; - size = sdi->nr_write_data; + offset = sdi->write_offset; + size = sdi->nr_write_data; #ifdef DEBUG_S3C2410_SDI - printf("SDI: card write %4u at %08x\n", size, offset); + printf( "SDI: card write %4u at %08x\n", size, offset ); #endif - sdi->write_index = 0; + sdi->write_index = 0; - if (sdi->bs) { - error = bdrv_pwrite(sdi->bs, offset, sdi->write_data, size); - } else { - if (sdi->fd < 0) { - free(sdi->write_data); - sdi->write_data = NULL; - return -ENODEV; - } + if ( sdi->bs ) { + error = bdrv_pwrite( sdi->bs, offset, sdi->write_data, size ); + } else { + if ( sdi->fd < 0 ) { + free( sdi->write_data ); + sdi->write_data = NULL; + return -ENODEV; + } - if (sdi->write_offset != lseek(sdi->fd, offset, SEEK_SET)) { - error = errno; - free(sdi->write_data); - sdi->write_data = NULL; - return -error; - } + if ( sdi->write_offset != lseek( sdi->fd, offset, SEEK_SET ) ) { + error = errno; + free( sdi->write_data ); + sdi->write_data = NULL; + return -error; + } - error = write(sdi->fd, sdi->write_data, size); - } + error = write( sdi->fd, sdi->write_data, size ); + } - if (error != size) { - error = errno; - free(sdi->write_data); - sdi->write_data = NULL; - return -error; - } + if ( error != size ) { + error = errno; + free( sdi->write_data ); + sdi->write_data = NULL; + return -error; + } - free(sdi->write_data); - sdi->write_data = NULL; - return 0; + free( sdi->write_data ); + sdi->write_data = NULL; + return 0; } -static uint32_t -s3c2410_sdi_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_sdi_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_sdi_t *sdi = opaque; - s3c2410_offset_t *reg; - unsigned int read_avail, write_avail; + s3c2410_sdi_t* sdi = opaque; + s3c2410_offset_t* reg; + unsigned int read_avail, write_avail; - if (! S3C2410_OFFSET_OK(sdi, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( sdi, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(sdi, offset); + reg = S3C2410_OFFSET_ENTRY( sdi, offset ); - switch (offset) { - case S3C2410_SDI_SDIDAT: - if( (sdi->sdidcon&0x3000)==0x2000) { - read_avail = sdi->nr_read_data - sdi->read_index; - if (read_avail > 0) { - *(reg->datap) = sdi->read_data[sdi->read_index++] << ((sdi->sdicon&0x10)? 24:0); - if ((sdi->nr_read_data - sdi->read_index) > 0) { - *(reg->datap) |= sdi->read_data[sdi->read_index++] << ((sdi->sdicon&0x10)? 16:8); - } - if ((sdi->nr_read_data - sdi->read_index) > 0) { - *(reg->datap) |= sdi->read_data[sdi->read_index++] << ((sdi->sdicon&0x10)? 8:16); - } - if ((sdi->nr_read_data - sdi->read_index) > 0) { - *(reg->datap) |= sdi->read_data[sdi->read_index++] << ((sdi->sdicon&0x10)? 0:24); - } + switch ( offset ) { + case S3C2410_SDI_SDIDAT: + if ( ( sdi->sdidcon & 0x3000 ) == 0x2000 ) { + read_avail = sdi->nr_read_data - sdi->read_index; + if ( read_avail > 0 ) { + *( reg->datap ) = sdi->read_data[ sdi->read_index++ ] << ( ( sdi->sdicon & 0x10 ) ? 24 : 0 ); + if ( ( sdi->nr_read_data - sdi->read_index ) > 0 ) { + *( reg->datap ) |= sdi->read_data[ sdi->read_index++ ] << ( ( sdi->sdicon & 0x10 ) ? 16 : 8 ); + } + if ( ( sdi->nr_read_data - sdi->read_index ) > 0 ) { + *( reg->datap ) |= sdi->read_data[ sdi->read_index++ ] << ( ( sdi->sdicon & 0x10 ) ? 8 : 16 ); + } + if ( ( sdi->nr_read_data - sdi->read_index ) > 0 ) { + *( reg->datap ) |= sdi->read_data[ sdi->read_index++ ] << ( ( sdi->sdicon & 0x10 ) ? 0 : 24 ); + } - if (sdi->read_index >= sdi->nr_read_data) { - sdi->read_index = 0; - free(sdi->read_data); - sdi->read_data = NULL; - if(sdi->multiple_block) --sdi->multiple_block; - if (sdi->multiple_block) { - sdi->read_offset += sdi->nr_read_data; + if ( sdi->read_index >= sdi->nr_read_data ) { + sdi->read_index = 0; + free( sdi->read_data ); + sdi->read_data = NULL; + if ( sdi->multiple_block ) + --sdi->multiple_block; + if ( sdi->multiple_block ) { + sdi->read_offset += sdi->nr_read_data; - sdcard_read(sdi); + sdcard_read( sdi ); - sdi->sdidsta |= (1 << 9) | (1 << 4); - } else { - sdi->nr_read_data = 0; - sdi->sdidcon&=~0x3000; - } - } + sdi->sdidsta |= ( 1 << 9 ) | ( 1 << 4 ); + } else { + sdi->nr_read_data = 0; + sdi->sdidcon &= ~0x3000; + } + } - read_avail = sdi->nr_read_data - sdi->read_index; + read_avail = sdi->nr_read_data - sdi->read_index; - sdi->sdidcnt = read_avail; - } - else *(reg->datap)=0; - } - break; + sdi->sdidcnt = read_avail; + } else + *( reg->datap ) = 0; + } + break; - case S3C2410_SDI_SDIFSTA: - *(reg->datap) = (1 << 13) | (1 << 10); + case S3C2410_SDI_SDIFSTA: + *( reg->datap ) = ( 1 << 13 ) | ( 1 << 10 ); - read_avail = sdi->nr_read_data - sdi->read_index; - if (read_avail > 63) - *(reg->datap) |= (1 << 12) | (1 << 8) | (1 << 7) | 0x40; - else if (read_avail > 31) - *(reg->datap) |= (1 << 12) | (1 << 9) | (1 << 7) | read_avail; - else - *(reg->datap) |= (1 << 12) | (1 << 9) | read_avail; - break; + read_avail = sdi->nr_read_data - sdi->read_index; + if ( read_avail > 63 ) + *( reg->datap ) |= ( 1 << 12 ) | ( 1 << 8 ) | ( 1 << 7 ) | 0x40; + else if ( read_avail > 31 ) + *( reg->datap ) |= ( 1 << 12 ) | ( 1 << 9 ) | ( 1 << 7 ) | read_avail; + else + *( reg->datap ) |= ( 1 << 12 ) | ( 1 << 9 ) | read_avail; + break; - case S3C2410_SDI_SDIDSTA: - *(reg->datap) &= ~(0x03); - if( (sdi->sdidcon&0x3000)==0x1000) { - *(reg->datap) |= (1 << 3); - sdi->sdidcon&=~0x3000; // BACK TO READY STATE - } - read_avail = sdi->nr_read_data - sdi->read_index; - if ( ((sdi->sdidcon&0x3000)==0x2000) && read_avail) { - *(reg->datap) |= (1 << 0); - } + case S3C2410_SDI_SDIDSTA: + *( reg->datap ) &= ~( 0x03 ); + if ( ( sdi->sdidcon & 0x3000 ) == 0x1000 ) { + *( reg->datap ) |= ( 1 << 3 ); + sdi->sdidcon &= ~0x3000; // BACK TO READY STATE + } + read_avail = sdi->nr_read_data - sdi->read_index; + if ( ( ( sdi->sdidcon & 0x3000 ) == 0x2000 ) && read_avail ) { + *( reg->datap ) |= ( 1 << 0 ); + } - write_avail = sdi->nr_write_data - sdi->write_index; - if ( ((sdi->sdidcon&0x3000)==0x3000) && write_avail) { - *(reg->datap) |= (1 << 1); - } - } + write_avail = sdi->nr_write_data - sdi->write_index; + if ( ( ( sdi->sdidcon & 0x3000 ) == 0x3000 ) && write_avail ) { + *( reg->datap ) |= ( 1 << 1 ); + } + } #ifdef DEBUG_S3C2410_SDI - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-sdi", S3C2410_SDI_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-sdi", S3C2410_SDI_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_sdi_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_sdi_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_sdi_t *sdi = opaque; - s3c2410_offset_t *reg; - unsigned int read_avail, write_avail; + s3c2410_sdi_t* sdi = opaque; + s3c2410_offset_t* reg; + unsigned int read_avail, write_avail; - if (! S3C2410_OFFSET_OK(sdi, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( sdi, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(sdi, offset); + reg = S3C2410_OFFSET_ENTRY( sdi, offset ); #ifdef DEBUG_S3C2410_SDI - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-sdi", S3C2410_SDI_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-sdi", S3C2410_SDI_BASE, reg->name, ( unsigned long )offset, data ); #endif - switch (offset) { - case S3C2410_SDI_SDICON: - *(reg->datap) = data & ~(2); - break; + switch ( offset ) { + case S3C2410_SDI_SDICON: + *( reg->datap ) = data & ~( 2 ); + break; - case S3C2410_SDI_SDICCON: - *(reg->datap) = data; + case S3C2410_SDI_SDICCON: + *( reg->datap ) = data; #ifdef DEBUG_S3C2410_SDI - printf("SDI: cmd %02u, start %u, %s response %u, data %u, abort %u\n", - data & 0x3f, (data >> 8) & 1, - (data >> 10) & 1 ? "long" : "short", - (data >> 9) & 1, - (data >> 11) & 1, (data >> 12) & 1); + printf( "SDI: cmd %02u, start %u, %s response %u, data %u, abort %u\n", data & 0x3f, ( data >> 8 ) & 1, + ( data >> 10 ) & 1 ? "long" : "short", ( data >> 9 ) & 1, ( data >> 11 ) & 1, ( data >> 12 ) & 1 ); #endif - if (data & (1 << 8)) { - sdi->sdicsta |= (1 << 11); - } else { - sdi->sdicsta = 0; - sdi->sdirsp0 = 0; - sdi->sdirsp1 = 0; - sdi->sdirsp2 = 0; - sdi->sdirsp3 = 0; - data = 0; - } + if ( data & ( 1 << 8 ) ) { + sdi->sdicsta |= ( 1 << 11 ); + } else { + sdi->sdicsta = 0; + sdi->sdirsp0 = 0; + sdi->sdirsp1 = 0; + sdi->sdirsp2 = 0; + sdi->sdirsp3 = 0; + data = 0; + } - sdi->sdicsta &= ~(1 << 12); - sdi->sdicsta &= ~(0xff); + sdi->sdicsta &= ~( 1 << 12 ); + sdi->sdicsta &= ~( 0xff ); - if (data & (1 << 9)) { - sdi->sdicsta |= (1 << 9); + if ( data & ( 1 << 9 ) ) { + sdi->sdicsta |= ( 1 << 9 ); - switch ((data & 0x3f)+(sdi->acmd<<8)) { - case 256: - case 0: - sdi->acmd=0; - sdi->card_state=0; - break; + switch ( ( data & 0x3f ) + ( sdi->acmd << 8 ) ) { + case 256: + case 0: + sdi->acmd = 0; + sdi->card_state = 0; + break; - case 1: - sdi->sdicsta |= 0x3f; - sdi->sdirsp0 = 0x80ff8000; - sdi->sdirsp1 = 0xff000000; - break; + case 1: + sdi->sdicsta |= 0x3f; + sdi->sdirsp0 = 0x80ff8000; + sdi->sdirsp1 = 0xff000000; + break; - case 2: - sdi->card_state=2; - sdi->sdicsta |= 0x3f; - sdi->sdirsp0 = 0x02000053; - sdi->sdirsp1 = 0x444d3036; - sdi->sdirsp2 = 0x34011234; - sdi->sdirsp3 = 0x567813ff; - break; + case 2: + sdi->card_state = 2; + sdi->sdicsta |= 0x3f; + sdi->sdirsp0 = 0x02000053; + sdi->sdirsp1 = 0x444d3036; + sdi->sdirsp2 = 0x34011234; + sdi->sdirsp3 = 0x567813ff; + break; - case 3: - sdi->card_state=3; - sdi->sdicsta |= 3; - sdi->sdirsp0 = sdi->card_state<<9; - if(sdi->card_state==5) sdi->sdirsp0 |=0x100; - if(sdi->acmd) sdi->sdirsp0|=0x20; - sdi->sdirsp0|=0x12340000; // PUBLISHED RCA NUMBER - sdi->sdirsp1 = 0xff000000; - break; + case 3: + sdi->card_state = 3; + sdi->sdicsta |= 3; + sdi->sdirsp0 = sdi->card_state << 9; + if ( sdi->card_state == 5 ) + sdi->sdirsp0 |= 0x100; + if ( sdi->acmd ) + sdi->sdirsp0 |= 0x20; + sdi->sdirsp0 |= 0x12340000; // PUBLISHED RCA NUMBER + sdi->sdirsp1 = 0xff000000; + break; - case 7: - sdi->sdicsta |= 7; - sdi->sdirsp0 = 0x00000900; - sdi->sdirsp1 = 0xff000000; - break; - case 8: - sdi->sdicsta |=0x400; // SEND TIMEOUT IMMEDIATELY + case 7: + sdi->sdicsta |= 7; + sdi->sdirsp0 = 0x00000900; + sdi->sdirsp1 = 0xff000000; + break; + case 8: + sdi->sdicsta |= 0x400; // SEND TIMEOUT IMMEDIATELY + break; + + case 9: + { + /* c_size_mult + 2 + * blocks = (c_size + 1) * 2 + * + * size = bl_len * blocks; + */ + uint32_t write_bl_len = 9; + uint32_t read_bl_len = 9; + uint32_t c_size_mult = 7; + uint32_t c_size = 4096 - 1; + + sdi->sdicsta |= 9; + /* 127 .. 96 */ + sdi->sdirsp0 = 0x000f005a; + /* 95 .. 64 */ + sdi->sdirsp1 = 0x0f508000 | ( ( c_size & 0xffc ) >> 2 ) | ( ( read_bl_len & 0xf ) << 16 ); + /* 63 .. 32 */ + sdi->sdirsp2 = 0x2dd47c1f | ( ( c_size & 0x003 ) << 30 ) | ( ( c_size_mult & 7 ) << 15 ); + /* 31 .. 0 */ + sdi->sdirsp3 = 0x8a0040ff | ( ( write_bl_len & 0xf ) << 22 ); break; - - case 9: - { - /* c_size_mult + 2 - * blocks = (c_size + 1) * 2 - * - * size = bl_len * blocks; - */ - uint32_t write_bl_len = 9; - uint32_t read_bl_len = 9; - uint32_t c_size_mult = 7; - uint32_t c_size = 4096 - 1; - - sdi->sdicsta |= 9; - /* 127 .. 96 */ - sdi->sdirsp0 = 0x000f005a; - /* 95 .. 64 */ - sdi->sdirsp1 = 0x0f508000 | ((c_size & 0xffc) >> 2) | ((read_bl_len & 0xf) << 16); - /* 63 .. 32 */ - sdi->sdirsp2 = 0x2dd47c1f | ((c_size & 0x003) << 30) | ((c_size_mult & 7) << 15); - /* 31 .. 0 */ - sdi->sdirsp3 = 0x8a0040ff | ((write_bl_len & 0xf) << 22); - break; - } - - case 10: - sdi->sdicsta |= 10; - sdi->sdirsp0 = 0x02000053; - sdi->sdirsp1 = 0x444d3036; - sdi->sdirsp2 = 0x34011234; - sdi->sdirsp3 = 0x567813ff; - break; - - case 12: - sdi->multiple_block = 0; - sdi->nr_read_data=sdi->nr_write_data=0; - sdi->write_index=sdi->read_index=0; - if(sdi->read_data) { - free(sdi->read_data); - sdi->read_data = NULL; - } - sdi->sdidcon&=0x3000; - sdi->sdicsta |= 12; - sdi->sdirsp0 = 0x00000900; - sdi->sdirsp1 = 0xff000000; - break; - case 16: - sdi->sdicsta |= 16; - sdi->sdicsta |= data & 0x3f; - sdi->sdirsp0 = sdi->card_state<<9; - if(sdi->card_state==5) sdi->sdirsp0 |=0x100; - if(sdi->acmd) sdi->sdirsp0|=0x20; - sdi->sdirsp1 = 0xff000000; - break; - - case 18: - sdi->multiple_block = sdi->sdicarg; - /* fall through */ - case 17: - sdi->read_offset = sdi->sdicarg; - sdi->nr_read_data = sdi->sdibsize & 0xfff; - - sdcard_read(sdi); - - read_avail = sdi->nr_read_data - sdi->read_index; - - sdi->sdicsta |= data & 0x3f; - sdi->sdirsp0 = 0x00000b00; - sdi->sdirsp1 = 0xff000000; - sdi->sdidcnt = (1 << 12) | read_avail; - sdi->sdidsta |= (1 << 9) | (1 << 4); - break; - - case 23: - - - case 25: - sdi->multiple_block = sdi->sdicarg; - /* fall through */ - case 24: - sdi->write_offset = sdi->sdicarg; - sdi->nr_write_data = sdi->sdibsize & 0xfff; - - sdcard_write_prepare(sdi); - - write_avail = sdi->nr_write_data - sdi->write_index; - - sdi->sdicsta |= data & 0x3f; - sdi->sdirsp0 = 0x00000d00; - sdi->sdirsp1 = 0xff000000; - sdi->sdidcnt = (1 << 12) | write_avail; - break; - case 256+55: - case 55: - sdi->acmd=1; - sdi->sdicsta |= data & 0x3f; - sdi->sdirsp0 = sdi->card_state<<9; - if(sdi->card_state==5) sdi->sdirsp0 |=0x100; - if(sdi->acmd) sdi->sdirsp0|=0x20; - sdi->sdirsp1 = 0xff000000; - break; - case 256+41: - // ACMD41 - sdi->card_state= 1; // READY - sdi->sdicsta |= data & 0x3f; - sdi->sdirsp0 = 0x80ff8000; - sdi->sdirsp1 = 0xff000000; - sdi->acmd=0; - break; - - case 256+42: - // ACMD42 - sdi->sdicsta |= data & 0x3f; - sdi->sdirsp0 = sdi->card_state<<9; - if(sdi->card_state==5) sdi->sdirsp0 |=0x100; - if(sdi->acmd) sdi->sdirsp0|=0x20; - sdi->sdirsp1 = 0xff000000; - break; - - case 256+51: - // ACMD51 - sdi->sdicsta |= 51; - sdi->sdirsp0 = 0x0; - sdi->sdirsp1 = 0x01050000; - sdi->sdirsp2 = 0x0; - sdi->sdirsp3 = 0x0; - sdi->acmd=0; - break; - - default: - printf("unhandled SDcard CMD %u\n", (data & 0x3f)); - - sdi->sdicsta |= (1 << 10); - sdi->sdicsta &= ~(1 << 9); - sdi->acmd=0; - -// abort(); - break; - } - } - - break; - - case S3C2410_SDI_SDIDAT: - *(reg->datap) = data; - if((sdi->sdidcon&0x3000)==0x3000) { - write_avail = sdi->nr_write_data - sdi->write_index; - if (write_avail > 0) { - sdi->write_data[sdi->write_index++] = (*(reg->datap) >> ((sdi->sdicon&0x10)? 24:0)) & 0xff; - if ((sdi->nr_write_data - sdi->write_index) > 0) { - sdi->write_data[sdi->write_index++] = (*(reg->datap) >> ((sdi->sdicon&0x10)? 16:8)) & 0xff; - } - if ((sdi->nr_write_data - sdi->write_index) > 0) { - sdi->write_data[sdi->write_index++] = (*(reg->datap) >> ((sdi->sdicon&0x10)? 8:16)) & 0xff; - } - if ((sdi->nr_write_data - sdi->write_index) > 0) { - sdi->write_data[sdi->write_index++] = (*(reg->datap) >> ((sdi->sdicon&0x10)? 0:24)) & 0xff; - } - - if (sdi->write_index >= sdi->nr_write_data) { - sdcard_write(sdi); - if(sdi->multiple_block) sdi->multiple_block--; - if (sdi->multiple_block) { - sdi->write_offset += sdi->nr_write_data; - sdcard_write_prepare(sdi); - } else { - sdi->nr_write_data = 0; - sdi->sdidcon&=~0x3000; - } - - sdi->sdidsta |= (1 << 9) | (1 << 4) | (1<<3); - } - - write_avail = sdi->nr_write_data - sdi->write_index; - - sdi->sdidcnt = write_avail; } - } - break; + case 10: + sdi->sdicsta |= 10; + sdi->sdirsp0 = 0x02000053; + sdi->sdirsp1 = 0x444d3036; + sdi->sdirsp2 = 0x34011234; + sdi->sdirsp3 = 0x567813ff; + break; - case S3C2410_SDI_SDICSTA: - *(reg->datap) &= ~(data & 0xf00); - break; + case 12: + sdi->multiple_block = 0; + sdi->nr_read_data = sdi->nr_write_data = 0; + sdi->write_index = sdi->read_index = 0; + if ( sdi->read_data ) { + free( sdi->read_data ); + sdi->read_data = NULL; + } + sdi->sdidcon &= 0x3000; + sdi->sdicsta |= 12; + sdi->sdirsp0 = 0x00000900; + sdi->sdirsp1 = 0xff000000; + break; + case 16: + sdi->sdicsta |= 16; + sdi->sdicsta |= data & 0x3f; + sdi->sdirsp0 = sdi->card_state << 9; + if ( sdi->card_state == 5 ) + sdi->sdirsp0 |= 0x100; + if ( sdi->acmd ) + sdi->sdirsp0 |= 0x20; + sdi->sdirsp1 = 0xff000000; + break; - case S3C2410_SDI_SDIDSTA: - *(reg->datap) &= ~(data & 0x7fc); - break; + case 18: + sdi->multiple_block = sdi->sdicarg; + /* fall through */ + case 17: + sdi->read_offset = sdi->sdicarg; + sdi->nr_read_data = sdi->sdibsize & 0xfff; - case S3C2410_SDI_SDIFSTA: + sdcard_read( sdi ); + + read_avail = sdi->nr_read_data - sdi->read_index; + + sdi->sdicsta |= data & 0x3f; + sdi->sdirsp0 = 0x00000b00; + sdi->sdirsp1 = 0xff000000; + sdi->sdidcnt = ( 1 << 12 ) | read_avail; + sdi->sdidsta |= ( 1 << 9 ) | ( 1 << 4 ); + break; + + case 23: + + case 25: + sdi->multiple_block = sdi->sdicarg; + /* fall through */ + case 24: + sdi->write_offset = sdi->sdicarg; + sdi->nr_write_data = sdi->sdibsize & 0xfff; + + sdcard_write_prepare( sdi ); + + write_avail = sdi->nr_write_data - sdi->write_index; + + sdi->sdicsta |= data & 0x3f; + sdi->sdirsp0 = 0x00000d00; + sdi->sdirsp1 = 0xff000000; + sdi->sdidcnt = ( 1 << 12 ) | write_avail; + break; + case 256 + 55: + case 55: + sdi->acmd = 1; + sdi->sdicsta |= data & 0x3f; + sdi->sdirsp0 = sdi->card_state << 9; + if ( sdi->card_state == 5 ) + sdi->sdirsp0 |= 0x100; + if ( sdi->acmd ) + sdi->sdirsp0 |= 0x20; + sdi->sdirsp1 = 0xff000000; + break; + case 256 + 41: + // ACMD41 + sdi->card_state = 1; // READY + sdi->sdicsta |= data & 0x3f; + sdi->sdirsp0 = 0x80ff8000; + sdi->sdirsp1 = 0xff000000; + sdi->acmd = 0; + break; + + case 256 + 42: + // ACMD42 + sdi->sdicsta |= data & 0x3f; + sdi->sdirsp0 = sdi->card_state << 9; + if ( sdi->card_state == 5 ) + sdi->sdirsp0 |= 0x100; + if ( sdi->acmd ) + sdi->sdirsp0 |= 0x20; + sdi->sdirsp1 = 0xff000000; + break; + + case 256 + 51: + // ACMD51 + sdi->sdicsta |= 51; + sdi->sdirsp0 = 0x0; + sdi->sdirsp1 = 0x01050000; + sdi->sdirsp2 = 0x0; + sdi->sdirsp3 = 0x0; + sdi->acmd = 0; + break; + + default: + printf( "unhandled SDcard CMD %u\n", ( data & 0x3f ) ); + + sdi->sdicsta |= ( 1 << 10 ); + sdi->sdicsta &= ~( 1 << 9 ); + sdi->acmd = 0; + + // abort(); + break; + } + } + + break; + + case S3C2410_SDI_SDIDAT: + *( reg->datap ) = data; + if ( ( sdi->sdidcon & 0x3000 ) == 0x3000 ) { + write_avail = sdi->nr_write_data - sdi->write_index; + if ( write_avail > 0 ) { + sdi->write_data[ sdi->write_index++ ] = ( *( reg->datap ) >> ( ( sdi->sdicon & 0x10 ) ? 24 : 0 ) ) & 0xff; + if ( ( sdi->nr_write_data - sdi->write_index ) > 0 ) { + sdi->write_data[ sdi->write_index++ ] = ( *( reg->datap ) >> ( ( sdi->sdicon & 0x10 ) ? 16 : 8 ) ) & 0xff; + } + if ( ( sdi->nr_write_data - sdi->write_index ) > 0 ) { + sdi->write_data[ sdi->write_index++ ] = ( *( reg->datap ) >> ( ( sdi->sdicon & 0x10 ) ? 8 : 16 ) ) & 0xff; + } + if ( ( sdi->nr_write_data - sdi->write_index ) > 0 ) { + sdi->write_data[ sdi->write_index++ ] = ( *( reg->datap ) >> ( ( sdi->sdicon & 0x10 ) ? 0 : 24 ) ) & 0xff; + } + + if ( sdi->write_index >= sdi->nr_write_data ) { + sdcard_write( sdi ); + if ( sdi->multiple_block ) + sdi->multiple_block--; + if ( sdi->multiple_block ) { + sdi->write_offset += sdi->nr_write_data; + sdcard_write_prepare( sdi ); + } else { + sdi->nr_write_data = 0; + sdi->sdidcon &= ~0x3000; + } + + sdi->sdidsta |= ( 1 << 9 ) | ( 1 << 4 ) | ( 1 << 3 ); + } + + write_avail = sdi->nr_write_data - sdi->write_index; + + sdi->sdidcnt = write_avail; + } + } + break; + + case S3C2410_SDI_SDICSTA: + *( reg->datap ) &= ~( data & 0xf00 ); + break; + + case S3C2410_SDI_SDIDSTA: + *( reg->datap ) &= ~( data & 0x7fc ); + break; + + case S3C2410_SDI_SDIFSTA: case S3C2410_SDI_SDIDCNT: - /* ignore */ - break; + /* ignore */ + break; - default: - *(reg->datap) = data; - break; - } + default: + *( reg->datap ) = data; + break; + } } -void -s3c2410_sdi_unmount(x49gp_t *x49gp) +void s3c2410_sdi_unmount( x49gp_t* x49gp ) { - s3c2410_sdi_t *sdi = x49gp->s3c2410_sdi; + s3c2410_sdi_t* sdi = x49gp->s3c2410_sdi; - if (sdi->bs) { - bdrv_delete(sdi->bs); - sdi->bs = NULL; - } + if ( sdi->bs ) { + bdrv_delete( sdi->bs ); + sdi->bs = NULL; + } - if (sdi->fd >= 0) { - close(sdi->fd); - sdi->fd = -1; - } + if ( sdi->fd >= 0 ) { + close( sdi->fd ); + sdi->fd = -1; + } - g_free(sdi->filename); - sdi->filename = g_strdup(""); + g_free( sdi->filename ); + sdi->filename = g_strdup( "" ); - s3c2410_io_port_f_set_bit(x49gp, 3, 0); + s3c2410_io_port_f_set_bit( x49gp, 3, 0 ); } -int -s3c2410_sdi_mount(x49gp_t *x49gp, char *filename) +int s3c2410_sdi_mount( x49gp_t* x49gp, char* filename ) { - s3c2410_sdi_t *sdi = x49gp->s3c2410_sdi; - struct stat st; - char vvfat_name[1024]; - int error = 0; + s3c2410_sdi_t* sdi = x49gp->s3c2410_sdi; + struct stat st; + char vvfat_name[ 1024 ]; + int error = 0; - s3c2410_sdi_unmount(x49gp); - g_free(sdi->filename); - sdi->filename = filename; + s3c2410_sdi_unmount( x49gp ); + g_free( sdi->filename ); + sdi->filename = filename; - if (strcmp(filename, "") && (stat(filename, &st) == 0)) { - if (S_ISDIR(st.st_mode)) { - sprintf(vvfat_name, "fat:rw:16:%s", filename); - sdi->bs = bdrv_new(""); - if (sdi->bs) { - error = bdrv_open(sdi->bs, vvfat_name, 0); - if (error != 0) { - fprintf(stderr, - "%s:%u: bdrv_open %s: %d\n", - __FUNCTION__, __LINE__, - vvfat_name, error); - bdrv_delete(sdi->bs); - sdi->bs = NULL; - } - } - } else { - sdi->fd = open(filename, O_RDWR); - if (sdi->fd < 0) { - fprintf(stderr, "%s:%u: open %s: %s\n", - __FUNCTION__, __LINE__, filename, - strerror(errno)); - } - } - } + if ( strcmp( filename, "" ) && ( stat( filename, &st ) == 0 ) ) { + if ( S_ISDIR( st.st_mode ) ) { + sprintf( vvfat_name, "fat:rw:16:%s", filename ); + sdi->bs = bdrv_new( "" ); + if ( sdi->bs ) { + error = bdrv_open( sdi->bs, vvfat_name, 0 ); + if ( error != 0 ) { + fprintf( stderr, "%s:%u: bdrv_open %s: %d\n", __FUNCTION__, __LINE__, vvfat_name, error ); + bdrv_delete( sdi->bs ); + sdi->bs = NULL; + } + } + } else { + sdi->fd = open( filename, O_RDWR ); + if ( sdi->fd < 0 ) { + fprintf( stderr, "%s:%u: open %s: %s\n", __FUNCTION__, __LINE__, filename, strerror( errno ) ); + } + } + } - if ((sdi->bs != NULL) || (sdi->fd >= 0)) { - s3c2410_io_port_f_set_bit(x49gp, 3, 1); - } else { - s3c2410_io_port_f_set_bit(x49gp, 3, 0); - } + if ( ( sdi->bs != NULL ) || ( sdi->fd >= 0 ) ) { + s3c2410_io_port_f_set_bit( x49gp, 3, 1 ); + } else { + s3c2410_io_port_f_set_bit( x49gp, 3, 0 ); + } - return error; + return error; } -int -s3c2410_sdi_is_mounted(x49gp_t *x49gp) +int s3c2410_sdi_is_mounted( x49gp_t* x49gp ) { - s3c2410_sdi_t *sdi = x49gp->s3c2410_sdi; + s3c2410_sdi_t* sdi = x49gp->s3c2410_sdi; - return (sdi->bs != NULL) || (sdi->fd >= 0); + return ( sdi->bs != NULL ) || ( sdi->fd >= 0 ); } -static int -s3c2410_sdi_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_sdi_load( x49gp_module_t* module, GKeyFile* key ) { - x49gp_t *x49gp = module->x49gp; - s3c2410_sdi_t *sdi = module->user_data; - s3c2410_offset_t *reg; - char *filename, *filepath; - int error, error2; - int i; + x49gp_t* x49gp = module->x49gp; + s3c2410_sdi_t* sdi = module->user_data; + s3c2410_offset_t* reg; + char *filename, *filepath; + int error, error2; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - sdi->fd = -1; - sdi->bs = NULL; - sdi->filename = NULL; + sdi->fd = -1; + sdi->bs = NULL; + sdi->filename = NULL; - error = x49gp_module_get_filename(module, key, "filename", "", - &filename, &filepath); - if (strcmp(filename, "")) { - error2 = s3c2410_sdi_mount(x49gp, filepath); - if (0 == error) error = error2; - } else { - s3c2410_sdi_unmount(x49gp); - } - g_free(sdi->filename); - sdi->filename = filename; + error = x49gp_module_get_filename( module, key, "filename", "", &filename, &filepath ); + if ( strcmp( filename, "" ) ) { + error2 = s3c2410_sdi_mount( x49gp, filepath ); + if ( 0 == error ) + error = error2; + } else { + s3c2410_sdi_unmount( x49gp ); + } + g_free( sdi->filename ); + sdi->filename = filename; - for (i = 0; i < sdi->nr_regs; i++) { - reg = &sdi->regs[i]; + for ( i = 0; i < sdi->nr_regs; i++ ) { + reg = &sdi->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_sdi_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_sdi_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_sdi_t *sdi = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_sdi_t* sdi = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - x49gp_module_set_filename(module, key, "filename", sdi->filename); + x49gp_module_set_filename( module, key, "filename", sdi->filename ); - for (i = 0; i < sdi->nr_regs; i++) { - reg = &sdi->regs[i]; + for ( i = 0; i < sdi->nr_regs; i++ ) { + reg = &sdi->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_sdi_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_sdi_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_sdi_t *sdi = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_sdi_t* sdi = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < sdi->nr_regs; i++) { - reg = &sdi->regs[i]; + for ( i = 0; i < sdi->nr_regs; i++ ) { + reg = &sdi->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_sdi_readfn[] = -{ - s3c2410_sdi_read, - s3c2410_sdi_read, - s3c2410_sdi_read -}; +static CPUReadMemoryFunc* s3c2410_sdi_readfn[] = { s3c2410_sdi_read, s3c2410_sdi_read, s3c2410_sdi_read }; -static CPUWriteMemoryFunc *s3c2410_sdi_writefn[] = -{ - s3c2410_sdi_write, - s3c2410_sdi_write, - s3c2410_sdi_write -}; +static CPUWriteMemoryFunc* s3c2410_sdi_writefn[] = { s3c2410_sdi_write, s3c2410_sdi_write, s3c2410_sdi_write }; -static int -s3c2410_sdi_init(x49gp_module_t *module) +static int s3c2410_sdi_init( x49gp_module_t* module ) { - s3c2410_sdi_t *sdi; - int iotype; + s3c2410_sdi_t* sdi; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - sdi = malloc(sizeof(s3c2410_sdi_t)); - if (NULL == sdi) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->x49gp->progname, __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_sdi_data_init(sdi)) { - free(sdi); - return -ENOMEM; - } + sdi = malloc( sizeof( s3c2410_sdi_t ) ); + if ( NULL == sdi ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->x49gp->progname, __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_sdi_data_init( sdi ) ) { + free( sdi ); + return -ENOMEM; + } - module->user_data = sdi; - module->x49gp->s3c2410_sdi = sdi; - sdi->x49gp = module->x49gp; + module->user_data = sdi; + module->x49gp->s3c2410_sdi = sdi; + sdi->x49gp = module->x49gp; - iotype = cpu_register_io_memory(s3c2410_sdi_readfn, - s3c2410_sdi_writefn, sdi); + iotype = cpu_register_io_memory( s3c2410_sdi_readfn, s3c2410_sdi_writefn, sdi ); #ifdef DEBUG_S3C2410_SDI - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_SDI_BASE, S3C2410_MAP_SIZE, iotype); + cpu_register_physical_memory( S3C2410_SDI_BASE, S3C2410_MAP_SIZE, iotype ); - bdrv_init(); - return 0; + bdrv_init(); + return 0; } -static int -s3c2410_sdi_exit(x49gp_module_t *module) +static int s3c2410_sdi_exit( x49gp_module_t* module ) { - s3c2410_sdi_t *sdi; + s3c2410_sdi_t* sdi; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - sdi = module->user_data; - if (sdi->regs) - free(sdi->regs); - free(sdi); - } + if ( module->user_data ) { + sdi = module->user_data; + if ( sdi->regs ) + free( sdi->regs ); + free( sdi ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_sdi_init(x49gp_t *x49gp) +int x49gp_s3c2410_sdi_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-sdi", - s3c2410_sdi_init, - s3c2410_sdi_exit, - s3c2410_sdi_reset, - s3c2410_sdi_load, - s3c2410_sdi_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-sdi", s3c2410_sdi_init, s3c2410_sdi_exit, s3c2410_sdi_reset, s3c2410_sdi_load, s3c2410_sdi_save, + NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_spi.c b/src/s3c2410_spi.c index 0958400..6afe5cd 100644 --- a/src/s3c2410_spi.c +++ b/src/s3c2410_spi.c @@ -13,281 +13,242 @@ #include "s3c2410.h" #include "s3c2410_intc.h" - typedef struct { - uint32_t spicon0; - uint32_t spista0; - uint32_t sppin0; - uint32_t sppre0; - uint32_t sptdat0; - uint32_t sprdat0; - uint32_t spicon1; - uint32_t spista1; - uint32_t sppin1; - uint32_t sppre1; - uint32_t sptdat1; - uint32_t sprdat1; + uint32_t spicon0; + uint32_t spista0; + uint32_t sppin0; + uint32_t sppre0; + uint32_t sptdat0; + uint32_t sprdat0; + uint32_t spicon1; + uint32_t spista1; + uint32_t sppin1; + uint32_t sppre1; + uint32_t sptdat1; + uint32_t sprdat1; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; } s3c2410_spi_t; -static int -s3c2410_spi_data_init(s3c2410_spi_t *spi) +static int s3c2410_spi_data_init( s3c2410_spi_t* spi ) { - s3c2410_offset_t regs[] = - { - S3C2410_OFFSET(SPI, SPICON0, 0x00000000, spi->spicon0), - S3C2410_OFFSET(SPI, SPISTA0, 0x00000000, spi->spista0), - S3C2410_OFFSET(SPI, SPPIN0, 0x00000000, spi->sppin0), - S3C2410_OFFSET(SPI, SPPRE0, 0x00000000, spi->sppre0), - S3C2410_OFFSET(SPI, SPTDAT0, 0x00000000, spi->sptdat0), - S3C2410_OFFSET(SPI, SPRDAT0, 0x00000000, spi->sprdat0), - S3C2410_OFFSET(SPI, SPICON1, 0x00000000, spi->spicon1), - S3C2410_OFFSET(SPI, SPISTA1, 0x00000000, spi->spista1), - S3C2410_OFFSET(SPI, SPPIN1, 0x00000000, spi->sppin1), - S3C2410_OFFSET(SPI, SPPRE1, 0x00000000, spi->sppre1), - S3C2410_OFFSET(SPI, SPTDAT1, 0x00000000, spi->sptdat1), - S3C2410_OFFSET(SPI, SPRDAT1, 0x00000000, spi->sprdat1), - }; + s3c2410_offset_t regs[] = { + S3C2410_OFFSET( SPI, SPICON0, 0x00000000, spi->spicon0 ), S3C2410_OFFSET( SPI, SPISTA0, 0x00000000, spi->spista0 ), + S3C2410_OFFSET( SPI, SPPIN0, 0x00000000, spi->sppin0 ), S3C2410_OFFSET( SPI, SPPRE0, 0x00000000, spi->sppre0 ), + S3C2410_OFFSET( SPI, SPTDAT0, 0x00000000, spi->sptdat0 ), S3C2410_OFFSET( SPI, SPRDAT0, 0x00000000, spi->sprdat0 ), + S3C2410_OFFSET( SPI, SPICON1, 0x00000000, spi->spicon1 ), S3C2410_OFFSET( SPI, SPISTA1, 0x00000000, spi->spista1 ), + S3C2410_OFFSET( SPI, SPPIN1, 0x00000000, spi->sppin1 ), S3C2410_OFFSET( SPI, SPPRE1, 0x00000000, spi->sppre1 ), + S3C2410_OFFSET( SPI, SPTDAT1, 0x00000000, spi->sptdat1 ), S3C2410_OFFSET( SPI, SPRDAT1, 0x00000000, spi->sprdat1 ), + }; - memset(spi, 0, sizeof(s3c2410_spi_t)); + memset( spi, 0, sizeof( s3c2410_spi_t ) ); - spi->regs = malloc(sizeof(regs)); - if (NULL == spi->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + spi->regs = malloc( sizeof( regs ) ); + if ( NULL == spi->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(spi->regs, regs, sizeof(regs)); - spi->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( spi->regs, regs, sizeof( regs ) ); + spi->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -uint32_t -s3c2410_spi_read(void *opaque, target_phys_addr_t offset) +uint32_t s3c2410_spi_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_spi_t *spi = opaque; - s3c2410_offset_t *reg; + s3c2410_spi_t* spi = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(spi, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( spi, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(spi, offset); + reg = S3C2410_OFFSET_ENTRY( spi, offset ); #ifdef DEBUG_S3C2410_SPI - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-spi", S3C2410_SPI_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-spi", S3C2410_SPI_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - switch (offset) { - case S3C2410_SPI_SPRDAT0: - spi->spista0 &= ~(1); - break; + switch ( offset ) { + case S3C2410_SPI_SPRDAT0: + spi->spista0 &= ~( 1 ); + break; - case S3C2410_SPI_SPRDAT1: - spi->spista1 &= ~(1); - break; - } + case S3C2410_SPI_SPRDAT1: + spi->spista1 &= ~( 1 ); + break; + } - return *(reg->datap); + return *( reg->datap ); } -void -s3c2410_spi_write(void *opaque, target_phys_addr_t offset, uint32_t data) +void s3c2410_spi_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_spi_t *spi = opaque; - x49gp_t *x49gp = spi->x49gp; - s3c2410_offset_t *reg; + s3c2410_spi_t* spi = opaque; + x49gp_t* x49gp = spi->x49gp; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(spi, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( spi, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(spi, offset); + reg = S3C2410_OFFSET_ENTRY( spi, offset ); #ifdef DEBUG_S3C2410_SPI - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-spi", S3C2410_SPI_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-spi", S3C2410_SPI_BASE, reg->name, ( unsigned long )offset, data ); #endif - *(reg->datap) = data; + *( reg->datap ) = data; - switch (offset) { - case S3C2410_SPI_SPTDAT0: - spi->spista0 |= 1; - s3c2410_intc_assert(x49gp, INT_SPI0, 0); - break; + switch ( offset ) { + case S3C2410_SPI_SPTDAT0: + spi->spista0 |= 1; + s3c2410_intc_assert( x49gp, INT_SPI0, 0 ); + break; - case S3C2410_SPI_SPTDAT1: - spi->spista1 |= 1; - s3c2410_intc_assert(x49gp, INT_SPI1, 0); - break; - } + case S3C2410_SPI_SPTDAT1: + spi->spista1 |= 1; + s3c2410_intc_assert( x49gp, INT_SPI1, 0 ); + break; + } } -static int -s3c2410_spi_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_spi_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_spi_t *spi = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_spi_t* spi = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < spi->nr_regs; i++) { - reg = &spi->regs[i]; + for ( i = 0; i < spi->nr_regs; i++ ) { + reg = &spi->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_spi_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_spi_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_spi_t *spi = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_spi_t* spi = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < spi->nr_regs; i++) { - reg = &spi->regs[i]; + for ( i = 0; i < spi->nr_regs; i++ ) { + reg = &spi->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_spi_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_spi_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_spi_t *spi = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_spi_t* spi = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < spi->nr_regs; i++) { - reg = &spi->regs[i]; + for ( i = 0; i < spi->nr_regs; i++ ) { + reg = &spi->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_spi_readfn[] = -{ - s3c2410_spi_read, - s3c2410_spi_read, - s3c2410_spi_read -}; +static CPUReadMemoryFunc* s3c2410_spi_readfn[] = { s3c2410_spi_read, s3c2410_spi_read, s3c2410_spi_read }; -static CPUWriteMemoryFunc *s3c2410_spi_writefn[] = -{ - s3c2410_spi_write, - s3c2410_spi_write, - s3c2410_spi_write -}; +static CPUWriteMemoryFunc* s3c2410_spi_writefn[] = { s3c2410_spi_write, s3c2410_spi_write, s3c2410_spi_write }; -static int -s3c2410_spi_init(x49gp_module_t *module) +static int s3c2410_spi_init( x49gp_module_t* module ) { - s3c2410_spi_t *spi; - int iotype; + s3c2410_spi_t* spi; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - spi = malloc(sizeof(s3c2410_spi_t)); - if (NULL == spi) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_spi_data_init(spi)) { - free(spi); - return -ENOMEM; - } + spi = malloc( sizeof( s3c2410_spi_t ) ); + if ( NULL == spi ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_spi_data_init( spi ) ) { + free( spi ); + return -ENOMEM; + } - module->user_data = spi; - spi->x49gp = module->x49gp; + module->user_data = spi; + spi->x49gp = module->x49gp; - iotype = cpu_register_io_memory(s3c2410_spi_readfn, - s3c2410_spi_writefn, spi); + iotype = cpu_register_io_memory( s3c2410_spi_readfn, s3c2410_spi_writefn, spi ); #ifdef DEBUG_S3C2410_SPI - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_SPI_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_SPI_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_spi_exit(x49gp_module_t *module) +static int s3c2410_spi_exit( x49gp_module_t* module ) { - s3c2410_spi_t *spi; + s3c2410_spi_t* spi; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - spi = module->user_data; - if (spi->regs) - free(spi->regs); - free(spi); - } + if ( module->user_data ) { + spi = module->user_data; + if ( spi->regs ) + free( spi->regs ); + free( spi ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_spi_init(x49gp_t *x49gp) +int x49gp_s3c2410_spi_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-spi", - s3c2410_spi_init, - s3c2410_spi_exit, - s3c2410_spi_reset, - s3c2410_spi_load, - s3c2410_spi_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-spi", s3c2410_spi_init, s3c2410_spi_exit, s3c2410_spi_reset, s3c2410_spi_load, s3c2410_spi_save, + NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_sram.c b/src/s3c2410_sram.c index 7bca21c..5e4cec7 100644 --- a/src/s3c2410_sram.c +++ b/src/s3c2410_sram.c @@ -14,182 +14,157 @@ #include "byteorder.h" typedef struct { - void *data; - char *filename; - int fd; - uint32_t offset; - size_t size; + void* data; + char* filename; + int fd; + uint32_t offset; + size_t size; } filemap_t; -static int -s3c2410_sram_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_sram_load( x49gp_module_t* module, GKeyFile* key ) { - filemap_t *filemap = module->user_data; - char *filename; - int error; + filemap_t* filemap = module->user_data; + char* filename; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - error = x49gp_module_get_filename(module, key, "filename", - "s3c2410-sram", &(filemap->filename), - &filename); + error = x49gp_module_get_filename( module, key, "filename", "s3c2410-sram", &( filemap->filename ), &filename ); - filemap->fd = open(filename, O_RDWR | O_CREAT, 0644); - if (filemap->fd < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: open %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - return error; - } + filemap->fd = open( filename, O_RDWR | O_CREAT, 0644 ); + if ( filemap->fd < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: open %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + return error; + } - filemap->size = S3C2410_SRAM_SIZE; - if (ftruncate(filemap->fd, filemap->size) < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: ftruncate %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(filemap->fd); - filemap->fd = -1; - return error; - } + filemap->size = S3C2410_SRAM_SIZE; + if ( ftruncate( filemap->fd, filemap->size ) < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: ftruncate %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( filemap->fd ); + filemap->fd = -1; + return error; + } - filemap->data = mmap(phys_ram_base + filemap->offset, filemap->size, - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, - filemap->fd, 0); - if (filemap->data == (void *) -1) { - error = -errno; - fprintf(stderr, "%s: %s:%u: mmap %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(filemap->fd); - filemap->fd = -1; - return error; - } + filemap->data = mmap( phys_ram_base + filemap->offset, filemap->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, filemap->fd, 0 ); + if ( filemap->data == ( void* )-1 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: mmap %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( filemap->fd ); + filemap->fd = -1; + return error; + } - g_free(filename); + g_free( filename ); - x49gp_schedule_lcd_update(module->x49gp); - return error; + x49gp_schedule_lcd_update( module->x49gp ); + return error; } -static int -s3c2410_sram_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_sram_save( x49gp_module_t* module, GKeyFile* key ) { - filemap_t *filemap = module->user_data; - int error; + filemap_t* filemap = module->user_data; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - x49gp_module_set_filename(module, key, "filename", filemap->filename); + x49gp_module_set_filename( module, key, "filename", filemap->filename ); - error = msync(filemap->data, filemap->size, MS_ASYNC); - if (error) { - fprintf(stderr, "%s:%u: msync: %s\n", - __FUNCTION__, __LINE__, strerror(errno)); - return error; - } + error = msync( filemap->data, filemap->size, MS_ASYNC ); + if ( error ) { + fprintf( stderr, "%s:%u: msync: %s\n", __FUNCTION__, __LINE__, strerror( errno ) ); + return error; + } - error = fsync(filemap->fd); - if (error) { - fprintf(stderr, "%s:%u: fsync: %s\n", - __FUNCTION__, __LINE__, strerror(errno)); - return error; - } + error = fsync( filemap->fd ); + if ( error ) { + fprintf( stderr, "%s:%u: fsync: %s\n", __FUNCTION__, __LINE__, strerror( errno ) ); + return error; + } - return 0; + return 0; } -static int -s3c2410_sram_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_sram_reset( x49gp_module_t* module, x49gp_reset_t reset ) { #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - return 0; + return 0; } -static int -s3c2410_sram_init(x49gp_module_t *module) +static int s3c2410_sram_init( x49gp_module_t* module ) { - filemap_t *filemap; + filemap_t* filemap; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - filemap = malloc(sizeof(filemap_t)); - if (NULL == filemap) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + filemap = malloc( sizeof( filemap_t ) ); + if ( NULL == filemap ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - filemap->size = 0; - filemap->fd = -1; + filemap->size = 0; + filemap->fd = -1; - module->user_data = filemap; + module->user_data = filemap; - filemap->data = (void *) -1; - filemap->offset = phys_ram_size; - phys_ram_size += S3C2410_SRAM_SIZE; + filemap->data = ( void* )-1; + filemap->offset = phys_ram_size; + phys_ram_size += S3C2410_SRAM_SIZE; - cpu_register_physical_memory(S3C2410_SRAM_BASE, S3C2410_SRAM_SIZE, - filemap->offset | IO_MEM_RAM); + cpu_register_physical_memory( S3C2410_SRAM_BASE, S3C2410_SRAM_SIZE, filemap->offset | IO_MEM_RAM ); - return 0; + return 0; } -static int -s3c2410_sram_exit(x49gp_module_t *module) +static int s3c2410_sram_exit( x49gp_module_t* module ) { - filemap_t *filemap; + filemap_t* filemap; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - filemap = module->user_data; + if ( module->user_data ) { + filemap = module->user_data; - if (filemap->data != (void *) -1) { - munmap(filemap->data, filemap->size); - } - if (filemap->fd >= 0) { - close(filemap->fd); - } + if ( filemap->data != ( void* )-1 ) { + munmap( filemap->data, filemap->size ); + } + if ( filemap->fd >= 0 ) { + close( filemap->fd ); + } - free(filemap); - } + free( filemap ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_sram_init(x49gp_t *x49gp) +int x49gp_s3c2410_sram_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-sram", - s3c2410_sram_init, - s3c2410_sram_exit, - s3c2410_sram_reset, - s3c2410_sram_load, - s3c2410_sram_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-sram", s3c2410_sram_init, s3c2410_sram_exit, s3c2410_sram_reset, s3c2410_sram_load, + s3c2410_sram_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_timer.c b/src/s3c2410_timer.c index ce5f0c0..af65c4d 100644 --- a/src/s3c2410_timer.c +++ b/src/s3c2410_timer.c @@ -14,546 +14,490 @@ #include "s3c2410_timer.h" #include "s3c2410_intc.h" - typedef struct { - uint32_t reload_bit; - uint32_t update_bit; - uint32_t start_bit; - unsigned int pre_shift; - unsigned int mux_shift; - int irq; + uint32_t reload_bit; + uint32_t update_bit; + uint32_t start_bit; + unsigned int pre_shift; + unsigned int mux_shift; + int irq; } s3c2410_timer_config_t; struct __s3c2410_timer_s__; typedef struct __s3c2410_timer_s__ s3c2410_timer_t; struct __s3c2410_timer_s__ { - uint32_t tcfg0; - uint32_t tcfg1; - uint32_t tcon; - uint32_t prev_tcon; + uint32_t tcfg0; + uint32_t tcfg1; + uint32_t tcon; + uint32_t prev_tcon; - x49gp_t *x49gp; + x49gp_t* x49gp; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - struct s3c2410_timeout { - uint32_t tcntb; - uint32_t tcmpb; - uint32_t tcnt; - uint32_t tcmp; + struct s3c2410_timeout { + uint32_t tcntb; + uint32_t tcmpb; + uint32_t tcnt; + uint32_t tcmp; - const s3c2410_timer_config_t *tconfig; - int index; + const s3c2410_timer_config_t* tconfig; + int index; - s3c2410_timer_t *main; + s3c2410_timer_t* main; - unsigned long interval; - x49gp_timer_t *timer; - } timeout[5]; + unsigned long interval; + x49gp_timer_t* timer; + } timeout[ 5 ]; }; -static const s3c2410_timer_config_t s3c2410_timer_config[] = -{ - { - TCON_TIMER0_RELOAD, TCON_TIMER0_UPDATE, TCON_TIMER0_START, - TCFG0_PRE0_SHIFT, TCFG1_MUX0_SHIFT, INT_TIMER0 - }, - { - TCON_TIMER1_RELOAD, TCON_TIMER1_UPDATE, TCON_TIMER1_START, - TCFG0_PRE0_SHIFT, TCFG1_MUX1_SHIFT, INT_TIMER1 - }, - { - TCON_TIMER2_RELOAD, TCON_TIMER2_UPDATE, TCON_TIMER2_START, - TCFG0_PRE1_SHIFT, TCFG1_MUX2_SHIFT, INT_TIMER2 - }, - { - TCON_TIMER3_RELOAD, TCON_TIMER3_UPDATE, TCON_TIMER3_START, - TCFG0_PRE1_SHIFT, TCFG1_MUX3_SHIFT, INT_TIMER3 - }, - { - TCON_TIMER4_RELOAD, TCON_TIMER4_UPDATE, TCON_TIMER4_START, - TCFG0_PRE1_SHIFT, TCFG1_MUX4_SHIFT, INT_TIMER4 - }, +static const s3c2410_timer_config_t s3c2410_timer_config[] = { + {TCON_TIMER0_RELOAD, TCON_TIMER0_UPDATE, TCON_TIMER0_START, TCFG0_PRE0_SHIFT, TCFG1_MUX0_SHIFT, INT_TIMER0}, + {TCON_TIMER1_RELOAD, TCON_TIMER1_UPDATE, TCON_TIMER1_START, TCFG0_PRE0_SHIFT, TCFG1_MUX1_SHIFT, INT_TIMER1}, + {TCON_TIMER2_RELOAD, TCON_TIMER2_UPDATE, TCON_TIMER2_START, TCFG0_PRE1_SHIFT, TCFG1_MUX2_SHIFT, INT_TIMER2}, + {TCON_TIMER3_RELOAD, TCON_TIMER3_UPDATE, TCON_TIMER3_START, TCFG0_PRE1_SHIFT, TCFG1_MUX3_SHIFT, INT_TIMER3}, + {TCON_TIMER4_RELOAD, TCON_TIMER4_UPDATE, TCON_TIMER4_START, TCFG0_PRE1_SHIFT, TCFG1_MUX4_SHIFT, INT_TIMER4}, }; -static int -s3c2410_timer_data_init(s3c2410_timer_t *timer) +static int s3c2410_timer_data_init( s3c2410_timer_t* timer ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(TIMER, TCFG0, 0, timer->tcfg0), - S3C2410_OFFSET(TIMER, TCFG1, 0, timer->tcfg1), - S3C2410_OFFSET(TIMER, TCON, 0, timer->tcon), - S3C2410_OFFSET(TIMER, TCNTB0, 0, timer->timeout[0].tcntb), - S3C2410_OFFSET(TIMER, TCMPB0, 0, timer->timeout[0].tcmpb), - S3C2410_OFFSET(TIMER, TCNTO0, 0, timer->timeout[0].tcnt), - S3C2410_OFFSET(TIMER, TCNTB1, 0, timer->timeout[1].tcntb), - S3C2410_OFFSET(TIMER, TCMPB1, 0, timer->timeout[1].tcmpb), - S3C2410_OFFSET(TIMER, TCNTO1, 0, timer->timeout[1].tcnt), - S3C2410_OFFSET(TIMER, TCNTB2, 0, timer->timeout[2].tcntb), - S3C2410_OFFSET(TIMER, TCMPB2, 0, timer->timeout[2].tcmpb), - S3C2410_OFFSET(TIMER, TCNTO2, 0, timer->timeout[2].tcnt), - S3C2410_OFFSET(TIMER, TCNTB3, 0, timer->timeout[3].tcntb), - S3C2410_OFFSET(TIMER, TCMPB3, 0, timer->timeout[3].tcmpb), - S3C2410_OFFSET(TIMER, TCNTO3, 0, timer->timeout[3].tcnt), - S3C2410_OFFSET(TIMER, TCNTB4, 0, timer->timeout[4].tcntb), - S3C2410_OFFSET(TIMER, TCNTO4, 0, timer->timeout[4].tcnt) - }; + s3c2410_offset_t regs[] = { S3C2410_OFFSET( TIMER, TCFG0, 0, timer->tcfg0 ), + S3C2410_OFFSET( TIMER, TCFG1, 0, timer->tcfg1 ), + S3C2410_OFFSET( TIMER, TCON, 0, timer->tcon ), + S3C2410_OFFSET( TIMER, TCNTB0, 0, timer->timeout[ 0 ].tcntb ), + S3C2410_OFFSET( TIMER, TCMPB0, 0, timer->timeout[ 0 ].tcmpb ), + S3C2410_OFFSET( TIMER, TCNTO0, 0, timer->timeout[ 0 ].tcnt ), + S3C2410_OFFSET( TIMER, TCNTB1, 0, timer->timeout[ 1 ].tcntb ), + S3C2410_OFFSET( TIMER, TCMPB1, 0, timer->timeout[ 1 ].tcmpb ), + S3C2410_OFFSET( TIMER, TCNTO1, 0, timer->timeout[ 1 ].tcnt ), + S3C2410_OFFSET( TIMER, TCNTB2, 0, timer->timeout[ 2 ].tcntb ), + S3C2410_OFFSET( TIMER, TCMPB2, 0, timer->timeout[ 2 ].tcmpb ), + S3C2410_OFFSET( TIMER, TCNTO2, 0, timer->timeout[ 2 ].tcnt ), + S3C2410_OFFSET( TIMER, TCNTB3, 0, timer->timeout[ 3 ].tcntb ), + S3C2410_OFFSET( TIMER, TCMPB3, 0, timer->timeout[ 3 ].tcmpb ), + S3C2410_OFFSET( TIMER, TCNTO3, 0, timer->timeout[ 3 ].tcnt ), + S3C2410_OFFSET( TIMER, TCNTB4, 0, timer->timeout[ 4 ].tcntb ), + S3C2410_OFFSET( TIMER, TCNTO4, 0, timer->timeout[ 4 ].tcnt ) }; - memset(timer, 0, sizeof(s3c2410_timer_t)); + memset( timer, 0, sizeof( s3c2410_timer_t ) ); - timer->regs = malloc(sizeof(regs)); - if (NULL == timer->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + timer->regs = malloc( sizeof( regs ) ); + if ( NULL == timer->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(timer->regs, regs, sizeof(regs)); - timer->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( timer->regs, regs, sizeof( regs ) ); + timer->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static void -s3c2410_timer_timeout(void *data) +static void s3c2410_timer_timeout( void* data ) { - struct s3c2410_timeout *t = data; - s3c2410_timer_t *timer = t->main; - x49gp_t *x49gp = timer->x49gp; - int64_t timeout; + struct s3c2410_timeout* t = data; + s3c2410_timer_t* timer = t->main; + x49gp_t* x49gp = timer->x49gp; + int64_t timeout; #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: assert TIMER%u interrupt\n", t->index); + printf( "s3c2410-timer: assert TIMER%u interrupt\n", t->index ); #endif - s3c2410_intc_assert(timer->x49gp, t->tconfig->irq, 0); + s3c2410_intc_assert( timer->x49gp, t->tconfig->irq, 0 ); - if (timer->tcon & t->tconfig->reload_bit) { - t->tcnt = t->tcntb; - t->tcmp = t->tcmpb; - } else { - timer->tcon &= ~(t->tconfig->start_bit); - return; - } + if ( timer->tcon & t->tconfig->reload_bit ) { + t->tcnt = t->tcntb; + t->tcmp = t->tcmpb; + } else { + timer->tcon &= ~( t->tconfig->start_bit ); + return; + } - timeout = 1000000LL * t->tcnt * t->interval / x49gp->PCLK; + timeout = 1000000LL * t->tcnt * t->interval / x49gp->PCLK; #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: reload TIMER%u: CNT %u (%lu PCLKs): %llu us\n", t->index, t->tcnt, t->interval, (unsigned long long) timeout); + printf( "s3c2410-timer: reload TIMER%u: CNT %u (%lu PCLKs): %llu us\n", t->index, t->tcnt, t->interval, ( unsigned long long )timeout ); #endif - x49gp_mod_timer(t->timer, x49gp_get_clock() + timeout); + x49gp_mod_timer( t->timer, x49gp_get_clock() + timeout ); } -unsigned long -s3c2410_timer_next_interrupt(x49gp_t *x49gp) +unsigned long s3c2410_timer_next_interrupt( x49gp_t* x49gp ) { - s3c2410_timer_t *timer = x49gp->s3c2410_timer; - struct s3c2410_timeout *t; - unsigned long irq, next; - unsigned long ticks; - int i; + s3c2410_timer_t* timer = x49gp->s3c2410_timer; + struct s3c2410_timeout* t; + unsigned long irq, next; + unsigned long ticks; + int i; - ticks = x49gp_get_clock(); + ticks = x49gp_get_clock(); - next = ~(0); - for (i = 0; i < 5; i++) { - t = &timer->timeout[i]; + next = ~( 0 ); + for ( i = 0; i < 5; i++ ) { + t = &timer->timeout[ i ]; - if (!(timer->tcon & t->tconfig->start_bit)) - continue; + if ( !( timer->tcon & t->tconfig->start_bit ) ) + continue; - if (x49gp_timer_pending(t->timer)) { - irq = x49gp_timer_expires(t->timer) - ticks; - } else { - irq = 0; - } + if ( x49gp_timer_pending( t->timer ) ) { + irq = x49gp_timer_expires( t->timer ) - ticks; + } else { + irq = 0; + } - if (t->tcnt) { - irq += (t->tcnt - 1) * t->interval; - } else { - if (!(timer->tcon & t->tconfig->reload_bit)) - continue; - irq += t->tcntb * t->interval; - } + if ( t->tcnt ) { + irq += ( t->tcnt - 1 ) * t->interval; + } else { + if ( !( timer->tcon & t->tconfig->reload_bit ) ) + continue; + irq += t->tcntb * t->interval; + } - if (irq < next) - next = irq; + if ( irq < next ) + next = irq; #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: TIMER%u: tcnt %u, interval %lu, pending %u, next irq %lu\n", - t->index, t->tcnt, t->interval, x49gp_timer_pending(t->timer), irq); + printf( "s3c2410-timer: TIMER%u: tcnt %u, interval %lu, pending %u, next irq %lu\n", t->index, t->tcnt, t->interval, + x49gp_timer_pending( t->timer ), irq ); #endif - } + } - return next; + return next; } -static void -s3c2410_update_tcfg(s3c2410_timer_t *timer) +static void s3c2410_update_tcfg( s3c2410_timer_t* timer ) { - struct s3c2410_timeout *t; - x49gp_t *x49gp = timer->x49gp; - uint32_t pre, mux; - int64_t timeout; - int i; + struct s3c2410_timeout* t; + x49gp_t* x49gp = timer->x49gp; + uint32_t pre, mux; + int64_t timeout; + int i; - for (i = 0; i < 5; i++) { - t = &timer->timeout[i]; + for ( i = 0; i < 5; i++ ) { + t = &timer->timeout[ i ]; - pre = (timer->tcfg0 >> t->tconfig->pre_shift) & TCFG0_PREx_MASK; - mux = (timer->tcfg1 >> t->tconfig->mux_shift) & TCFG1_MUXx_MASK; + pre = ( timer->tcfg0 >> t->tconfig->pre_shift ) & TCFG0_PREx_MASK; + mux = ( timer->tcfg1 >> t->tconfig->mux_shift ) & TCFG1_MUXx_MASK; - if (mux > 3) { - printf("s3c2410-timer: can't handle MUX %02x for TIMER%u\n", - mux, t->index); - mux = 3; - } + if ( mux > 3 ) { + printf( "s3c2410-timer: can't handle MUX %02x for TIMER%u\n", mux, t->index ); + mux = 3; + } - t->interval = (pre + 1) * (2 << mux); + t->interval = ( pre + 1 ) * ( 2 << mux ); #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: TIMER%u: pre %u, mux %u, tick %lu PCLKs\n", - t->index, pre, mux, t->interval); + printf( "s3c2410-timer: TIMER%u: pre %u, mux %u, tick %lu PCLKs\n", t->index, pre, mux, t->interval ); #endif - if (x49gp_timer_pending(t->timer)) { - timeout = 1000000LL * t->tcnt * t->interval / x49gp->PCLK; + if ( x49gp_timer_pending( t->timer ) ) { + timeout = 1000000LL * t->tcnt * t->interval / x49gp->PCLK; #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: mod TIMER%u: CNT %u (%lu PCLKs): %llu us\n", t->index, t->tcnt, t->interval, (unsigned long long) timeout); + printf( "s3c2410-timer: mod TIMER%u: CNT %u (%lu PCLKs): %llu us\n", t->index, t->tcnt, t->interval, + ( unsigned long long )timeout ); #endif - x49gp_mod_timer(t->timer, x49gp_get_clock() + timeout); - } - } + x49gp_mod_timer( t->timer, x49gp_get_clock() + timeout ); + } + } } -static void -s3c2410_update_tcon(s3c2410_timer_t *timer) +static void s3c2410_update_tcon( s3c2410_timer_t* timer ) { - struct s3c2410_timeout *t; - x49gp_t *x49gp = timer->x49gp; - int64_t timeout; - uint32_t change; - int i; + struct s3c2410_timeout* t; + x49gp_t* x49gp = timer->x49gp; + int64_t timeout; + uint32_t change; + int i; - change = timer->prev_tcon ^ timer->tcon; - timer->prev_tcon = timer->tcon; + change = timer->prev_tcon ^ timer->tcon; + timer->prev_tcon = timer->tcon; - for (i = 0; i < 5; i++) { - t = &timer->timeout[i]; + for ( i = 0; i < 5; i++ ) { + t = &timer->timeout[ i ]; - if (timer->tcon & t->tconfig->update_bit) { - t->tcnt = t->tcntb; - t->tcmp = t->tcmpb; + if ( timer->tcon & t->tconfig->update_bit ) { + t->tcnt = t->tcntb; + t->tcmp = t->tcmpb; #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: update TIMER%u tcnt %u, tcmp %u\n", t->index, t->tcnt, t->tcmp); + printf( "s3c2410-timer: update TIMER%u tcnt %u, tcmp %u\n", t->index, t->tcnt, t->tcmp ); #endif - } + } - if (change & t->tconfig->start_bit) { - if (timer->tcon & t->tconfig->start_bit) { - timeout = 1000000LL * t->tcnt * t->interval / x49gp->PCLK; + if ( change & t->tconfig->start_bit ) { + if ( timer->tcon & t->tconfig->start_bit ) { + timeout = 1000000LL * t->tcnt * t->interval / x49gp->PCLK; #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: start TIMER%u: CNT %u (%lu PCLKs): %llu us\n", t->index, t->tcnt, t->interval, (unsigned long long) timeout); + printf( "s3c2410-timer: start TIMER%u: CNT %u (%lu PCLKs): %llu us\n", t->index, t->tcnt, t->interval, + ( unsigned long long )timeout ); #endif - x49gp_mod_timer(t->timer, x49gp_get_clock() + timeout); - } else { - x49gp_del_timer(t->timer); + x49gp_mod_timer( t->timer, x49gp_get_clock() + timeout ); + } else { + x49gp_del_timer( t->timer ); #ifdef DEBUG_S3C2410_TIMER - printf("s3c2410-timer: stop TIMER%u\n", t->index); + printf( "s3c2410-timer: stop TIMER%u\n", t->index ); #endif - } - } - } + } + } + } } -static uint32_t -s3c2410_read_tcnt(s3c2410_timer_t *timer, int index) +static uint32_t s3c2410_read_tcnt( s3c2410_timer_t* timer, int index ) { - struct s3c2410_timeout *t = &timer->timeout[index]; - x49gp_t *x49gp = timer->x49gp; - int64_t now, expires, timeout; + struct s3c2410_timeout* t = &timer->timeout[ index ]; + x49gp_t* x49gp = timer->x49gp; + int64_t now, expires, timeout; - if (!(timer->tcon & t->tconfig->start_bit)) - return t->tcnt; + if ( !( timer->tcon & t->tconfig->start_bit ) ) + return t->tcnt; - if (x49gp_timer_pending(t->timer)) { - now = x49gp_get_clock(); - expires = x49gp_timer_expires(t->timer); + if ( x49gp_timer_pending( t->timer ) ) { + now = x49gp_get_clock(); + expires = x49gp_timer_expires( t->timer ); - timeout = expires - now; - if (timeout <= 0) - return 0; + timeout = expires - now; + if ( timeout <= 0 ) + return 0; - t->tcnt = timeout * x49gp->PCLK / (1000000LL * t->interval); - } + t->tcnt = timeout * x49gp->PCLK / ( 1000000LL * t->interval ); + } - return t->tcnt; + return t->tcnt; } -static uint32_t -s3c2410_timer_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_timer_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_timer_t *timer = opaque; - s3c2410_offset_t *reg; - uint32_t data; + s3c2410_timer_t* timer = opaque; + s3c2410_offset_t* reg; + uint32_t data; - if (! S3C2410_OFFSET_OK(timer, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( timer, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(timer, offset); + reg = S3C2410_OFFSET_ENTRY( timer, offset ); - switch (offset) { - case S3C2410_TIMER_TCNTO0: - data = s3c2410_read_tcnt(timer, 0); - break; - case S3C2410_TIMER_TCNTO1: - data = s3c2410_read_tcnt(timer, 1); - break; - case S3C2410_TIMER_TCNTO2: - data = s3c2410_read_tcnt(timer, 2); - break; - case S3C2410_TIMER_TCNTO3: - data = s3c2410_read_tcnt(timer, 3); - break; - case S3C2410_TIMER_TCNTO4: - data = s3c2410_read_tcnt(timer, 4); - break; - default: - data = *(reg->datap); - break; - } + switch ( offset ) { + case S3C2410_TIMER_TCNTO0: + data = s3c2410_read_tcnt( timer, 0 ); + break; + case S3C2410_TIMER_TCNTO1: + data = s3c2410_read_tcnt( timer, 1 ); + break; + case S3C2410_TIMER_TCNTO2: + data = s3c2410_read_tcnt( timer, 2 ); + break; + case S3C2410_TIMER_TCNTO3: + data = s3c2410_read_tcnt( timer, 3 ); + break; + case S3C2410_TIMER_TCNTO4: + data = s3c2410_read_tcnt( timer, 4 ); + break; + default: + data = *( reg->datap ); + break; + } #ifdef DEBUG_S3C2410_TIMER - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-timer", S3C2410_TIMER_BASE, - reg->name, (unsigned long) offset, data); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-timer", S3C2410_TIMER_BASE, reg->name, ( unsigned long )offset, data ); #endif - return data; + return data; } -static void -s3c2410_timer_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_timer_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_timer_t *timer = opaque; - s3c2410_offset_t *reg; + s3c2410_timer_t* timer = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(timer, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( timer, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(timer, offset); + reg = S3C2410_OFFSET_ENTRY( timer, offset ); #ifdef DEBUG_S3C2410_TIMER - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-timer", S3C2410_TIMER_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-timer", S3C2410_TIMER_BASE, reg->name, ( unsigned long )offset, data ); #endif - switch (offset) { - case S3C2410_TIMER_TCFG0: - *(reg->datap) = data; - s3c2410_update_tcfg(timer); - break; - case S3C2410_TIMER_TCFG1: - *(reg->datap) = data; - s3c2410_update_tcfg(timer); - break; - case S3C2410_TIMER_TCON: - *(reg->datap) = data; - s3c2410_update_tcon(timer); - break; - case S3C2410_TIMER_TCNTO0: - case S3C2410_TIMER_TCNTO1: - case S3C2410_TIMER_TCNTO2: - case S3C2410_TIMER_TCNTO3: - case S3C2410_TIMER_TCNTO4: - /* read only */ - break; - default: - *(reg->datap) = data; - break; - } + switch ( offset ) { + case S3C2410_TIMER_TCFG0: + *( reg->datap ) = data; + s3c2410_update_tcfg( timer ); + break; + case S3C2410_TIMER_TCFG1: + *( reg->datap ) = data; + s3c2410_update_tcfg( timer ); + break; + case S3C2410_TIMER_TCON: + *( reg->datap ) = data; + s3c2410_update_tcon( timer ); + break; + case S3C2410_TIMER_TCNTO0: + case S3C2410_TIMER_TCNTO1: + case S3C2410_TIMER_TCNTO2: + case S3C2410_TIMER_TCNTO3: + case S3C2410_TIMER_TCNTO4: + /* read only */ + break; + default: + *( reg->datap ) = data; + break; + } } -static int -s3c2410_timer_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_timer_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_timer_t *timer = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_timer_t* timer = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < timer->nr_regs; i++) { - reg = &timer->regs[i]; + for ( i = 0; i < timer->nr_regs; i++ ) { + reg = &timer->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - s3c2410_update_tcon(timer); - s3c2410_update_tcfg(timer); + s3c2410_update_tcon( timer ); + s3c2410_update_tcfg( timer ); - return error; + return error; } -static int -s3c2410_timer_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_timer_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_timer_t *timer = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_timer_t* timer = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < timer->nr_regs; i++) { - reg = &timer->regs[i]; + for ( i = 0; i < timer->nr_regs; i++ ) { + reg = &timer->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_timer_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_timer_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_timer_t *timer = module->user_data; - s3c2410_offset_t *reg; - int i; - + s3c2410_timer_t* timer = module->user_data; + s3c2410_offset_t* reg; + int i; + #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < timer->nr_regs; i++) { - reg = &timer->regs[i]; + for ( i = 0; i < timer->nr_regs; i++ ) { + reg = &timer->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - s3c2410_update_tcon(timer); - s3c2410_update_tcfg(timer); + s3c2410_update_tcon( timer ); + s3c2410_update_tcfg( timer ); - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_timer_readfn[] = -{ - s3c2410_timer_read, - s3c2410_timer_read, - s3c2410_timer_read -}; +static CPUReadMemoryFunc* s3c2410_timer_readfn[] = { s3c2410_timer_read, s3c2410_timer_read, s3c2410_timer_read }; -static CPUWriteMemoryFunc *s3c2410_timer_writefn[] = -{ - s3c2410_timer_write, - s3c2410_timer_write, - s3c2410_timer_write -}; +static CPUWriteMemoryFunc* s3c2410_timer_writefn[] = { s3c2410_timer_write, s3c2410_timer_write, s3c2410_timer_write }; -static int -s3c2410_timer_init(x49gp_module_t *module) +static int s3c2410_timer_init( x49gp_module_t* module ) { - s3c2410_timer_t *timer; - struct s3c2410_timeout *t; - int iotype; - int i; + s3c2410_timer_t* timer; + struct s3c2410_timeout* t; + int iotype; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - timer = malloc(sizeof(s3c2410_timer_t)); - if (NULL == timer) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->x49gp->progname, __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_timer_data_init(timer)) { - free(timer); - return -ENOMEM; - } + timer = malloc( sizeof( s3c2410_timer_t ) ); + if ( NULL == timer ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->x49gp->progname, __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_timer_data_init( timer ) ) { + free( timer ); + return -ENOMEM; + } - module->user_data = timer; + module->user_data = timer; - timer->x49gp = module->x49gp; - module->x49gp->s3c2410_timer = timer; + timer->x49gp = module->x49gp; + module->x49gp->s3c2410_timer = timer; - for (i = 0; i < 5; i++) { - t = &timer->timeout[i]; + for ( i = 0; i < 5; i++ ) { + t = &timer->timeout[ i ]; - t->tconfig = &s3c2410_timer_config[i]; - t->index = i; + t->tconfig = &s3c2410_timer_config[ i ]; + t->index = i; - t->main = timer; + t->main = timer; - t->timer = x49gp_new_timer(X49GP_TIMER_VIRTUAL, s3c2410_timer_timeout, t); - } + t->timer = x49gp_new_timer( X49GP_TIMER_VIRTUAL, s3c2410_timer_timeout, t ); + } - iotype = cpu_register_io_memory(s3c2410_timer_readfn, - s3c2410_timer_writefn, timer); + iotype = cpu_register_io_memory( s3c2410_timer_readfn, s3c2410_timer_writefn, timer ); #ifdef DEBUG_S3C2410_TIMER - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_TIMER_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_TIMER_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_timer_exit(x49gp_module_t *module) +static int s3c2410_timer_exit( x49gp_module_t* module ) { - s3c2410_timer_t *timer; + s3c2410_timer_t* timer; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - timer = module->user_data; - if (timer->regs) - free(timer->regs); - free(timer); - } + if ( module->user_data ) { + timer = module->user_data; + if ( timer->regs ) + free( timer->regs ); + free( timer ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_timer_init(x49gp_t *x49gp) +int x49gp_s3c2410_timer_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-timer", - s3c2410_timer_init, - s3c2410_timer_exit, - s3c2410_timer_reset, - s3c2410_timer_load, - s3c2410_timer_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-timer", s3c2410_timer_init, s3c2410_timer_exit, s3c2410_timer_reset, s3c2410_timer_load, + s3c2410_timer_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_timer.h b/src/s3c2410_timer.h index 9ad6d6d..5510258 100644 --- a/src/s3c2410_timer.h +++ b/src/s3c2410_timer.h @@ -6,43 +6,43 @@ #include "x49gp_types.h" -#define TCFG0_DEAD_SHIFT 16 -#define TCFG0_DEAD_MASK 0xff -#define TCFG0_PRE1_SHIFT 8 -#define TCFG0_PRE0_SHIFT 0 -#define TCFG0_PREx_MASK 0xff +#define TCFG0_DEAD_SHIFT 16 +#define TCFG0_DEAD_MASK 0xff +#define TCFG0_PRE1_SHIFT 8 +#define TCFG0_PRE0_SHIFT 0 +#define TCFG0_PREx_MASK 0xff -#define TCFG1_DMA_SHIFT 20 -#define TCFG1_DMA_MASK 0x0f -#define TCFG1_MUX4_SHIFT 16 -#define TCFG1_MUX3_SHIFT 12 -#define TCFG1_MUX2_SHIFT 8 -#define TCFG1_MUX1_SHIFT 4 -#define TCFG1_MUX0_SHIFT 0 -#define TCFG1_MUXx_MASK 0x0f +#define TCFG1_DMA_SHIFT 20 +#define TCFG1_DMA_MASK 0x0f +#define TCFG1_MUX4_SHIFT 16 +#define TCFG1_MUX3_SHIFT 12 +#define TCFG1_MUX2_SHIFT 8 +#define TCFG1_MUX1_SHIFT 4 +#define TCFG1_MUX0_SHIFT 0 +#define TCFG1_MUXx_MASK 0x0f -#define TCON_TIMER4_RELOAD 0x00400000 -#define TCON_TIMER4_UPDATE 0x00200000 -#define TCON_TIMER4_START 0x00100000 -#define TCON_TIMER3_RELOAD 0x00080000 -#define TCON_TIMER3_INVERT 0x00040000 -#define TCON_TIMER3_UPDATE 0x00020000 -#define TCON_TIMER3_START 0x00010000 -#define TCON_TIMER2_RELOAD 0x00008000 -#define TCON_TIMER2_INVERT 0x00004000 -#define TCON_TIMER2_UPDATE 0x00002000 -#define TCON_TIMER2_START 0x00001000 -#define TCON_TIMER1_RELOAD 0x00000800 -#define TCON_TIMER1_INVERT 0x00000400 -#define TCON_TIMER1_UPDATE 0x00000200 -#define TCON_TIMER1_START 0x00000100 -#define TCON_TIMER0_DEADZONE 0x00000010 -#define TCON_TIMER0_RELOAD 0x00000008 -#define TCON_TIMER0_INVERT 0x00000004 -#define TCON_TIMER0_UPDATE 0x00000002 -#define TCON_TIMER0_START 0x00000001 +#define TCON_TIMER4_RELOAD 0x00400000 +#define TCON_TIMER4_UPDATE 0x00200000 +#define TCON_TIMER4_START 0x00100000 +#define TCON_TIMER3_RELOAD 0x00080000 +#define TCON_TIMER3_INVERT 0x00040000 +#define TCON_TIMER3_UPDATE 0x00020000 +#define TCON_TIMER3_START 0x00010000 +#define TCON_TIMER2_RELOAD 0x00008000 +#define TCON_TIMER2_INVERT 0x00004000 +#define TCON_TIMER2_UPDATE 0x00002000 +#define TCON_TIMER2_START 0x00001000 +#define TCON_TIMER1_RELOAD 0x00000800 +#define TCON_TIMER1_INVERT 0x00000400 +#define TCON_TIMER1_UPDATE 0x00000200 +#define TCON_TIMER1_START 0x00000100 +#define TCON_TIMER0_DEADZONE 0x00000010 +#define TCON_TIMER0_RELOAD 0x00000008 +#define TCON_TIMER0_INVERT 0x00000004 +#define TCON_TIMER0_UPDATE 0x00000002 +#define TCON_TIMER0_START 0x00000001 -void s3c2410_run_timers(x49gp_t *x49gp); -clock_t s3c2410_next_timer(x49gp_t *x49gp); +void s3c2410_run_timers( x49gp_t* x49gp ); +clock_t s3c2410_next_timer( x49gp_t* x49gp ); #endif /* !(_S3C2410_TIMER_H) */ diff --git a/src/s3c2410_uart.c b/src/s3c2410_uart.c index 51c230e..6d241b4 100644 --- a/src/s3c2410_uart.c +++ b/src/s3c2410_uart.c @@ -13,466 +13,415 @@ #include "s3c2410.h" #include "s3c2410_intc.h" - typedef struct { - uint32_t ulcon; - uint32_t ucon; - uint32_t ufcon; - uint32_t umcon; - uint32_t utrstat; - uint32_t uerstat; - uint32_t ufstat; - uint32_t umstat; - uint32_t utxh; - uint32_t urxh; - uint32_t ubrdiv; + uint32_t ulcon; + uint32_t ucon; + uint32_t ufcon; + uint32_t umcon; + uint32_t utrstat; + uint32_t uerstat; + uint32_t ufstat; + uint32_t umstat; + uint32_t utxh; + uint32_t urxh; + uint32_t ubrdiv; - int int_err; - int int_txd; - int int_rxd; + int int_err; + int int_txd; + int int_rxd; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; } s3c2410_uart_reg_t; typedef struct { - s3c2410_uart_reg_t uart[3]; + s3c2410_uart_reg_t uart[ 3 ]; } s3c2410_uart_t; -static int -s3c2410_uart_data_init(s3c2410_uart_t *uart) +static int s3c2410_uart_data_init( s3c2410_uart_t* uart ) { - s3c2410_offset_t regs0[] = { - S3C2410_OFFSET(UART0, ULCON, 0x00000000, uart->uart[0].ulcon), - S3C2410_OFFSET(UART0, UCON, 0x00000000, uart->uart[0].ucon), - S3C2410_OFFSET(UART0, UFCON, 0x00000000, uart->uart[0].ufcon), - S3C2410_OFFSET(UART0, UMCON, 0x00000000, uart->uart[0].umcon), - S3C2410_OFFSET(UART0, UTRSTAT, 0x00000006, uart->uart[0].utrstat), - S3C2410_OFFSET(UART0, UERSTAT, 0x00000000, uart->uart[0].uerstat), - S3C2410_OFFSET(UART0, UFSTAT, 0x00000000, uart->uart[0].ufstat), - S3C2410_OFFSET(UART0, UMSTAT, 0x00000000, uart->uart[0].umstat), - S3C2410_OFFSET(UART0, UTXH, 0, uart->uart[0].utxh), - S3C2410_OFFSET(UART0, URXH, 0, uart->uart[0].urxh), - S3C2410_OFFSET(UART0, UBRDIV, 0, uart->uart[0].ubrdiv) - }; - s3c2410_offset_t regs1[] = { - S3C2410_OFFSET(UART1, ULCON, 0x00000000, uart->uart[1].ulcon), - S3C2410_OFFSET(UART1, UCON, 0x00000000, uart->uart[1].ucon), - S3C2410_OFFSET(UART1, UFCON, 0x00000000, uart->uart[1].ufcon), - S3C2410_OFFSET(UART1, UMCON, 0x00000000, uart->uart[1].umcon), - S3C2410_OFFSET(UART1, UTRSTAT, 0x00000006, uart->uart[1].utrstat), - S3C2410_OFFSET(UART1, UERSTAT, 0x00000000, uart->uart[1].uerstat), - S3C2410_OFFSET(UART1, UFSTAT, 0x00000000, uart->uart[1].ufstat), - S3C2410_OFFSET(UART1, UMSTAT, 0x00000000, uart->uart[1].umstat), - S3C2410_OFFSET(UART1, UTXH, 0, uart->uart[1].utxh), - S3C2410_OFFSET(UART1, URXH, 0, uart->uart[1].urxh), - S3C2410_OFFSET(UART1, UBRDIV, 0, uart->uart[1].ubrdiv) - }; - s3c2410_offset_t regs2[] = { - S3C2410_OFFSET(UART2, ULCON, 0x00000000, uart->uart[2].ulcon), - S3C2410_OFFSET(UART2, UCON, 0x00000000, uart->uart[2].ucon), - S3C2410_OFFSET(UART2, UFCON, 0x00000000, uart->uart[2].ufcon), - S3C2410_OFFSET(UART2, UTRSTAT, 0x00000006, uart->uart[2].utrstat), - S3C2410_OFFSET(UART2, UERSTAT, 0x00000000, uart->uart[2].uerstat), - S3C2410_OFFSET(UART2, UFSTAT, 0x00000000, uart->uart[2].ufstat), - S3C2410_OFFSET(UART2, UTXH, 0, uart->uart[2].utxh), - S3C2410_OFFSET(UART2, URXH, 0, uart->uart[2].urxh), - S3C2410_OFFSET(UART2, UBRDIV, 0, uart->uart[2].ubrdiv) - }; + s3c2410_offset_t regs0[] = { S3C2410_OFFSET( UART0, ULCON, 0x00000000, uart->uart[ 0 ].ulcon ), + S3C2410_OFFSET( UART0, UCON, 0x00000000, uart->uart[ 0 ].ucon ), + S3C2410_OFFSET( UART0, UFCON, 0x00000000, uart->uart[ 0 ].ufcon ), + S3C2410_OFFSET( UART0, UMCON, 0x00000000, uart->uart[ 0 ].umcon ), + S3C2410_OFFSET( UART0, UTRSTAT, 0x00000006, uart->uart[ 0 ].utrstat ), + S3C2410_OFFSET( UART0, UERSTAT, 0x00000000, uart->uart[ 0 ].uerstat ), + S3C2410_OFFSET( UART0, UFSTAT, 0x00000000, uart->uart[ 0 ].ufstat ), + S3C2410_OFFSET( UART0, UMSTAT, 0x00000000, uart->uart[ 0 ].umstat ), + S3C2410_OFFSET( UART0, UTXH, 0, uart->uart[ 0 ].utxh ), + S3C2410_OFFSET( UART0, URXH, 0, uart->uart[ 0 ].urxh ), + S3C2410_OFFSET( UART0, UBRDIV, 0, uart->uart[ 0 ].ubrdiv ) }; + s3c2410_offset_t regs1[] = { S3C2410_OFFSET( UART1, ULCON, 0x00000000, uart->uart[ 1 ].ulcon ), + S3C2410_OFFSET( UART1, UCON, 0x00000000, uart->uart[ 1 ].ucon ), + S3C2410_OFFSET( UART1, UFCON, 0x00000000, uart->uart[ 1 ].ufcon ), + S3C2410_OFFSET( UART1, UMCON, 0x00000000, uart->uart[ 1 ].umcon ), + S3C2410_OFFSET( UART1, UTRSTAT, 0x00000006, uart->uart[ 1 ].utrstat ), + S3C2410_OFFSET( UART1, UERSTAT, 0x00000000, uart->uart[ 1 ].uerstat ), + S3C2410_OFFSET( UART1, UFSTAT, 0x00000000, uart->uart[ 1 ].ufstat ), + S3C2410_OFFSET( UART1, UMSTAT, 0x00000000, uart->uart[ 1 ].umstat ), + S3C2410_OFFSET( UART1, UTXH, 0, uart->uart[ 1 ].utxh ), + S3C2410_OFFSET( UART1, URXH, 0, uart->uart[ 1 ].urxh ), + S3C2410_OFFSET( UART1, UBRDIV, 0, uart->uart[ 1 ].ubrdiv ) }; + s3c2410_offset_t regs2[] = { S3C2410_OFFSET( UART2, ULCON, 0x00000000, uart->uart[ 2 ].ulcon ), + S3C2410_OFFSET( UART2, UCON, 0x00000000, uart->uart[ 2 ].ucon ), + S3C2410_OFFSET( UART2, UFCON, 0x00000000, uart->uart[ 2 ].ufcon ), + S3C2410_OFFSET( UART2, UTRSTAT, 0x00000006, uart->uart[ 2 ].utrstat ), + S3C2410_OFFSET( UART2, UERSTAT, 0x00000000, uart->uart[ 2 ].uerstat ), + S3C2410_OFFSET( UART2, UFSTAT, 0x00000000, uart->uart[ 2 ].ufstat ), + S3C2410_OFFSET( UART2, UTXH, 0, uart->uart[ 2 ].utxh ), + S3C2410_OFFSET( UART2, URXH, 0, uart->uart[ 2 ].urxh ), + S3C2410_OFFSET( UART2, UBRDIV, 0, uart->uart[ 2 ].ubrdiv ) }; - uart->uart[0].regs = malloc(sizeof(regs0)); - if (NULL == uart->uart[0].regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - uart->uart[1].regs = malloc(sizeof(regs1)); - if (NULL == uart->uart[1].regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - free(uart->uart[0].regs); - return -ENOMEM; - } - uart->uart[2].regs = malloc(sizeof(regs2)); - if (NULL == uart->uart[2].regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - free(uart->uart[0].regs); - free(uart->uart[1].regs); - return -ENOMEM; - } + uart->uart[ 0 ].regs = malloc( sizeof( regs0 ) ); + if ( NULL == uart->uart[ 0 ].regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + uart->uart[ 1 ].regs = malloc( sizeof( regs1 ) ); + if ( NULL == uart->uart[ 1 ].regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + free( uart->uart[ 0 ].regs ); + return -ENOMEM; + } + uart->uart[ 2 ].regs = malloc( sizeof( regs2 ) ); + if ( NULL == uart->uart[ 2 ].regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + free( uart->uart[ 0 ].regs ); + free( uart->uart[ 1 ].regs ); + return -ENOMEM; + } - memcpy(uart->uart[0].regs, regs0, sizeof(regs0)); - uart->uart[0].nr_regs = sizeof(regs0) / sizeof(regs0[0]); - uart->uart[0].int_err = SUB_INT_ERR0; - uart->uart[0].int_txd = SUB_INT_TXD0; - uart->uart[0].int_rxd = SUB_INT_RXD0; + memcpy( uart->uart[ 0 ].regs, regs0, sizeof( regs0 ) ); + uart->uart[ 0 ].nr_regs = sizeof( regs0 ) / sizeof( regs0[ 0 ] ); + uart->uart[ 0 ].int_err = SUB_INT_ERR0; + uart->uart[ 0 ].int_txd = SUB_INT_TXD0; + uart->uart[ 0 ].int_rxd = SUB_INT_RXD0; - memcpy(uart->uart[1].regs, regs1, sizeof(regs1)); - uart->uart[1].nr_regs = sizeof(regs1) / sizeof(regs1[0]); - uart->uart[1].int_err = SUB_INT_ERR1; - uart->uart[1].int_txd = SUB_INT_TXD1; - uart->uart[1].int_rxd = SUB_INT_RXD1; + memcpy( uart->uart[ 1 ].regs, regs1, sizeof( regs1 ) ); + uart->uart[ 1 ].nr_regs = sizeof( regs1 ) / sizeof( regs1[ 0 ] ); + uart->uart[ 1 ].int_err = SUB_INT_ERR1; + uart->uart[ 1 ].int_txd = SUB_INT_TXD1; + uart->uart[ 1 ].int_rxd = SUB_INT_RXD1; - memcpy(uart->uart[2].regs, regs2, sizeof(regs2)); - uart->uart[2].nr_regs = sizeof(regs2) / sizeof(regs2[0]); - uart->uart[2].int_err = SUB_INT_ERR2; - uart->uart[2].int_txd = SUB_INT_TXD2; - uart->uart[2].int_rxd = SUB_INT_RXD2; + memcpy( uart->uart[ 2 ].regs, regs2, sizeof( regs2 ) ); + uart->uart[ 2 ].nr_regs = sizeof( regs2 ) / sizeof( regs2[ 0 ] ); + uart->uart[ 2 ].int_err = SUB_INT_ERR2; + uart->uart[ 2 ].int_txd = SUB_INT_TXD2; + uart->uart[ 2 ].int_rxd = SUB_INT_RXD2; - return 0; + return 0; } -static uint32_t -s3c2410_uart_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_uart_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_uart_reg_t *uart_regs = opaque; - x49gp_t *x49gp = uart_regs->x49gp; - s3c2410_offset_t *reg; + s3c2410_uart_reg_t* uart_regs = opaque; + x49gp_t* x49gp = uart_regs->x49gp; + s3c2410_offset_t* reg; #ifdef DEBUG_S3C2410_UART - const char *module; - uint32_t mod_offset; - uint32_t base; + const char* module; + uint32_t mod_offset; + uint32_t base; - base = (offset & 0x0000c000) >> 14; + base = ( offset & 0x0000c000 ) >> 14; - switch (base) { - case 0: - module = "s3c2410-uart0"; - mod_offset = S3C2410_UART0_BASE; - break; - case 1: - module = "s3c2410-uart1"; - mod_offset = S3C2410_UART1_BASE; - break; - case 2: - module = "s3c2410-uart2"; - mod_offset = S3C2410_UART2_BASE; - break; - default: - return ~(0); - } + switch ( base ) { + case 0: + module = "s3c2410-uart0"; + mod_offset = S3C2410_UART0_BASE; + break; + case 1: + module = "s3c2410-uart1"; + mod_offset = S3C2410_UART1_BASE; + break; + case 2: + module = "s3c2410-uart2"; + mod_offset = S3C2410_UART2_BASE; + break; + default: + return ~( 0 ); + } #endif - offset &= ~(0xffffc000); - if (! S3C2410_OFFSET_OK(uart_regs, offset)) { - return ~(0); - } + offset &= ~( 0xffffc000 ); + if ( !S3C2410_OFFSET_OK( uart_regs, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(uart_regs, offset); + reg = S3C2410_OFFSET_ENTRY( uart_regs, offset ); #ifdef DEBUG_S3C2410_UART - printf("read %s [%08x] %s [%08lx] data %08x\n", - module, mod_offset, reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", module, mod_offset, reg->name, ( unsigned long )offset, *( reg->datap ) ); #endif - switch (offset) { - case S3C2410_UART0_URXH: - uart_regs->utrstat &= ~(1 << 0); + switch ( offset ) { + case S3C2410_UART0_URXH: + uart_regs->utrstat &= ~( 1 << 0 ); - if (uart_regs->ucon & (1 << 8)) { - s3c2410_intc_sub_deassert(x49gp, uart_regs->int_rxd); - } + if ( uart_regs->ucon & ( 1 << 8 ) ) { + s3c2410_intc_sub_deassert( x49gp, uart_regs->int_rxd ); + } - break; - } + break; + } - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_uart_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_uart_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_uart_reg_t *uart_regs = opaque; - x49gp_t *x49gp = uart_regs->x49gp; - s3c2410_offset_t *reg; - uint32_t base; + s3c2410_uart_reg_t* uart_regs = opaque; + x49gp_t* x49gp = uart_regs->x49gp; + s3c2410_offset_t* reg; + uint32_t base; #ifdef DEBUG_S3C2410_UART - const char *module; - uint32_t mod_offset, ubrdivn, baud; + const char* module; + uint32_t mod_offset, ubrdivn, baud; #endif - base = (offset & 0x0000c000) >> 14; + base = ( offset & 0x0000c000 ) >> 14; #ifdef DEBUG_S3C2410_UART - switch (base) { - case 0: - module = "s3c2410-uart0"; - mod_offset = S3C2410_UART0_BASE; - break; - case 1: - module = "s3c2410-uart1"; - mod_offset = S3C2410_UART1_BASE; - break; - case 2: - module = "s3c2410-uart2"; - mod_offset = S3C2410_UART2_BASE; - break; - default: - return; - } + switch ( base ) { + case 0: + module = "s3c2410-uart0"; + mod_offset = S3C2410_UART0_BASE; + break; + case 1: + module = "s3c2410-uart1"; + mod_offset = S3C2410_UART1_BASE; + break; + case 2: + module = "s3c2410-uart2"; + mod_offset = S3C2410_UART2_BASE; + break; + default: + return; + } #endif - offset &= ~(0xffffc000); - if (! S3C2410_OFFSET_OK(uart_regs, offset)) { - return; - } + offset &= ~( 0xffffc000 ); + if ( !S3C2410_OFFSET_OK( uart_regs, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(uart_regs, offset); + reg = S3C2410_OFFSET_ENTRY( uart_regs, offset ); #ifdef DEBUG_S3C2410_UART - printf("write %s [%08x] %s [%08lx] data %08x\n", - module, mod_offset, reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", module, mod_offset, reg->name, ( unsigned long )offset, data ); #endif - *(reg->datap) = data; + *( reg->datap ) = data; - switch (offset) { - case S3C2410_UART0_UCON: - if (*(reg->datap) & (1 << 9)) - s3c2410_intc_sub_assert(x49gp, uart_regs->int_txd, 1); - if (*(reg->datap) & (1 << 8)) - s3c2410_intc_sub_deassert(x49gp, uart_regs->int_rxd); - break; + switch ( offset ) { + case S3C2410_UART0_UCON: + if ( *( reg->datap ) & ( 1 << 9 ) ) + s3c2410_intc_sub_assert( x49gp, uart_regs->int_txd, 1 ); + if ( *( reg->datap ) & ( 1 << 8 ) ) + s3c2410_intc_sub_deassert( x49gp, uart_regs->int_rxd ); + break; - case S3C2410_UART0_UBRDIV: + case S3C2410_UART0_UBRDIV: #ifdef DEBUG_S3C2410_UART - ubrdivn = (data >> 0) & 0xffff; - if (uart_regs->ucon & (1 << 10)) { - baud = x49gp->UCLK / 16 / (ubrdivn + 1); - printf("%s: UEXTCLK %u, ubrdivn %u, baud %u\n", - module, x49gp->UCLK, ubrdivn, baud); - } else { - baud = x49gp->PCLK / 16 / (ubrdivn + 1); - printf("%s: PCLK %u, ubrdivn %u, baud %u\n", - module, x49gp->PCLK, ubrdivn, baud); - } + ubrdivn = ( data >> 0 ) & 0xffff; + if ( uart_regs->ucon & ( 1 << 10 ) ) { + baud = x49gp->UCLK / 16 / ( ubrdivn + 1 ); + printf( "%s: UEXTCLK %u, ubrdivn %u, baud %u\n", module, x49gp->UCLK, ubrdivn, baud ); + } else { + baud = x49gp->PCLK / 16 / ( ubrdivn + 1 ); + printf( "%s: PCLK %u, ubrdivn %u, baud %u\n", module, x49gp->PCLK, ubrdivn, baud ); + } #endif - break; + break; - case S3C2410_UART0_UTXH: - if (uart_regs->ucon & (1 << 9)) - s3c2410_intc_sub_deassert(x49gp, uart_regs->int_txd); + case S3C2410_UART0_UTXH: + if ( uart_regs->ucon & ( 1 << 9 ) ) + s3c2410_intc_sub_deassert( x49gp, uart_regs->int_txd ); - uart_regs->utrstat |= (1 << 2) | (1 << 1); + uart_regs->utrstat |= ( 1 << 2 ) | ( 1 << 1 ); - if (uart_regs->ucon & (1 << 9)) - s3c2410_intc_sub_assert(x49gp, uart_regs->int_txd, 1); - else - s3c2410_intc_sub_assert(x49gp, uart_regs->int_txd, 0); + if ( uart_regs->ucon & ( 1 << 9 ) ) + s3c2410_intc_sub_assert( x49gp, uart_regs->int_txd, 1 ); + else + s3c2410_intc_sub_assert( x49gp, uart_regs->int_txd, 0 ); - if (uart_regs->ucon & (1 << 5)) { - uart_regs->urxh = data; - uart_regs->utrstat |= (1 << 0); + if ( uart_regs->ucon & ( 1 << 5 ) ) { + uart_regs->urxh = data; + uart_regs->utrstat |= ( 1 << 0 ); - if (uart_regs->ucon & (1 << 8)) - s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 1); - else - s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 0); - } else if (base == 2) { - uart_regs->urxh = data; - uart_regs->utrstat |= (1 << 0); + if ( uart_regs->ucon & ( 1 << 8 ) ) + s3c2410_intc_sub_assert( x49gp, uart_regs->int_rxd, 1 ); + else + s3c2410_intc_sub_assert( x49gp, uart_regs->int_rxd, 0 ); + } else if ( base == 2 ) { + uart_regs->urxh = data; + uart_regs->utrstat |= ( 1 << 0 ); - if (uart_regs->ucon & (1 << 8)) - s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 1); - else - s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 0); - } + if ( uart_regs->ucon & ( 1 << 8 ) ) + s3c2410_intc_sub_assert( x49gp, uart_regs->int_rxd, 1 ); + else + s3c2410_intc_sub_assert( x49gp, uart_regs->int_rxd, 0 ); + } - break; - } + break; + } } -static int -s3c2410_uart_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_uart_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_uart_reg_t *uart_regs = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_uart_reg_t* uart_regs = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < uart_regs->nr_regs; i++) { - reg = &uart_regs->regs[i]; + for ( i = 0; i < uart_regs->nr_regs; i++ ) { + reg = &uart_regs->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_uart_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_uart_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_uart_reg_t *uart_regs = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_uart_reg_t* uart_regs = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < uart_regs->nr_regs; i++) { - reg = &uart_regs->regs[i]; + for ( i = 0; i < uart_regs->nr_regs; i++ ) { + reg = &uart_regs->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_uart_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_uart_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_uart_reg_t *uart_regs = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_uart_reg_t* uart_regs = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < uart_regs->nr_regs; i++) { - reg = &uart_regs->regs[i]; + for ( i = 0; i < uart_regs->nr_regs; i++ ) { + reg = &uart_regs->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_uart_readfn[] = -{ - s3c2410_uart_read, - s3c2410_uart_read, - s3c2410_uart_read -}; +static CPUReadMemoryFunc* s3c2410_uart_readfn[] = { s3c2410_uart_read, s3c2410_uart_read, s3c2410_uart_read }; -static CPUWriteMemoryFunc *s3c2410_uart_writefn[] = -{ - s3c2410_uart_write, - s3c2410_uart_write, - s3c2410_uart_write -}; +static CPUWriteMemoryFunc* s3c2410_uart_writefn[] = { s3c2410_uart_write, s3c2410_uart_write, s3c2410_uart_write }; -static int -s3c2410_uart_init(x49gp_module_t *module) +static int s3c2410_uart_init( x49gp_module_t* module ) { - s3c2410_uart_reg_t *uart_regs = module->user_data; - int iotype; + s3c2410_uart_reg_t* uart_regs = module->user_data; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - iotype = cpu_register_io_memory(s3c2410_uart_readfn, - s3c2410_uart_writefn, uart_regs); + iotype = cpu_register_io_memory( s3c2410_uart_readfn, s3c2410_uart_writefn, uart_regs ); #ifdef DEBUG_S3C2410_UART - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_UART0_BASE, S3C2410_MAP_SIZE, iotype); + cpu_register_physical_memory( S3C2410_UART0_BASE, S3C2410_MAP_SIZE, iotype ); - return 0; + return 0; } -static int -s3c2410_uart_exit(x49gp_module_t *module) +static int s3c2410_uart_exit( x49gp_module_t* module ) { - s3c2410_uart_reg_t *uart_regs; + s3c2410_uart_reg_t* uart_regs; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - uart_regs = module->user_data; - if (uart_regs->regs) - free(uart_regs->regs); - } + if ( module->user_data ) { + uart_regs = module->user_data; + if ( uart_regs->regs ) + free( uart_regs->regs ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_uart_init(x49gp_t *x49gp) +int x49gp_s3c2410_uart_init( x49gp_t* x49gp ) { - s3c2410_uart_t *uart; - x49gp_module_t *module; + s3c2410_uart_t* uart; + x49gp_module_t* module; - uart = malloc(sizeof(s3c2410_uart_t)); - if (NULL == uart) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - memset(uart, 0, sizeof(s3c2410_uart_t)); + uart = malloc( sizeof( s3c2410_uart_t ) ); + if ( NULL == uart ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + memset( uart, 0, sizeof( s3c2410_uart_t ) ); - if (s3c2410_uart_data_init(uart)) { - free(uart); - return -ENOMEM; - } + if ( s3c2410_uart_data_init( uart ) ) { + free( uart ); + return -ENOMEM; + } - uart->uart[0].x49gp = x49gp; - uart->uart[1].x49gp = x49gp; - uart->uart[2].x49gp = x49gp; + uart->uart[ 0 ].x49gp = x49gp; + uart->uart[ 1 ].x49gp = x49gp; + uart->uart[ 2 ].x49gp = x49gp; - if (x49gp_module_init(x49gp, "s3c2410-uart0", - s3c2410_uart_init, - s3c2410_uart_exit, - s3c2410_uart_reset, - s3c2410_uart_load, - s3c2410_uart_save, - &uart->uart[0], &module)) { - return -1; - } - if (x49gp_module_register(module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-uart0", s3c2410_uart_init, s3c2410_uart_exit, s3c2410_uart_reset, s3c2410_uart_load, + s3c2410_uart_save, &uart->uart[ 0 ], &module ) ) { + return -1; + } + if ( x49gp_module_register( module ) ) { + return -1; + } - if (x49gp_module_init(x49gp, "s3c2410-uart1", - s3c2410_uart_init, - s3c2410_uart_exit, - s3c2410_uart_reset, - s3c2410_uart_load, - s3c2410_uart_save, - &uart->uart[1], &module)) { - return -1; - } - if (x49gp_module_register(module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-uart1", s3c2410_uart_init, s3c2410_uart_exit, s3c2410_uart_reset, s3c2410_uart_load, + s3c2410_uart_save, &uart->uart[ 1 ], &module ) ) { + return -1; + } + if ( x49gp_module_register( module ) ) { + return -1; + } - if (x49gp_module_init(x49gp, "s3c2410-uart2", - s3c2410_uart_init, - s3c2410_uart_exit, - s3c2410_uart_reset, - s3c2410_uart_load, - s3c2410_uart_save, - &uart->uart[2], &module)) { - return -1; - } - if (x49gp_module_register(module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-uart2", s3c2410_uart_init, s3c2410_uart_exit, s3c2410_uart_reset, s3c2410_uart_load, + s3c2410_uart_save, &uart->uart[ 2 ], &module ) ) { + return -1; + } + if ( x49gp_module_register( module ) ) { + return -1; + } - return 0; + return 0; } diff --git a/src/s3c2410_usbdev.c b/src/s3c2410_usbdev.c index 8796929..cc15179 100644 --- a/src/s3c2410_usbdev.c +++ b/src/s3c2410_usbdev.c @@ -12,322 +12,288 @@ #include "x49gp.h" #include "s3c2410.h" - typedef struct { - uint32_t func_addr_reg; - uint32_t pwr_reg; - uint32_t ep_int_reg; - uint32_t usb_int_reg; - uint32_t ep_int_en_reg; - uint32_t usb_int_en_reg; - uint32_t frame_num1_reg; - uint32_t frame_num2_reg; - uint32_t index_reg; - uint32_t ep0_fifo_reg; - uint32_t ep1_fifo_reg; - uint32_t ep2_fifo_reg; - uint32_t ep3_fifo_reg; - uint32_t ep4_fifo_reg; - uint32_t ep1_dma_con; - uint32_t ep1_dma_unit; - uint32_t ep1_dma_fifo; - uint32_t ep1_dma_ttc_l; - uint32_t ep1_dma_ttc_m; - uint32_t ep1_dma_ttc_h; - uint32_t ep2_dma_con; - uint32_t ep2_dma_unit; - uint32_t ep2_dma_fifo; - uint32_t ep2_dma_ttc_l; - uint32_t ep2_dma_ttc_m; - uint32_t ep2_dma_ttc_h; - uint32_t ep3_dma_con; - uint32_t ep3_dma_unit; - uint32_t ep3_dma_fifo; - uint32_t ep3_dma_ttc_l; - uint32_t ep3_dma_ttc_m; - uint32_t ep3_dma_ttc_h; - uint32_t ep4_dma_con; - uint32_t ep4_dma_unit; - uint32_t ep4_dma_fifo; - uint32_t ep4_dma_ttc_l; - uint32_t ep4_dma_ttc_m; - uint32_t ep4_dma_ttc_h; - uint32_t __wrong_maxp_reg; - uint32_t in_csr1_reg_ep0_csr; - uint32_t in_csr2_reg; - uint32_t maxp_reg; - uint32_t out_csr1_reg; - uint32_t out_csr2_reg; - uint32_t out_fifo_cnt1_reg; - uint32_t out_fifo_cnt2_reg; + uint32_t func_addr_reg; + uint32_t pwr_reg; + uint32_t ep_int_reg; + uint32_t usb_int_reg; + uint32_t ep_int_en_reg; + uint32_t usb_int_en_reg; + uint32_t frame_num1_reg; + uint32_t frame_num2_reg; + uint32_t index_reg; + uint32_t ep0_fifo_reg; + uint32_t ep1_fifo_reg; + uint32_t ep2_fifo_reg; + uint32_t ep3_fifo_reg; + uint32_t ep4_fifo_reg; + uint32_t ep1_dma_con; + uint32_t ep1_dma_unit; + uint32_t ep1_dma_fifo; + uint32_t ep1_dma_ttc_l; + uint32_t ep1_dma_ttc_m; + uint32_t ep1_dma_ttc_h; + uint32_t ep2_dma_con; + uint32_t ep2_dma_unit; + uint32_t ep2_dma_fifo; + uint32_t ep2_dma_ttc_l; + uint32_t ep2_dma_ttc_m; + uint32_t ep2_dma_ttc_h; + uint32_t ep3_dma_con; + uint32_t ep3_dma_unit; + uint32_t ep3_dma_fifo; + uint32_t ep3_dma_ttc_l; + uint32_t ep3_dma_ttc_m; + uint32_t ep3_dma_ttc_h; + uint32_t ep4_dma_con; + uint32_t ep4_dma_unit; + uint32_t ep4_dma_fifo; + uint32_t ep4_dma_ttc_l; + uint32_t ep4_dma_ttc_m; + uint32_t ep4_dma_ttc_h; + uint32_t __wrong_maxp_reg; + uint32_t in_csr1_reg_ep0_csr; + uint32_t in_csr2_reg; + uint32_t maxp_reg; + uint32_t out_csr1_reg; + uint32_t out_csr2_reg; + uint32_t out_fifo_cnt1_reg; + uint32_t out_fifo_cnt2_reg; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; } s3c2410_usbdev_t; -static int -s3c2410_usbdev_data_init(s3c2410_usbdev_t *usbdev) +static int s3c2410_usbdev_data_init( s3c2410_usbdev_t* usbdev ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(USBDEV, FUNC_ADDR_REG, 0x00, usbdev->func_addr_reg), - S3C2410_OFFSET(USBDEV, PWR_REG, 0x00, usbdev->pwr_reg), - S3C2410_OFFSET(USBDEV, EP_INT_REG, 0x00, usbdev->ep_int_reg), - S3C2410_OFFSET(USBDEV, USB_INT_REG, 0x00, usbdev->usb_int_reg), - S3C2410_OFFSET(USBDEV, EP_INT_EN_REG, 0xff, usbdev->ep_int_en_reg), - S3C2410_OFFSET(USBDEV, USB_INT_EN_REG, 0x04, usbdev->usb_int_en_reg), - S3C2410_OFFSET(USBDEV, FRAME_NUM1_REG, 0x00, usbdev->frame_num1_reg), - S3C2410_OFFSET(USBDEV, FRAME_NUM2_REG, 0x00, usbdev->frame_num2_reg), - S3C2410_OFFSET(USBDEV, INDEX_REG, 0x00, usbdev->index_reg), - S3C2410_OFFSET(USBDEV, EP0_FIFO_REG, 0, usbdev->ep0_fifo_reg), - S3C2410_OFFSET(USBDEV, EP1_FIFO_REG, 0, usbdev->ep1_fifo_reg), - S3C2410_OFFSET(USBDEV, EP2_FIFO_REG, 0, usbdev->ep2_fifo_reg), - S3C2410_OFFSET(USBDEV, EP3_FIFO_REG, 0, usbdev->ep3_fifo_reg), - S3C2410_OFFSET(USBDEV, EP4_FIFO_REG, 0, usbdev->ep4_fifo_reg), - S3C2410_OFFSET(USBDEV, EP1_DMA_CON, 0x00, usbdev->ep1_dma_con), - S3C2410_OFFSET(USBDEV, EP1_DMA_UNIT, 0x00, usbdev->ep1_dma_unit), - S3C2410_OFFSET(USBDEV, EP1_DMA_FIFO, 0x00, usbdev->ep1_dma_fifo), - S3C2410_OFFSET(USBDEV, EP1_DMA_TTC_L, 0x00, usbdev->ep1_dma_ttc_l), - S3C2410_OFFSET(USBDEV, EP1_DMA_TTC_M, 0x00, usbdev->ep1_dma_ttc_m), - S3C2410_OFFSET(USBDEV, EP1_DMA_TTC_H, 0x00, usbdev->ep1_dma_ttc_h), - S3C2410_OFFSET(USBDEV, EP2_DMA_CON, 0x00, usbdev->ep2_dma_con), - S3C2410_OFFSET(USBDEV, EP2_DMA_UNIT, 0x00, usbdev->ep2_dma_unit), - S3C2410_OFFSET(USBDEV, EP2_DMA_FIFO, 0x00, usbdev->ep2_dma_fifo), - S3C2410_OFFSET(USBDEV, EP2_DMA_TTC_L, 0x00, usbdev->ep2_dma_ttc_l), - S3C2410_OFFSET(USBDEV, EP2_DMA_TTC_M, 0x00, usbdev->ep2_dma_ttc_m), - S3C2410_OFFSET(USBDEV, EP2_DMA_TTC_H, 0x00, usbdev->ep2_dma_ttc_h), - S3C2410_OFFSET(USBDEV, EP3_DMA_CON, 0x00, usbdev->ep3_dma_con), - S3C2410_OFFSET(USBDEV, EP3_DMA_UNIT, 0x00, usbdev->ep3_dma_unit), - S3C2410_OFFSET(USBDEV, EP3_DMA_FIFO, 0x00, usbdev->ep3_dma_fifo), - S3C2410_OFFSET(USBDEV, EP3_DMA_TTC_L, 0x00, usbdev->ep3_dma_ttc_l), - S3C2410_OFFSET(USBDEV, EP3_DMA_TTC_M, 0x00, usbdev->ep3_dma_ttc_m), - S3C2410_OFFSET(USBDEV, EP3_DMA_TTC_H, 0x00, usbdev->ep3_dma_ttc_h), - S3C2410_OFFSET(USBDEV, EP4_DMA_CON, 0x00, usbdev->ep4_dma_con), - S3C2410_OFFSET(USBDEV, EP4_DMA_UNIT, 0x00, usbdev->ep4_dma_unit), - S3C2410_OFFSET(USBDEV, EP4_DMA_FIFO, 0x00, usbdev->ep4_dma_fifo), - S3C2410_OFFSET(USBDEV, EP4_DMA_TTC_L, 0x00, usbdev->ep4_dma_ttc_l), - S3C2410_OFFSET(USBDEV, EP4_DMA_TTC_M, 0x00, usbdev->ep4_dma_ttc_m), - S3C2410_OFFSET(USBDEV, EP4_DMA_TTC_H, 0x00, usbdev->ep4_dma_ttc_h), - S3C2410_OFFSET(USBDEV, MAXP_REG_WRONG, 0x01, usbdev->__wrong_maxp_reg), - S3C2410_OFFSET(USBDEV, IN_CSR1_REG_EP0_CSR, 0x00, usbdev->in_csr1_reg_ep0_csr), - S3C2410_OFFSET(USBDEV, IN_CSR2_REG, 0x20, usbdev->in_csr2_reg), - S3C2410_OFFSET(USBDEV, MAXP_REG, 0x01, usbdev->maxp_reg), - S3C2410_OFFSET(USBDEV, OUT_CSR1_REG, 0x00, usbdev->out_csr1_reg), - S3C2410_OFFSET(USBDEV, OUT_CSR2_REG, 0x00, usbdev->out_csr2_reg), - S3C2410_OFFSET(USBDEV, OUT_FIFO_CNT1_REG, 0x00, usbdev->out_fifo_cnt1_reg), - S3C2410_OFFSET(USBDEV, OUT_FIFO_CNT2_REG, 0x00, usbdev->out_fifo_cnt2_reg) - }; + s3c2410_offset_t regs[] = { S3C2410_OFFSET( USBDEV, FUNC_ADDR_REG, 0x00, usbdev->func_addr_reg ), + S3C2410_OFFSET( USBDEV, PWR_REG, 0x00, usbdev->pwr_reg ), + S3C2410_OFFSET( USBDEV, EP_INT_REG, 0x00, usbdev->ep_int_reg ), + S3C2410_OFFSET( USBDEV, USB_INT_REG, 0x00, usbdev->usb_int_reg ), + S3C2410_OFFSET( USBDEV, EP_INT_EN_REG, 0xff, usbdev->ep_int_en_reg ), + S3C2410_OFFSET( USBDEV, USB_INT_EN_REG, 0x04, usbdev->usb_int_en_reg ), + S3C2410_OFFSET( USBDEV, FRAME_NUM1_REG, 0x00, usbdev->frame_num1_reg ), + S3C2410_OFFSET( USBDEV, FRAME_NUM2_REG, 0x00, usbdev->frame_num2_reg ), + S3C2410_OFFSET( USBDEV, INDEX_REG, 0x00, usbdev->index_reg ), + S3C2410_OFFSET( USBDEV, EP0_FIFO_REG, 0, usbdev->ep0_fifo_reg ), + S3C2410_OFFSET( USBDEV, EP1_FIFO_REG, 0, usbdev->ep1_fifo_reg ), + S3C2410_OFFSET( USBDEV, EP2_FIFO_REG, 0, usbdev->ep2_fifo_reg ), + S3C2410_OFFSET( USBDEV, EP3_FIFO_REG, 0, usbdev->ep3_fifo_reg ), + S3C2410_OFFSET( USBDEV, EP4_FIFO_REG, 0, usbdev->ep4_fifo_reg ), + S3C2410_OFFSET( USBDEV, EP1_DMA_CON, 0x00, usbdev->ep1_dma_con ), + S3C2410_OFFSET( USBDEV, EP1_DMA_UNIT, 0x00, usbdev->ep1_dma_unit ), + S3C2410_OFFSET( USBDEV, EP1_DMA_FIFO, 0x00, usbdev->ep1_dma_fifo ), + S3C2410_OFFSET( USBDEV, EP1_DMA_TTC_L, 0x00, usbdev->ep1_dma_ttc_l ), + S3C2410_OFFSET( USBDEV, EP1_DMA_TTC_M, 0x00, usbdev->ep1_dma_ttc_m ), + S3C2410_OFFSET( USBDEV, EP1_DMA_TTC_H, 0x00, usbdev->ep1_dma_ttc_h ), + S3C2410_OFFSET( USBDEV, EP2_DMA_CON, 0x00, usbdev->ep2_dma_con ), + S3C2410_OFFSET( USBDEV, EP2_DMA_UNIT, 0x00, usbdev->ep2_dma_unit ), + S3C2410_OFFSET( USBDEV, EP2_DMA_FIFO, 0x00, usbdev->ep2_dma_fifo ), + S3C2410_OFFSET( USBDEV, EP2_DMA_TTC_L, 0x00, usbdev->ep2_dma_ttc_l ), + S3C2410_OFFSET( USBDEV, EP2_DMA_TTC_M, 0x00, usbdev->ep2_dma_ttc_m ), + S3C2410_OFFSET( USBDEV, EP2_DMA_TTC_H, 0x00, usbdev->ep2_dma_ttc_h ), + S3C2410_OFFSET( USBDEV, EP3_DMA_CON, 0x00, usbdev->ep3_dma_con ), + S3C2410_OFFSET( USBDEV, EP3_DMA_UNIT, 0x00, usbdev->ep3_dma_unit ), + S3C2410_OFFSET( USBDEV, EP3_DMA_FIFO, 0x00, usbdev->ep3_dma_fifo ), + S3C2410_OFFSET( USBDEV, EP3_DMA_TTC_L, 0x00, usbdev->ep3_dma_ttc_l ), + S3C2410_OFFSET( USBDEV, EP3_DMA_TTC_M, 0x00, usbdev->ep3_dma_ttc_m ), + S3C2410_OFFSET( USBDEV, EP3_DMA_TTC_H, 0x00, usbdev->ep3_dma_ttc_h ), + S3C2410_OFFSET( USBDEV, EP4_DMA_CON, 0x00, usbdev->ep4_dma_con ), + S3C2410_OFFSET( USBDEV, EP4_DMA_UNIT, 0x00, usbdev->ep4_dma_unit ), + S3C2410_OFFSET( USBDEV, EP4_DMA_FIFO, 0x00, usbdev->ep4_dma_fifo ), + S3C2410_OFFSET( USBDEV, EP4_DMA_TTC_L, 0x00, usbdev->ep4_dma_ttc_l ), + S3C2410_OFFSET( USBDEV, EP4_DMA_TTC_M, 0x00, usbdev->ep4_dma_ttc_m ), + S3C2410_OFFSET( USBDEV, EP4_DMA_TTC_H, 0x00, usbdev->ep4_dma_ttc_h ), + S3C2410_OFFSET( USBDEV, MAXP_REG_WRONG, 0x01, usbdev->__wrong_maxp_reg ), + S3C2410_OFFSET( USBDEV, IN_CSR1_REG_EP0_CSR, 0x00, usbdev->in_csr1_reg_ep0_csr ), + S3C2410_OFFSET( USBDEV, IN_CSR2_REG, 0x20, usbdev->in_csr2_reg ), + S3C2410_OFFSET( USBDEV, MAXP_REG, 0x01, usbdev->maxp_reg ), + S3C2410_OFFSET( USBDEV, OUT_CSR1_REG, 0x00, usbdev->out_csr1_reg ), + S3C2410_OFFSET( USBDEV, OUT_CSR2_REG, 0x00, usbdev->out_csr2_reg ), + S3C2410_OFFSET( USBDEV, OUT_FIFO_CNT1_REG, 0x00, usbdev->out_fifo_cnt1_reg ), + S3C2410_OFFSET( USBDEV, OUT_FIFO_CNT2_REG, 0x00, usbdev->out_fifo_cnt2_reg ) }; - memset(usbdev, 0, sizeof(s3c2410_usbdev_t)); + memset( usbdev, 0, sizeof( s3c2410_usbdev_t ) ); - usbdev->regs = malloc(sizeof(regs)); - if (NULL == usbdev->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + usbdev->regs = malloc( sizeof( regs ) ); + if ( NULL == usbdev->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(usbdev->regs, regs, sizeof(regs)); - usbdev->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( usbdev->regs, regs, sizeof( regs ) ); + usbdev->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static uint32_t -s3c2410_usbdev_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_usbdev_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_usbdev_t *usbdev = opaque; - s3c2410_offset_t *reg; + s3c2410_usbdev_t* usbdev = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(usbdev, offset)) { - return ~(0); - } + if ( !S3C2410_OFFSET_OK( usbdev, offset ) ) { + return ~( 0 ); + } - reg = S3C2410_OFFSET_ENTRY(usbdev, offset); + reg = S3C2410_OFFSET_ENTRY( usbdev, offset ); #ifdef DEBUG_S3C2410_USBDEV - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-usbdev", S3C2410_USBDEV_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-usbdev", S3C2410_USBDEV_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_usbdev_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_usbdev_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_usbdev_t *usbdev = opaque; - s3c2410_offset_t *reg; + s3c2410_usbdev_t* usbdev = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(usbdev, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( usbdev, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(usbdev, offset); + reg = S3C2410_OFFSET_ENTRY( usbdev, offset ); #ifdef DEBUG_S3C2410_USBDEV - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-usbdev", S3C2410_USBDEV_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-usbdev", S3C2410_USBDEV_BASE, reg->name, ( unsigned long )offset, data ); #endif - *(reg->datap) = data; + *( reg->datap ) = data; } -static int -s3c2410_usbdev_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_usbdev_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_usbdev_t *usbdev = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_usbdev_t* usbdev = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < usbdev->nr_regs; i++) { - reg = &usbdev->regs[i]; + for ( i = 0; i < usbdev->nr_regs; i++ ) { + reg = &usbdev->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - return error; + return error; } -static int -s3c2410_usbdev_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_usbdev_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_usbdev_t *usbdev = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_usbdev_t* usbdev = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < usbdev->nr_regs; i++) { - reg = &usbdev->regs[i]; + for ( i = 0; i < usbdev->nr_regs; i++ ) { + reg = &usbdev->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_usbdev_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_usbdev_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_usbdev_t *usbdev = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_usbdev_t* usbdev = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < usbdev->nr_regs; i++) { - reg = &usbdev->regs[i]; + for ( i = 0; i < usbdev->nr_regs; i++ ) { + reg = &usbdev->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_usbdev_readfn[] = -{ - s3c2410_usbdev_read, - s3c2410_usbdev_read, - s3c2410_usbdev_read -}; +static CPUReadMemoryFunc* s3c2410_usbdev_readfn[] = { s3c2410_usbdev_read, s3c2410_usbdev_read, s3c2410_usbdev_read }; -static CPUWriteMemoryFunc *s3c2410_usbdev_writefn[] = -{ - s3c2410_usbdev_write, - s3c2410_usbdev_write, - s3c2410_usbdev_write -}; +static CPUWriteMemoryFunc* s3c2410_usbdev_writefn[] = { s3c2410_usbdev_write, s3c2410_usbdev_write, s3c2410_usbdev_write }; -static int -s3c2410_usbdev_init(x49gp_module_t *module) +static int s3c2410_usbdev_init( x49gp_module_t* module ) { - s3c2410_usbdev_t *usbdev; - int iotype; + s3c2410_usbdev_t* usbdev; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - usbdev = malloc(sizeof(s3c2410_usbdev_t)); - if (NULL == usbdev) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_usbdev_data_init(usbdev)) { - free(usbdev); - return -ENOMEM; - } + usbdev = malloc( sizeof( s3c2410_usbdev_t ) ); + if ( NULL == usbdev ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_usbdev_data_init( usbdev ) ) { + free( usbdev ); + return -ENOMEM; + } - module->user_data = usbdev; + module->user_data = usbdev; - iotype = cpu_register_io_memory(s3c2410_usbdev_readfn, - s3c2410_usbdev_writefn, usbdev); + iotype = cpu_register_io_memory( s3c2410_usbdev_readfn, s3c2410_usbdev_writefn, usbdev ); #ifdef DEBUG_S3C2410_USBDEV - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_USBDEV_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_USBDEV_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_usbdev_exit(x49gp_module_t *module) +static int s3c2410_usbdev_exit( x49gp_module_t* module ) { - s3c2410_usbdev_t *usbdev; + s3c2410_usbdev_t* usbdev; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - usbdev = module->user_data; - if (usbdev->regs) - free(usbdev->regs); - free(usbdev); - } + if ( module->user_data ) { + usbdev = module->user_data; + if ( usbdev->regs ) + free( usbdev->regs ); + free( usbdev ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_usbdev_init(x49gp_t *x49gp) +int x49gp_s3c2410_usbdev_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-usbdev", - s3c2410_usbdev_init, - s3c2410_usbdev_exit, - s3c2410_usbdev_reset, - s3c2410_usbdev_load, - s3c2410_usbdev_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-usbdev", s3c2410_usbdev_init, s3c2410_usbdev_exit, s3c2410_usbdev_reset, s3c2410_usbdev_load, + s3c2410_usbdev_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/s3c2410_watchdog.c b/src/s3c2410_watchdog.c index 9714e9f..3756c3c 100644 --- a/src/s3c2410_watchdog.c +++ b/src/s3c2410_watchdog.c @@ -13,367 +13,328 @@ #include "s3c2410.h" #include "s3c2410_intc.h" - typedef struct { - uint32_t wtcon; - uint32_t wtdat; - uint32_t wtcnt; + uint32_t wtcon; + uint32_t wtdat; + uint32_t wtcnt; - unsigned int nr_regs; - s3c2410_offset_t *regs; + unsigned int nr_regs; + s3c2410_offset_t* regs; - x49gp_t *x49gp; + x49gp_t* x49gp; - unsigned long interval; - x49gp_timer_t *timer; + unsigned long interval; + x49gp_timer_t* timer; } s3c2410_watchdog_t; -static int -s3c2410_watchdog_data_init(s3c2410_watchdog_t *watchdog) +static int s3c2410_watchdog_data_init( s3c2410_watchdog_t* watchdog ) { - s3c2410_offset_t regs[] = { - S3C2410_OFFSET(WATCHDOG, WTCON, 0x8021, watchdog->wtcon), - S3C2410_OFFSET(WATCHDOG, WTDAT, 0x8000, watchdog->wtdat), - S3C2410_OFFSET(WATCHDOG, WTCNT, 0x8000, watchdog->wtcnt) - }; + s3c2410_offset_t regs[] = { S3C2410_OFFSET( WATCHDOG, WTCON, 0x8021, watchdog->wtcon ), + S3C2410_OFFSET( WATCHDOG, WTDAT, 0x8000, watchdog->wtdat ), + S3C2410_OFFSET( WATCHDOG, WTCNT, 0x8000, watchdog->wtcnt ) }; - memset(watchdog, 0, sizeof(s3c2410_watchdog_t)); + memset( watchdog, 0, sizeof( s3c2410_watchdog_t ) ); - watchdog->regs = malloc(sizeof(regs)); - if (NULL == watchdog->regs) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } + watchdog->regs = malloc( sizeof( regs ) ); + if ( NULL == watchdog->regs ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } - memcpy(watchdog->regs, regs, sizeof(regs)); - watchdog->nr_regs = sizeof(regs) / sizeof(regs[0]); + memcpy( watchdog->regs, regs, sizeof( regs ) ); + watchdog->nr_regs = sizeof( regs ) / sizeof( regs[ 0 ] ); - return 0; + return 0; } -static void -s3c2410_watchdog_tick(void *data) +static void s3c2410_watchdog_tick( void* data ) { - s3c2410_watchdog_t *watchdog = data; - x49gp_t *x49gp = watchdog->x49gp; + s3c2410_watchdog_t* watchdog = data; + x49gp_t* x49gp = watchdog->x49gp; - if (watchdog->wtcnt > 0) { - watchdog->wtcnt--; - } else { - watchdog->wtcnt = watchdog->wtdat; - } + if ( watchdog->wtcnt > 0 ) { + watchdog->wtcnt--; + } else { + watchdog->wtcnt = watchdog->wtdat; + } - if (watchdog->wtcnt > 0) { -// watchdog->timer.expires += watchdog->interval; - x49gp_mod_timer(watchdog->timer, x49gp_get_clock() + watchdog->interval); - return; - } + if ( watchdog->wtcnt > 0 ) { + // watchdog->timer.expires += watchdog->interval; + x49gp_mod_timer( watchdog->timer, x49gp_get_clock() + watchdog->interval ); + return; + } - if (watchdog->wtcon & 0x0004) { + if ( watchdog->wtcon & 0x0004 ) { #ifdef DEBUG_S3C2410_WATCHDOG - printf("WATCHDOG: assert WDT interrupt\n"); + printf( "WATCHDOG: assert WDT interrupt\n" ); #endif -// g_mutex_lock(x49gp->memlock); + // g_mutex_lock(x49gp->memlock); - s3c2410_intc_assert(x49gp, INT_WDT, 0); + s3c2410_intc_assert( x49gp, INT_WDT, 0 ); -// g_mutex_unlock(x49gp->memlock); - } + // g_mutex_unlock(x49gp->memlock); + } - if (watchdog->wtcon & 0x0001) { + if ( watchdog->wtcon & 0x0001 ) { #ifdef DEBUG_S3C2410_WATCHDOG - printf("WATCHDOG: assert internal RESET\n"); + printf( "WATCHDOG: assert internal RESET\n" ); #endif - x49gp_modules_reset(x49gp, X49GP_RESET_WATCHDOG); - cpu_reset(x49gp->env); + x49gp_modules_reset( x49gp, X49GP_RESET_WATCHDOG ); + cpu_reset( x49gp->env ); -// if (x49gp->arm->NresetSig != LOW) { -// x49gp->arm->NresetSig = LOW; -// x49gp->arm->Exception++; -// } - return; - } + // if (x49gp->arm->NresetSig != LOW) { + // x49gp->arm->NresetSig = LOW; + // x49gp->arm->Exception++; + // } + return; + } -// watchdog->timer.expires += watchdog->interval; - x49gp_mod_timer(watchdog->timer, x49gp_get_clock() + watchdog->interval); + // watchdog->timer.expires += watchdog->interval; + x49gp_mod_timer( watchdog->timer, x49gp_get_clock() + watchdog->interval ); } -unsigned long -s3c2410_watchdog_next_interrupt(x49gp_t *x49gp) +unsigned long s3c2410_watchdog_next_interrupt( x49gp_t* x49gp ) { - s3c2410_watchdog_t *watchdog = x49gp->s3c2410_watchdog; - unsigned long irq; - unsigned long ticks; + s3c2410_watchdog_t* watchdog = x49gp->s3c2410_watchdog; + unsigned long irq; + unsigned long ticks; - ticks = x49gp_get_clock(); + ticks = x49gp_get_clock(); - if (!(watchdog->wtcon & 0x0020)) { - return ~(0); - } + if ( !( watchdog->wtcon & 0x0020 ) ) { + return ~( 0 ); + } - if (x49gp_timer_pending(watchdog->timer)) { - irq = x49gp_timer_expires(watchdog->timer) - ticks; - } else { - irq = 0; - } + if ( x49gp_timer_pending( watchdog->timer ) ) { + irq = x49gp_timer_expires( watchdog->timer ) - ticks; + } else { + irq = 0; + } - if (watchdog->wtcnt) { - irq += (watchdog->wtcnt - 1) * watchdog->interval; - } else { - irq += watchdog->wtdat * watchdog->interval; - } + if ( watchdog->wtcnt ) { + irq += ( watchdog->wtcnt - 1 ) * watchdog->interval; + } else { + irq += watchdog->wtdat * watchdog->interval; + } #ifdef DEBUG_S3C2410_WATCHDOG - printf("WATCHDOG: wtcnt %u, interval %lu, expires %llu, next irq %lu\n", - watchdog->wtcnt, watchdog->interval, (unsigned long long) (x49gp_timer_pending(watchdog->timer) ? x49gp_timer_expires(watchdog->timer) : 0), irq); + printf( "WATCHDOG: wtcnt %u, interval %lu, expires %llu, next irq %lu\n", watchdog->wtcnt, watchdog->interval, + ( unsigned long long )( x49gp_timer_pending( watchdog->timer ) ? x49gp_timer_expires( watchdog->timer ) : 0 ), irq ); #endif - return irq; + return irq; } -static int -s3c2410_watchdog_update(s3c2410_watchdog_t *watchdog) +static int s3c2410_watchdog_update( s3c2410_watchdog_t* watchdog ) { - uint32_t pre, mux; + uint32_t pre, mux; - if (!(watchdog->wtcon & 0x0020)) { - x49gp_del_timer(watchdog->timer); + if ( !( watchdog->wtcon & 0x0020 ) ) { + x49gp_del_timer( watchdog->timer ); #ifdef DEBUG_S3C2410_WATCHDOG - printf("WATCHDOG: stop timer\n"); + printf( "WATCHDOG: stop timer\n" ); #endif - return 0; - } + return 0; + } - pre = (watchdog->wtcon >> 8) & 0xff; - mux = (watchdog->wtcon >> 3) & 3; + pre = ( watchdog->wtcon >> 8 ) & 0xff; + mux = ( watchdog->wtcon >> 3 ) & 3; - watchdog->interval = (pre + 1) * (16 << mux); + watchdog->interval = ( pre + 1 ) * ( 16 << mux ); #ifdef DEBUG_S3C2410_WATCHDOG - printf("WATCHDOG: start tick (%lu PCLKs)\n", watchdog->interval); + printf( "WATCHDOG: start tick (%lu PCLKs)\n", watchdog->interval ); #endif - x49gp_mod_timer(watchdog->timer, x49gp_get_clock() + watchdog->interval); - return 0; + x49gp_mod_timer( watchdog->timer, x49gp_get_clock() + watchdog->interval ); + return 0; } -static uint32_t -s3c2410_watchdog_read(void *opaque, target_phys_addr_t offset) +static uint32_t s3c2410_watchdog_read( void* opaque, target_phys_addr_t offset ) { - s3c2410_watchdog_t *watchdog = opaque; - s3c2410_offset_t *reg; + s3c2410_watchdog_t* watchdog = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(watchdog, offset)) { - return ~(0); - } - - reg = S3C2410_OFFSET_ENTRY(watchdog, offset); + if ( !S3C2410_OFFSET_OK( watchdog, offset ) ) { + return ~( 0 ); + } + reg = S3C2410_OFFSET_ENTRY( watchdog, offset ); #ifdef DEBUG_S3C2410_WATCHDOG - printf("read %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-watchdog", S3C2410_WATCHDOG_BASE, - reg->name, (unsigned long) offset, *(reg->datap)); + printf( "read %s [%08x] %s [%08lx] data %08x\n", "s3c2410-watchdog", S3C2410_WATCHDOG_BASE, reg->name, ( unsigned long )offset, + *( reg->datap ) ); #endif - return *(reg->datap); + return *( reg->datap ); } -static void -s3c2410_watchdog_write(void *opaque, target_phys_addr_t offset, uint32_t data) +static void s3c2410_watchdog_write( void* opaque, target_phys_addr_t offset, uint32_t data ) { - s3c2410_watchdog_t *watchdog = opaque; - s3c2410_offset_t *reg; + s3c2410_watchdog_t* watchdog = opaque; + s3c2410_offset_t* reg; - if (! S3C2410_OFFSET_OK(watchdog, offset)) { - return; - } + if ( !S3C2410_OFFSET_OK( watchdog, offset ) ) { + return; + } - reg = S3C2410_OFFSET_ENTRY(watchdog, offset); + reg = S3C2410_OFFSET_ENTRY( watchdog, offset ); #ifdef DEBUG_S3C2410_WATCHDOG - printf("write %s [%08x] %s [%08lx] data %08x\n", - "s3c2410-watchdog", S3C2410_WATCHDOG_BASE, - reg->name, (unsigned long) offset, data); + printf( "write %s [%08x] %s [%08lx] data %08x\n", "s3c2410-watchdog", S3C2410_WATCHDOG_BASE, reg->name, ( unsigned long )offset, data ); #endif - *(reg->datap) = data; + *( reg->datap ) = data; - switch (offset) { - case S3C2410_WATCHDOG_WTCON: - case S3C2410_WATCHDOG_WTCNT: - s3c2410_watchdog_update(watchdog); - break; - default: - break; - } + switch ( offset ) { + case S3C2410_WATCHDOG_WTCON: + case S3C2410_WATCHDOG_WTCNT: + s3c2410_watchdog_update( watchdog ); + break; + default: + break; + } } -static int -s3c2410_watchdog_load(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_watchdog_load( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_watchdog_t *watchdog = module->user_data; - s3c2410_offset_t *reg; - int error = 0; - int i; + s3c2410_watchdog_t* watchdog = module->user_data; + s3c2410_offset_t* reg; + int error = 0; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < watchdog->nr_regs; i++) { - reg = &watchdog->regs[i]; + for ( i = 0; i < watchdog->nr_regs; i++ ) { + reg = &watchdog->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - if (x49gp_module_get_u32(module, key, reg->name, - reg->reset, reg->datap)) - error = -EAGAIN; - } + if ( x49gp_module_get_u32( module, key, reg->name, reg->reset, reg->datap ) ) + error = -EAGAIN; + } - s3c2410_watchdog_update(watchdog); + s3c2410_watchdog_update( watchdog ); - return error; + return error; } -static int -s3c2410_watchdog_save(x49gp_module_t *module, GKeyFile *key) +static int s3c2410_watchdog_save( x49gp_module_t* module, GKeyFile* key ) { - s3c2410_watchdog_t *watchdog = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_watchdog_t* watchdog = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < watchdog->nr_regs; i++) { - reg = &watchdog->regs[i]; + for ( i = 0; i < watchdog->nr_regs; i++ ) { + reg = &watchdog->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - x49gp_module_set_u32(module, key, reg->name, *(reg->datap)); - } + x49gp_module_set_u32( module, key, reg->name, *( reg->datap ) ); + } - return 0; + return 0; } -static int -s3c2410_watchdog_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int s3c2410_watchdog_reset( x49gp_module_t* module, x49gp_reset_t reset ) { - s3c2410_watchdog_t *watchdog = module->user_data; - s3c2410_offset_t *reg; - int i; + s3c2410_watchdog_t* watchdog = module->user_data; + s3c2410_offset_t* reg; + int i; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - for (i = 0; i < watchdog->nr_regs; i++) { - reg = &watchdog->regs[i]; + for ( i = 0; i < watchdog->nr_regs; i++ ) { + reg = &watchdog->regs[ i ]; - if (NULL == reg->name) - continue; + if ( NULL == reg->name ) + continue; - *(reg->datap) = reg->reset; - } + *( reg->datap ) = reg->reset; + } - s3c2410_watchdog_update(watchdog); + s3c2410_watchdog_update( watchdog ); - return 0; + return 0; } -static CPUReadMemoryFunc *s3c2410_watchdog_readfn[] = -{ - s3c2410_watchdog_read, - s3c2410_watchdog_read, - s3c2410_watchdog_read -}; +static CPUReadMemoryFunc* s3c2410_watchdog_readfn[] = { s3c2410_watchdog_read, s3c2410_watchdog_read, s3c2410_watchdog_read }; -static CPUWriteMemoryFunc *s3c2410_watchdog_writefn[] = -{ - s3c2410_watchdog_write, - s3c2410_watchdog_write, - s3c2410_watchdog_write -}; +static CPUWriteMemoryFunc* s3c2410_watchdog_writefn[] = { s3c2410_watchdog_write, s3c2410_watchdog_write, s3c2410_watchdog_write }; -static int -s3c2410_watchdog_init(x49gp_module_t *module) +static int s3c2410_watchdog_init( x49gp_module_t* module ) { - s3c2410_watchdog_t *watchdog; - int iotype; + s3c2410_watchdog_t* watchdog; + int iotype; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - watchdog = malloc(sizeof(s3c2410_watchdog_t)); - if (NULL == watchdog) { - fprintf(stderr, "%s: %s:%u: Out of memory\n", - module->x49gp->progname, __FUNCTION__, __LINE__); - return -ENOMEM; - } - if (s3c2410_watchdog_data_init(watchdog)) { - free(watchdog); - return -ENOMEM; - } + watchdog = malloc( sizeof( s3c2410_watchdog_t ) ); + if ( NULL == watchdog ) { + fprintf( stderr, "%s: %s:%u: Out of memory\n", module->x49gp->progname, __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + if ( s3c2410_watchdog_data_init( watchdog ) ) { + free( watchdog ); + return -ENOMEM; + } - module->user_data = watchdog; + module->user_data = watchdog; - watchdog->x49gp = module->x49gp; - module->x49gp->s3c2410_watchdog = watchdog; + watchdog->x49gp = module->x49gp; + module->x49gp->s3c2410_watchdog = watchdog; - watchdog->timer = x49gp_new_timer(X49GP_TIMER_VIRTUAL, - s3c2410_watchdog_tick, watchdog); + watchdog->timer = x49gp_new_timer( X49GP_TIMER_VIRTUAL, s3c2410_watchdog_tick, watchdog ); - iotype = cpu_register_io_memory(s3c2410_watchdog_readfn, - s3c2410_watchdog_writefn, watchdog); + iotype = cpu_register_io_memory( s3c2410_watchdog_readfn, s3c2410_watchdog_writefn, watchdog ); #ifdef DEBUG_S3C2410_WATCHDOG - printf("%s: iotype %08x\n", __FUNCTION__, iotype); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); #endif - cpu_register_physical_memory(S3C2410_WATCHDOG_BASE, S3C2410_MAP_SIZE, iotype); - return 0; + cpu_register_physical_memory( S3C2410_WATCHDOG_BASE, S3C2410_MAP_SIZE, iotype ); + return 0; } -static int -s3c2410_watchdog_exit(x49gp_module_t *module) +static int s3c2410_watchdog_exit( x49gp_module_t* module ) { - s3c2410_watchdog_t *watchdog; + s3c2410_watchdog_t* watchdog; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - watchdog = module->user_data; - if (watchdog->regs) - free(watchdog->regs); - free(watchdog); - } + if ( module->user_data ) { + watchdog = module->user_data; + if ( watchdog->regs ) + free( watchdog->regs ); + free( watchdog ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_s3c2410_watchdog_init(x49gp_t *x49gp) +int x49gp_s3c2410_watchdog_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "s3c2410-watchdog", - s3c2410_watchdog_init, - s3c2410_watchdog_exit, - s3c2410_watchdog_reset, - s3c2410_watchdog_load, - s3c2410_watchdog_save, - NULL, &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "s3c2410-watchdog", s3c2410_watchdog_init, s3c2410_watchdog_exit, s3c2410_watchdog_reset, + s3c2410_watchdog_load, s3c2410_watchdog_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/saturn.h b/src/saturn.h index b1102ff..c989279 100644 --- a/src/saturn.h +++ b/src/saturn.h @@ -4,40 +4,40 @@ #ifndef SATURN_H #define SATURN_H 1 -#define NB_RSTK 8 +#define NB_RSTK 8 -typedef uint64_t saturn_reg_t; +typedef uint64_t saturn_reg_t; typedef struct { - uint32_t read_map[256 + 1]; - uint32_t write_map[256 + 1]; - uint8_t top_map[256 + 1 + 3]; - saturn_reg_t A; - saturn_reg_t B; - saturn_reg_t C; - saturn_reg_t D; - saturn_reg_t R0; - saturn_reg_t R1; - saturn_reg_t R2; - saturn_reg_t R3; - saturn_reg_t R4; - uint32_t D0; - uint32_t D1; - uint32_t P, P4, P4_32; - uint32_t ST; - uint32_t HST; - uint32_t carry; - int dec; - uint32_t RSTK[NB_RSTK]; - uint32_t RSTK_i; - uint32_t REG_FIELD[32]; - uint32_t FIELD_START[32]; - uint32_t FIELD_LENGTH[32]; + uint32_t read_map[ 256 + 1 ]; + uint32_t write_map[ 256 + 1 ]; + uint8_t top_map[ 256 + 1 + 3 ]; + saturn_reg_t A; + saturn_reg_t B; + saturn_reg_t C; + saturn_reg_t D; + saturn_reg_t R0; + saturn_reg_t R1; + saturn_reg_t R2; + saturn_reg_t R3; + saturn_reg_t R4; + uint32_t D0; + uint32_t D1; + uint32_t P, P4, P4_32; + uint32_t ST; + uint32_t HST; + uint32_t carry; + int dec; + uint32_t RSTK[ NB_RSTK ]; + uint32_t RSTK_i; + uint32_t REG_FIELD[ 32 ]; + uint32_t FIELD_START[ 32 ]; + uint32_t FIELD_LENGTH[ 32 ]; } saturn_cpu_t; -#define SAT_RPLTOP 0x8076b -#define SAT_RSKTOP 0x806f3 /* RETTOP */ -#define SAT_DSKTOP 0x806f8 -#define SAT_EDITLINE 0x806fd +#define SAT_RPLTOP 0x8076b +#define SAT_RSKTOP 0x806f3 /* RETTOP */ +#define SAT_DSKTOP 0x806f8 +#define SAT_EDITLINE 0x806fd #endif /* !(SATURN_H) */ diff --git a/src/sram.c b/src/sram.c index 353885c..2f2c7f5 100644 --- a/src/sram.c +++ b/src/sram.c @@ -17,471 +17,451 @@ #include "saturn.h" typedef struct { - void *data; - void *shadow; - char *filename; - int fd; - size_t size; - uint32_t offset; - x49gp_t *x49gp; + void* data; + void* shadow; + char* filename; + int fd; + size_t size; + uint32_t offset; + x49gp_t* x49gp; } x49gp_sram_t; -#define S3C2410_SRAM_BASE 0x08000000 -#define S3C2410_SRAM_SIZE 0x00080000 +#define S3C2410_SRAM_BASE 0x08000000 +#define S3C2410_SRAM_SIZE 0x00080000 -#define BASE 0xbcbb5 +#define BASE 0xbcbb5 -#define SATURN(r) ((target_phys_addr_t) &((saturn_cpu_t *)0)->r) +#define SATURN( r ) ( ( target_phys_addr_t ) & ( ( saturn_cpu_t* )0 )->r ) -#if defined(DEBUG_X49GP_SYSRAM_READ) || defined(DEBUG_X49GP_SYSRAM_WRITE) || \ - defined(DEBUG_X49GP_IRAM_READ) || defined(DEBUG_X49GP_IRAM_WRITE) || \ - defined(DEBUG_X49GP_ERAM_READ) || defined(DEBUG_X49GP_ERAM_WRITE) -#define DEBUG_X49GP_SRAM 1 +#if defined( DEBUG_X49GP_SYSRAM_READ ) || defined( DEBUG_X49GP_SYSRAM_WRITE ) || defined( DEBUG_X49GP_IRAM_READ ) || \ + defined( DEBUG_X49GP_IRAM_WRITE ) || defined( DEBUG_X49GP_ERAM_READ ) || defined( DEBUG_X49GP_ERAM_WRITE ) +# define DEBUG_X49GP_SRAM 1 #endif typedef struct { - uint32_t x; - uint32_t ml; - uint32_t mh; - uint8_t m; - uint8_t s; + uint32_t x; + uint32_t ml; + uint32_t mh; + uint8_t m; + uint8_t s; } hp_real_t; #ifdef DEBUG_X49GP_SRAM -static uint32_t -saturn_map_s2a(saturn_cpu_t *saturn, uint32_t saddr) +static uint32_t saturn_map_s2a( saturn_cpu_t* saturn, uint32_t saddr ) { - uint32_t addr; + uint32_t addr; - addr = ldl_p(&saturn->read_map[saddr >> 12]) | ((saddr >> 1) & 0x7ff); + addr = ldl_p( &saturn->read_map[ saddr >> 12 ] ) | ( ( saddr >> 1 ) & 0x7ff ); -// printf("SATURN: saddr %05x, addr %08x\n", saddr, addr); + // printf("SATURN: saddr %05x, addr %08x\n", saddr, addr); - return addr; + return addr; } -static uint32_t -saturn_peek(saturn_cpu_t *saturn, uint32_t saddr, uint32_t size) +static uint32_t saturn_peek( saturn_cpu_t* saturn, uint32_t saddr, uint32_t size ) { - uint32_t addr, rot, mask, data; - uint64_t value; + uint32_t addr, rot, mask, data; + uint64_t value; - addr = saturn_map_s2a(saturn, saddr) & 0xfffffffc; - if (addr > 0x08080000) - return 0; + addr = saturn_map_s2a( saturn, saddr ) & 0xfffffffc; + if ( addr > 0x08080000 ) + return 0; -// printf("SATURN: addr %08x\n", addr); - value = ((uint64_t) ldl_phys(addr)) | (((uint64_t) ldl_phys(addr + 4)) << 32); -// printf("SATURN: value %016llx\n", value); + // printf("SATURN: addr %08x\n", addr); + value = ( ( uint64_t )ldl_phys( addr ) ) | ( ( ( uint64_t )ldl_phys( addr + 4 ) ) << 32 ); + // printf("SATURN: value %016llx\n", value); - rot = (saddr & 7) << 2; - mask = (1ULL << (size << 2)) - 1; -// printf("SATURN: rot %u, mask %08x\n", rot, mask); + rot = ( saddr & 7 ) << 2; + mask = ( 1ULL << ( size << 2 ) ) - 1; + // printf("SATURN: rot %u, mask %08x\n", rot, mask); - data = ((uint32_t) (value >> rot)) & mask; -// printf("SATURN: data %08x\n", data); + data = ( ( uint32_t )( value >> rot ) ) & mask; + // printf("SATURN: data %08x\n", data); - return data; + return data; } -static uint32_t -saturn_peek_address(saturn_cpu_t *saturn, uint32_t saddr) +static uint32_t saturn_peek_address( saturn_cpu_t* saturn, uint32_t saddr ) { return saturn_peek( saturn, saddr, 5 ); } + +static int hxs2real( int hxs ) { - return saturn_peek(saturn, saddr, 5); + int n = 0, c = 1; + + while ( hxs ) { + n += ( hxs & 0xf ) * c; + c *= 10; + hxs >>= 4; + } + return n; } -static int -hxs2real(int hxs) +static char* real_number( saturn_cpu_t* saturn, uint32_t saddr, char* buffer, int ml, int xl ) { - int n = 0, c = 1; + char* p = buffer; + char fmt[ 20 ]; + char m[ 16 ]; + hp_real_t r; + int re, xs; + int i; + uint32_t pc; - while (hxs) { - n += (hxs & 0xf) * c; - c *= 10; - hxs >>= 4; - } - return n; + pc = saddr; + + /* + * Read the number + */ + r.x = saturn_peek( saturn, pc, xl ); + pc += xl; + r.ml = saturn_peek( saturn, pc, ml - 8 ); + pc += ml - 8; + r.mh = saturn_peek( saturn, pc, 8 ); + pc += 8; + r.m = saturn_peek( saturn, pc, 1 ); + pc += 1; + r.s = saturn_peek( saturn, pc, 1 ); + pc += 1; + + /* + * Figure out the exponent + */ + xs = 5; + while ( --xl ) + xs *= 10; + re = hxs2real( r.x ); + if ( re >= xs ) + re = re - 2 * xs; + + if ( ( re >= 0 ) && ( re < ml + 1 ) ) { + if ( r.s >= 5 ) + *p++ = '-'; + + sprintf( fmt, "%%.1X%%.8X%%.%dX", ml - 8 ); + sprintf( m, fmt, r.m, r.mh, r.ml ); + + for ( i = 0; i <= re; i++ ) + *p++ = m[ i ]; + *p++ = '.'; + for ( ; i < ml + 1; i++ ) + *p++ = m[ i ]; + p--; + while ( *p == '0' ) + p--; + if ( *p == '.' ) + p--; + *++p = '\0'; + + return buffer; + } + + if ( ( re < 0 ) && ( re >= -ml - 1 ) ) { + sprintf( fmt, "%%.1X%%.8X%%.%dX", ml - 8 ); + sprintf( m, fmt, r.m, r.mh, r.ml ); + + for ( i = ml; m[ i ] == '0'; i-- ) + ; + + if ( -re <= ml - i + 1 ) { + if ( r.s >= 5 ) + *p++ = '-'; + + *p++ = '.'; + + for ( i = 1; i < -re; i++ ) + *p++ = '0'; + + for ( i = 0; i < ml + 1; i++ ) + *p++ = m[ i ]; + p--; + while ( *p == '0' ) + p--; + *++p = '\0'; + + return buffer; + } + } + + sprintf( fmt, "%%s%%X.%%.8X%%.%dX", ml - 8 ); + sprintf( p, fmt, ( r.s >= 5 ) ? "-" : "", r.m, r.mh, r.ml ); + + p += strlen( p ) - 1; + + while ( *p == '0' ) + p--; + *++p = '\0'; + + if ( re ) { + sprintf( p, "E%d", re ); + p += strlen( p ); + *p = '\0'; + } + + return buffer; } -static char * -real_number(saturn_cpu_t *saturn, uint32_t saddr, char *buffer, int ml, int xl) +static uint32_t dump_object( x49gp_t* x49gp, x49gp_sram_t* sram, uint32_t saddr ) { - char *p = buffer; - char fmt[20]; - char m[16]; - hp_real_t r; - int re, xs; - int i; - uint32_t pc; + saturn_cpu_t* saturn = ( sram->data + 0x3340 ); + char buffer[ 128 ]; + uint32_t prolog, pc; + char c; + int i, n; - pc = saddr; + pc = saddr; - /* - * Read the number - */ - r.x = saturn_peek(saturn, pc, xl); - pc += xl; - r.ml = saturn_peek(saturn, pc, ml - 8); - pc += ml - 8; - r.mh = saturn_peek(saturn, pc, 8); - pc += 8; - r.m = saturn_peek(saturn, pc, 1); - pc += 1; - r.s = saturn_peek(saturn, pc, 1); - pc += 1; + prolog = saturn_peek( saturn, pc, 5 ); + pc += 5; - /* - * Figure out the exponent - */ - xs = 5; - while (--xl) - xs *= 10; - re = hxs2real(r.x); - if (re >= xs) - re = re - 2 * xs; + switch ( prolog ) { + case 0x02e48: + case 0x02e6d: + printf( " " ); + n = saturn_peek( saturn, pc, 2 ); + pc += 2; + for ( i = 0; i < n; i++ ) { + c = saturn_peek( saturn, pc, 2 ); + printf( "%c", c ); + pc += 2; + } + break; - if ((re >= 0) && (re < ml + 1)) { - if (r.s >= 5) - *p++ = '-'; + case 0x02a4e: + n = saturn_peek( saturn, pc, 5 ); + pc += 5; + if ( n <= 16 ) { + printf( " #%08x%08xh", saturn_peek( saturn, pc + 8, 8 ), saturn_peek( saturn, pc + 0, 8 ) ); + } else { + printf( " C#" ); + for ( i = 0; i < n; i++ ) { + printf( "%x", saturn_peek( saturn, pc + i, 1 ) ); + } + } + pc += n; + break; - sprintf(fmt, "%%.1X%%.8X%%.%dX", ml - 8); - sprintf(m, fmt, r.m, r.mh, r.ml); + case 0x02911: + printf( " <%05x>", saturn_peek( saturn, pc, 5 ) ); + pc += 5; + break; - for (i = 0; i <= re; i++) - *p++ = m[i]; - *p++ = '.'; - for ( ; i < ml + 1; i++) - *p++ = m[i]; - p--; - while(*p == '0') - p--; - if (*p == '.') - p--; - *++p = '\0'; + case 0x02933: + printf( " %%%s", real_number( saturn, pc, buffer, 11, 3 ) ); + pc += 16; + break; - return buffer; - } + case 0x02955: + printf( " %%%%%s", real_number( saturn, pc, buffer, 14, 5 ) ); + pc += 21; + break; - if ((re < 0) && (re >= -ml - 1)) { - sprintf(fmt, "%%.1X%%.8X%%.%dX", ml - 8); - sprintf(m, fmt, r.m, r.mh, r.ml); + case 0x02e92: + printf( " <%03x %03x>", saturn_peek( saturn, pc + 0, 3 ), saturn_peek( saturn, pc + 3, 3 ) ); + pc += 6; + break; - for (i = ml; m[i] == '0'; i--) - ; + case 0x026ac: + printf( " <%03x %04x>", saturn_peek( saturn, pc + 0, 3 ), saturn_peek( saturn, pc + 3, 4 ) ); + pc += 7; + break; - if (-re <= ml - i + 1) { - if (r.s >= 5) - *p++ = '-'; + case 0x02ab8: + printf( " '" ); + while ( 1 ) { + prolog = saturn_peek( saturn, pc, 5 ); + if ( prolog == 0x0312b ) { + pc += 5; + break; + } + pc += dump_object( x49gp, sram, pc ); + } + printf( " '" ); + break; - *p++ = '.'; + case 0x02d9d: + printf( " :" ); + while ( 1 ) { + prolog = saturn_peek( saturn, pc, 5 ); + if ( prolog == 0x0312b ) { + pc += 5; + break; + } + pc += dump_object( x49gp, sram, pc ); + } + printf( " ;" ); + break; - for (i = 1; i < -re; i++) - *p++ = '0'; + default: + printf( " %05x", prolog ); + break; + } - for (i = 0; i < ml + 1; i++) - *p++ = m[i]; - p--; - while(*p == '0') - p--; - *++p = '\0'; - - return buffer; - } - } - - sprintf(fmt, "%%s%%X.%%.8X%%.%dX", ml - 8); - sprintf(p, fmt, (r.s >= 5) ? "-" : "", r.m, r.mh, r.ml); - - p += strlen(p) - 1; - - while(*p == '0') - p--; - *++p = '\0'; - - if (re) { - sprintf(p, "E%d", re); - p += strlen(p); - *p = '\0'; - } - - return buffer; + return pc - saddr; } -static uint32_t -dump_object(x49gp_t *x49gp, x49gp_sram_t *sram, uint32_t saddr) +static void debug_saturn( x49gp_t* x49gp, x49gp_sram_t* sram ) { - saturn_cpu_t *saturn = (sram->data + 0x3340); - char buffer[128]; - uint32_t prolog, pc; - char c; - int i, n; + saturn_cpu_t* saturn = ( sram->data + 0x3340 ); + uint32_t sp, se; + uint32_t prolog; + uint32_t rsp; + uint32_t first; + static uint32_t prev_first = 0; + static int state = 0; + int depth; + int i; - pc = saddr; + // printf("SATURN: %p, RPLTOP: %08x\n", saturn, saturn_map_s2a(saturn, SAT_RPLTOP)); + // printf("SATURN: %p, RSKTOP: %08x\n", saturn, saturn_map_s2a(saturn, SAT_RSKTOP)); - prolog = saturn_peek(saturn, pc, 5); - pc += 5; + // rpltop = saturn_peek_address(saturn, SAT_RPLTOP); + // printf("SATURN: RPLTOP: %05x\n", rpltop); - switch (prolog) { - case 0x02e48: - case 0x02e6d: - printf(" "); - n = saturn_peek(saturn, pc, 2); - pc += 2; - for (i = 0; i < n; i++) { - c = saturn_peek(saturn, pc, 2); - printf("%c", c); - pc += 2; - } - break; + rsp = saturn_peek_address( saturn, SAT_RSKTOP ); + if ( rsp == 0 ) + return; - case 0x02a4e: - n = saturn_peek(saturn, pc, 5); - pc += 5; - if (n <= 16) { - printf(" #%08x%08xh", saturn_peek(saturn, pc + 8, 8), - saturn_peek(saturn, pc + 0, 8)); - } else { - printf(" C#"); - for (i = 0; i < n; i++) { - printf("%x", saturn_peek(saturn, pc + i, 1)); - } - } - pc += n; - break; + first = saturn_peek_address( saturn, rsp - 5 ); + if ( first == prev_first ) { + return; + } + prev_first = first; - case 0x02911: - printf(" <%05x>", saturn_peek(saturn, pc, 5)); - pc += 5; - break; + if ( first == ( BASE + 0x14a54 ) ) { + state = 1; + } + if ( state == 0 ) + return; - case 0x02933: - printf(" %%%s", real_number(saturn, pc, buffer, 11, 3)); - pc += 16; - break; + printf( "SATURN: %p, rsp: %05x %08x\n", saturn, rsp, saturn_map_s2a( saturn, rsp ) ); - case 0x02955: - printf(" %%%%%s", real_number(saturn, pc, buffer, 14, 5)); - pc += 21; - break; + for ( i = 0;; i++ ) { + rsp -= 5; - case 0x02e92: - printf(" <%03x %03x>", saturn_peek(saturn, pc + 0, 3), - saturn_peek(saturn, pc + 3, 3)); - pc += 6; - break; + se = saturn_peek_address( saturn, rsp ); + if ( se == 0 ) + break; - case 0x026ac: - printf(" <%03x %04x>", saturn_peek(saturn, pc + 0, 3), - saturn_peek(saturn, pc + 3, 4)); - pc += 7; - break; + if ( se > BASE ) + printf( "SATURN: RSTK %02u: %05x <%05x>\n", i, se, se - BASE ); + else + printf( "SATURN: RSTK %02u: %05x\n", i, se ); + } - case 0x02ab8: - printf(" '"); - while (1) { - prolog = saturn_peek(saturn, pc, 5); - if (prolog == 0x0312b) { - pc += 5; - break; - } - pc += dump_object(x49gp, sram, pc); - } - printf(" '"); - break; + depth = ( saturn_peek_address( saturn, SAT_EDITLINE ) - saturn_peek_address( saturn, SAT_DSKTOP ) - 5 ) / 5; - case 0x02d9d: - printf(" :"); - while (1) { - prolog = saturn_peek(saturn, pc, 5); - if (prolog == 0x0312b) { - pc += 5; - break; - } - pc += dump_object(x49gp, sram, pc); - } - printf(" ;"); - break; + printf( "SATURN: depth %d\n", depth ); - default: - printf(" %05x", prolog); - break; - } + sp = saturn_peek_address( saturn, SAT_EDITLINE ) - 10; - return pc - saddr; + for ( i = 0; i < depth; i++ ) { + se = saturn_peek_address( saturn, sp ); + sp -= 5; + + prolog = saturn_peek( saturn, se, 5 ); + + printf( "SATURN: %02u: <%05x> <%05x>", depth - i, se, prolog ); + + dump_object( x49gp, sram, se ); + + printf( "\n" ); + } } -static void -debug_saturn(x49gp_t *x49gp, x49gp_sram_t *sram) +static uint32_t sram_get_word( void* opaque, target_phys_addr_t offset ) { - saturn_cpu_t *saturn = (sram->data + 0x3340); - uint32_t sp, se; - uint32_t prolog; - uint32_t rsp; - uint32_t first; - static uint32_t prev_first = 0; - static int state = 0; - int depth; - int i; + x49gp_sram_t* sram = opaque; + uint32_t data; -// printf("SATURN: %p, RPLTOP: %08x\n", saturn, saturn_map_s2a(saturn, SAT_RPLTOP)); -// printf("SATURN: %p, RSKTOP: %08x\n", saturn, saturn_map_s2a(saturn, SAT_RSKTOP)); + data = ldl_p( sram->data + offset ); -// rpltop = saturn_peek_address(saturn, SAT_RPLTOP); -// printf("SATURN: RPLTOP: %05x\n", rpltop); - - rsp = saturn_peek_address(saturn, SAT_RSKTOP); - if (rsp == 0) - return; - - first = saturn_peek_address(saturn, rsp - 5); - if (first == prev_first) { - return; - } - prev_first = first; - - if (first == (BASE + 0x14a54)) { - state = 1; - } - if (state == 0) - return; - - printf("SATURN: %p, rsp: %05x %08x\n", saturn, rsp, saturn_map_s2a(saturn, rsp)); - - for (i = 0; ; i++) { - rsp -= 5; - - se = saturn_peek_address(saturn, rsp); - if (se == 0) - break; - - if (se > BASE) - printf("SATURN: RSTK %02u: %05x <%05x>\n", i, se, se - BASE); - else - printf("SATURN: RSTK %02u: %05x\n", i, se); - } - - - depth = (saturn_peek_address(saturn, SAT_EDITLINE) - saturn_peek_address(saturn, SAT_DSKTOP) - 5) / 5; - - printf("SATURN: depth %d\n", depth); - - sp = saturn_peek_address(saturn, SAT_EDITLINE) - 10; - - for (i = 0; i < depth; i++) { - se = saturn_peek_address(saturn, sp); - sp -= 5; - - prolog = saturn_peek(saturn, se, 5); - - printf("SATURN: %02u: <%05x> <%05x>", depth - i, se, prolog); - - dump_object(x49gp, sram, se); - - printf("\n"); - } -} - -static uint32_t -sram_get_word(void *opaque, target_phys_addr_t offset) -{ - x49gp_sram_t *sram = opaque; - uint32_t data; - - data = ldl_p(sram->data + offset); - -#if 0 +# if 0 if (offset == 0x00000a1c) { printf("read SRAM at offset %08x: %08x (pc %08x)\n", offset, data, x49gp->arm->Reg[15]); } -#endif +# endif -#ifdef DEBUG_X49GP_SYSRAM_READ - if ((offset & ~(0x0001ffff)) == 0x00000000) { - printf("read SRAM 4 at offset %08x: %08x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_ERAM_READ - if ((offset & ~(0x0001ffff)) == 0x00020000) { - printf("read SRAM 4 at offset %08x: %08x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_IRAM_READ - if ((offset & ~(0x0003ffff)) == 0x00040000) { - printf("read SRAM 4 at offset %08x: %08x\n", offset, data); - } -#endif +# ifdef DEBUG_X49GP_SYSRAM_READ + if ( ( offset & ~( 0x0001ffff ) ) == 0x00000000 ) { + printf( "read SRAM 4 at offset %08x: %08x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_ERAM_READ + if ( ( offset & ~( 0x0001ffff ) ) == 0x00020000 ) { + printf( "read SRAM 4 at offset %08x: %08x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_IRAM_READ + if ( ( offset & ~( 0x0003ffff ) ) == 0x00040000 ) { + printf( "read SRAM 4 at offset %08x: %08x\n", offset, data ); + } +# endif - return data; + return data; } -static uint32_t -sram_get_halfword(void *opaque, target_phys_addr_t offset) +static uint32_t sram_get_halfword( void* opaque, target_phys_addr_t offset ) { - x49gp_sram_t *sram = opaque; - unsigned short data; + x49gp_sram_t* sram = opaque; + unsigned short data; - data = lduw_p(sram->data + offset); + data = lduw_p( sram->data + offset ); -#ifdef DEBUG_X49GP_SYSRAM_READ - if ((offset & ~(0x0001ffff)) == 0x00000000) { - printf("read SRAM 2 at offset %08x: %04x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_ERAM_READ - if ((offset & ~(0x0001ffff)) == 0x00020000) { - printf("read SRAM 2 at offset %08x: %04x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_IRAM_READ - if ((offset & ~(0x0003ffff)) == 0x00040000) { - printf("read SRAM 2 at offset %08x: %04x\n", offset, data); - } -#endif +# ifdef DEBUG_X49GP_SYSRAM_READ + if ( ( offset & ~( 0x0001ffff ) ) == 0x00000000 ) { + printf( "read SRAM 2 at offset %08x: %04x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_ERAM_READ + if ( ( offset & ~( 0x0001ffff ) ) == 0x00020000 ) { + printf( "read SRAM 2 at offset %08x: %04x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_IRAM_READ + if ( ( offset & ~( 0x0003ffff ) ) == 0x00040000 ) { + printf( "read SRAM 2 at offset %08x: %04x\n", offset, data ); + } +# endif - return data; + return data; } -static uint32_t -sram_get_byte(void *opaque, target_phys_addr_t offset) +static uint32_t sram_get_byte( void* opaque, target_phys_addr_t offset ) { - x49gp_sram_t *sram = opaque; - unsigned char data; + x49gp_sram_t* sram = opaque; + unsigned char data; - data = ldub_p(sram->data + offset); + data = ldub_p( sram->data + offset ); -#ifdef DEBUG_X49GP_SYSRAM_READ - if ((offset & ~(0x0001ffff)) == 0x00000000) { - printf("read SRAM 1 at offset %08x: %02x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_ERAM_READ - if ((offset & ~(0x0001ffff)) == 0x00020000) { - printf("read SRAM 1 at offset %08x: %02x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_IRAM_READ - if ((offset & ~(0x0003ffff)) == 0x00040000) { - printf("read SRAM 1 at offset %08x: %02x\n", offset, data); - } -#endif +# ifdef DEBUG_X49GP_SYSRAM_READ + if ( ( offset & ~( 0x0001ffff ) ) == 0x00000000 ) { + printf( "read SRAM 1 at offset %08x: %02x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_ERAM_READ + if ( ( offset & ~( 0x0001ffff ) ) == 0x00020000 ) { + printf( "read SRAM 1 at offset %08x: %02x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_IRAM_READ + if ( ( offset & ~( 0x0003ffff ) ) == 0x00040000 ) { + printf( "read SRAM 1 at offset %08x: %02x\n", offset, data ); + } +# endif - return data; + return data; } -static void -sram_put_word(void *opaque, target_phys_addr_t offset, uint32_t data) +static void sram_put_word( void* opaque, target_phys_addr_t offset, uint32_t data ) { - x49gp_sram_t *sram = opaque; + x49gp_sram_t* sram = opaque; - if (offset == 0x00000a1c) { - printf("write SRAM 4 at offset %08x: %08x (pc %08x)\n", - offset, data, sram->x49gp->env->regs[15]); - } + if ( offset == 0x00000a1c ) { + printf( "write SRAM 4 at offset %08x: %08x (pc %08x)\n", offset, data, sram->x49gp->env->regs[ 15 ] ); + } - debug_saturn(sram->x49gp, sram); + debug_saturn( sram->x49gp, sram ); -#if 0 +# if 0 if (offset == 0x3340 + SATURN(D1)) { printf("write D1 at offset %08x: %08x (pc %08x)\n", offset, data, sram->x49gp->env->regs[15]); @@ -494,301 +474,260 @@ sram_put_word(void *opaque, target_phys_addr_t offset, uint32_t data) printf("write Al at offset %08x: %08x (pc %08x)\n", offset, data, sram->x49gp->env->regs[15]); } -#endif +# endif -#ifdef DEBUG_X49GP_SYSRAM_WRITE - if ((offset & ~(0x0001ffff)) == 0x00000000) { - printf("write SRAM 4 at offset %08x: %08x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_ERAM_WRITE - if ((offset & ~(0x0001ffff)) == 0x00020000) { - printf("write SRAM 4 at offset %08x: %08x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_IRAM_WRITE - if ((offset & ~(0x0003ffff)) == 0x00040000) { - printf("write SRAM 4 at offset %08x: %08x\n", offset, data); - } -#endif +# ifdef DEBUG_X49GP_SYSRAM_WRITE + if ( ( offset & ~( 0x0001ffff ) ) == 0x00000000 ) { + printf( "write SRAM 4 at offset %08x: %08x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_ERAM_WRITE + if ( ( offset & ~( 0x0001ffff ) ) == 0x00020000 ) { + printf( "write SRAM 4 at offset %08x: %08x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_IRAM_WRITE + if ( ( offset & ~( 0x0003ffff ) ) == 0x00040000 ) { + printf( "write SRAM 4 at offset %08x: %08x\n", offset, data ); + } +# endif - stl_p(sram->data + offset, data); + stl_p( sram->data + offset, data ); } -static void -sram_put_halfword(void *opaque, target_phys_addr_t offset, uint32_t data) +static void sram_put_halfword( void* opaque, target_phys_addr_t offset, uint32_t data ) { - x49gp_sram_t *sram = opaque; + x49gp_sram_t* sram = opaque; - data &= 0xffff; + data &= 0xffff; -#ifdef DEBUG_X49GP_SYSRAM_WRITE - if ((offset & ~(0x0001ffff)) == 0x00000000) { - printf("write SRAM 2 at offset %08x: %04x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_ERAM_WRITE - if ((offset & ~(0x0001ffff)) == 0x00020000) { - printf("write SRAM 2 at offset %08x: %04x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_IRAM_WRITE - if ((offset & ~(0x0003ffff)) == 0x00040000) { - printf("write SRAM 2 at offset %08x: %04x\n", offset, data); - } -#endif +# ifdef DEBUG_X49GP_SYSRAM_WRITE + if ( ( offset & ~( 0x0001ffff ) ) == 0x00000000 ) { + printf( "write SRAM 2 at offset %08x: %04x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_ERAM_WRITE + if ( ( offset & ~( 0x0001ffff ) ) == 0x00020000 ) { + printf( "write SRAM 2 at offset %08x: %04x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_IRAM_WRITE + if ( ( offset & ~( 0x0003ffff ) ) == 0x00040000 ) { + printf( "write SRAM 2 at offset %08x: %04x\n", offset, data ); + } +# endif - stw_p(sram->data + offset, data); + stw_p( sram->data + offset, data ); } -static void -sram_put_byte(void *opaque, target_phys_addr_t offset, uint32_t data) +static void sram_put_byte( void* opaque, target_phys_addr_t offset, uint32_t data ) { - x49gp_sram_t *sram = opaque; + x49gp_sram_t* sram = opaque; - data &= 0xff; + data &= 0xff; -#ifdef DEBUG_X49GP_SYSRAM_WRITE - if ((offset & ~(0x0001ffff)) == 0x00000000) { - printf("write SRAM 1 at offset %08x: %02x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_ERAM_WRITE - if ((offset & ~(0x0001ffff)) == 0x00020000) { - printf("write SRAM 1 at offset %08x: %02x\n", offset, data); - } -#endif -#ifdef DEBUG_X49GP_IRAM_WRITE - if ((offset & ~(0x0003ffff)) == 0x00040000) { - printf("write SRAM 1 at offset %08x: %02x\n", offset, data); - } -#endif +# ifdef DEBUG_X49GP_SYSRAM_WRITE + if ( ( offset & ~( 0x0001ffff ) ) == 0x00000000 ) { + printf( "write SRAM 1 at offset %08x: %02x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_ERAM_WRITE + if ( ( offset & ~( 0x0001ffff ) ) == 0x00020000 ) { + printf( "write SRAM 1 at offset %08x: %02x\n", offset, data ); + } +# endif +# ifdef DEBUG_X49GP_IRAM_WRITE + if ( ( offset & ~( 0x0003ffff ) ) == 0x00040000 ) { + printf( "write SRAM 1 at offset %08x: %02x\n", offset, data ); + } +# endif - stb_p(sram->data + offset, data); + stb_p( sram->data + offset, data ); } -static CPUReadMemoryFunc *sram_readfn[] = -{ - sram_get_byte, - sram_get_halfword, - sram_get_word -}; +static CPUReadMemoryFunc* sram_readfn[] = { sram_get_byte, sram_get_halfword, sram_get_word }; -static CPUWriteMemoryFunc *sram_writefn[] = -{ - sram_put_byte, - sram_put_halfword, - sram_put_word -}; +static CPUWriteMemoryFunc* sram_writefn[] = { sram_put_byte, sram_put_halfword, sram_put_word }; #endif /* DEBUG_X49GP_SRAM */ -static int -sram_load(x49gp_module_t *module, GKeyFile *key) +static int sram_load( x49gp_module_t* module, GKeyFile* key ) { - x49gp_sram_t *sram = module->user_data; - char *filename; - int error; + x49gp_sram_t* sram = module->user_data; + char* filename; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - error = x49gp_module_get_filename(module, key, "filename", "sram", - &(sram->filename), &filename); + error = x49gp_module_get_filename( module, key, "filename", "sram", &( sram->filename ), &filename ); - sram->fd = open(filename, O_RDWR | O_CREAT, 0644); - if (sram->fd < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: open %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - return error; - } + sram->fd = open( filename, O_RDWR | O_CREAT, 0644 ); + if ( sram->fd < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: open %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + return error; + } - sram->size = 0x00080000; - if (ftruncate(sram->fd, sram->size) < 0) { - error = -errno; - fprintf(stderr, "%s: %s:%u: ftruncate %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(sram->fd); - sram->fd = -1; - return error; - } + sram->size = 0x00080000; + if ( ftruncate( sram->fd, sram->size ) < 0 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: ftruncate %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( sram->fd ); + sram->fd = -1; + return error; + } - sram->data = mmap(phys_ram_base + sram->offset, sram->size, - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, - sram->fd, 0); - if (sram->data == (void *) -1) { - error = -errno; - fprintf(stderr, "%s: %s:%u: mmap %s: %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(sram->fd); - sram->fd = -1; - return error; - } + sram->data = mmap( phys_ram_base + sram->offset, sram->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, sram->fd, 0 ); + if ( sram->data == ( void* )-1 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: mmap %s: %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( sram->fd ); + sram->fd = -1; + return error; + } - sram->shadow = mmap(phys_ram_base + sram->offset + sram->size, - sram->size, - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, - sram->fd, 0); - if (sram->shadow == (void *) -1) { - error = -errno; - fprintf(stderr, "%s: %s:%u: mmap %s (shadow): %s\n", - module->name, __FUNCTION__, __LINE__, - filename, strerror(errno)); - g_free(filename); - close(sram->fd); - sram->fd = -1; - return error; - } + sram->shadow = + mmap( phys_ram_base + sram->offset + sram->size, sram->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, sram->fd, 0 ); + if ( sram->shadow == ( void* )-1 ) { + error = -errno; + fprintf( stderr, "%s: %s:%u: mmap %s (shadow): %s\n", module->name, __FUNCTION__, __LINE__, filename, strerror( errno ) ); + g_free( filename ); + close( sram->fd ); + sram->fd = -1; + return error; + } - sram->x49gp->sram = phys_ram_base + sram->offset; + sram->x49gp->sram = phys_ram_base + sram->offset; - g_free(filename); - return error; + g_free( filename ); + return error; } -static int -sram_save(x49gp_module_t *module, GKeyFile *key) +static int sram_save( x49gp_module_t* module, GKeyFile* key ) { - x49gp_sram_t *sram = module->user_data; - int error; + x49gp_sram_t* sram = module->user_data; + int error; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - x49gp_module_set_filename(module, key, "filename", sram->filename); + x49gp_module_set_filename( module, key, "filename", sram->filename ); - error = msync(sram->data, sram->size, MS_ASYNC); - if (error) { - fprintf(stderr, "%s:%u: msync: %s\n", - __FUNCTION__, __LINE__, strerror(errno)); - return error; - } + error = msync( sram->data, sram->size, MS_ASYNC ); + if ( error ) { + fprintf( stderr, "%s:%u: msync: %s\n", __FUNCTION__, __LINE__, strerror( errno ) ); + return error; + } - error = fsync(sram->fd); - if (error) { - fprintf(stderr, "%s:%u: fsync: %s\n", - __FUNCTION__, __LINE__, strerror(errno)); - return error; - } + error = fsync( sram->fd ); + if ( error ) { + fprintf( stderr, "%s:%u: fsync: %s\n", __FUNCTION__, __LINE__, strerror( errno ) ); + return error; + } - return 0; + return 0; } -static int -sram_reset(x49gp_module_t *module, x49gp_reset_t reset) +static int sram_reset( x49gp_module_t* module, x49gp_reset_t reset ) { #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - return 0; + return 0; } -static int -sram_init(x49gp_module_t *module) +static int sram_init( x49gp_module_t* module ) { - x49gp_sram_t *sram; + x49gp_sram_t* sram; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - sram = malloc(sizeof(x49gp_sram_t)); - if (NULL == sram) { - fprintf(stderr, "%s:%u: Out of memory\n", - __FUNCTION__, __LINE__); - return -ENOMEM; - } - memset(sram, 0, sizeof(x49gp_sram_t)); + sram = malloc( sizeof( x49gp_sram_t ) ); + if ( NULL == sram ) { + fprintf( stderr, "%s:%u: Out of memory\n", __FUNCTION__, __LINE__ ); + return -ENOMEM; + } + memset( sram, 0, sizeof( x49gp_sram_t ) ); - sram->fd = -1; + sram->fd = -1; - module->user_data = sram; - sram->x49gp = module->x49gp; + module->user_data = sram; + sram->x49gp = module->x49gp; - sram->data = (void *) -1; - sram->shadow = (void *) -1; - sram->offset = phys_ram_size; - phys_ram_size += S3C2410_SRAM_SIZE; - phys_ram_size += S3C2410_SRAM_SIZE; + sram->data = ( void* )-1; + sram->shadow = ( void* )-1; + sram->offset = phys_ram_size; + phys_ram_size += S3C2410_SRAM_SIZE; + phys_ram_size += S3C2410_SRAM_SIZE; #ifdef DEBUG_X49GP_SRAM -{ - int iotype; + { + int iotype; - iotype = cpu_register_io_memory(0, sram_readfn, sram_writefn, sram); -printf("%s: iotype %08x\n", __FUNCTION__, iotype); - cpu_register_physical_memory(S3C2410_SRAM_BASE, - S3C2410_SRAM_SIZE, - sram->offset | iotype | IO_MEM_ROMD); + iotype = cpu_register_io_memory( 0, sram_readfn, sram_writefn, sram ); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); + cpu_register_physical_memory( S3C2410_SRAM_BASE, S3C2410_SRAM_SIZE, sram->offset | iotype | IO_MEM_ROMD ); - iotype = cpu_register_io_memory(0, sram_readfn, sram_writefn, sram); -printf("%s: iotype %08x\n", __FUNCTION__, iotype); - cpu_register_physical_memory(S3C2410_SRAM_BASE + S3C2410_SRAM_SIZE, - S3C2410_SRAM_SIZE, - (sram->offset + S3C2410_SRAM_SIZE) | iotype | IO_MEM_ROMD); -} + iotype = cpu_register_io_memory( 0, sram_readfn, sram_writefn, sram ); + printf( "%s: iotype %08x\n", __FUNCTION__, iotype ); + cpu_register_physical_memory( S3C2410_SRAM_BASE + S3C2410_SRAM_SIZE, S3C2410_SRAM_SIZE, + ( sram->offset + S3C2410_SRAM_SIZE ) | iotype | IO_MEM_ROMD ); + } #else - cpu_register_physical_memory(S3C2410_SRAM_BASE, S3C2410_SRAM_SIZE, - sram->offset | IO_MEM_RAM); - cpu_register_physical_memory(S3C2410_SRAM_BASE + S3C2410_SRAM_SIZE, - S3C2410_SRAM_SIZE, - (sram->offset + S3C2410_SRAM_SIZE) | IO_MEM_RAM); + cpu_register_physical_memory( S3C2410_SRAM_BASE, S3C2410_SRAM_SIZE, sram->offset | IO_MEM_RAM ); + cpu_register_physical_memory( S3C2410_SRAM_BASE + S3C2410_SRAM_SIZE, S3C2410_SRAM_SIZE, + ( sram->offset + S3C2410_SRAM_SIZE ) | IO_MEM_RAM ); #endif - return 0; + return 0; } -static int -sram_exit(x49gp_module_t *module) +static int sram_exit( x49gp_module_t* module ) { - x49gp_sram_t *sram; + x49gp_sram_t* sram; #ifdef DEBUG_X49GP_MODULES - printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__); + printf( "%s: %s:%u\n", module->name, __FUNCTION__, __LINE__ ); #endif - if (module->user_data) { - sram = module->user_data; + if ( module->user_data ) { + sram = module->user_data; - if (sram->shadow != (void *) -1) { - munmap(sram->shadow, sram->size); - } - if (sram->data != (void *) -1) { - munmap(sram->data, sram->size); - } - if (sram->fd >= 0) { - close(sram->fd); - } + if ( sram->shadow != ( void* )-1 ) { + munmap( sram->shadow, sram->size ); + } + if ( sram->data != ( void* )-1 ) { + munmap( sram->data, sram->size ); + } + if ( sram->fd >= 0 ) { + close( sram->fd ); + } - free(sram); - } + free( sram ); + } - x49gp_module_unregister(module); - free(module); + x49gp_module_unregister( module ); + free( module ); - return 0; + return 0; } -int -x49gp_sram_init(x49gp_t *x49gp) +int x49gp_sram_init( x49gp_t* x49gp ) { - x49gp_module_t *module; + x49gp_module_t* module; - if (x49gp_module_init(x49gp, "sram", sram_init, sram_exit, - sram_reset, sram_load, sram_save, NULL, - &module)) { - return -1; - } + if ( x49gp_module_init( x49gp, "sram", sram_init, sram_exit, sram_reset, sram_load, sram_save, NULL, &module ) ) { + return -1; + } - return x49gp_module_register(module); + return x49gp_module_register( module ); } diff --git a/src/symbol.c b/src/symbol.c index b17d018..0128974 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -9,271 +9,184 @@ #include "symbol.h" -static const cairo_path_data_t symbol_square_path_data[] = -{ - SYMBOL_MOVE_TO( 0.100, 0.100 ), - SYMBOL_LINE_TO( 0.500, 0.000 ), - SYMBOL_LINE_TO( 0.000, 0.500 ), - SYMBOL_LINE_TO( -0.500, 0.000 ), - SYMBOL_CLOSE_PATH() -}; +static const cairo_path_data_t symbol_square_path_data[] = { SYMBOL_MOVE_TO( 0.100, 0.100 ), SYMBOL_LINE_TO( 0.500, 0.000 ), + SYMBOL_LINE_TO( 0.000, 0.500 ), SYMBOL_LINE_TO( -0.500, 0.000 ), + SYMBOL_CLOSE_PATH() }; -SYMBOL(square, 0.7, 0.0, 0.1, 0.1, 0.6, 0.6); +SYMBOL( square, 0.7, 0.0, 0.1, 0.1, 0.6, 0.6 ); +static const cairo_path_data_t symbol_triangleup_path_data[] = { SYMBOL_MOVE_TO( 0.100, 0.000 ), SYMBOL_LINE_TO( 0.800, 0.000 ), + SYMBOL_LINE_TO( -0.400, 0.693 ), SYMBOL_CLOSE_PATH() }; -static const cairo_path_data_t symbol_triangleup_path_data[] = -{ - SYMBOL_MOVE_TO( 0.100, 0.000 ), - SYMBOL_LINE_TO( 0.800, 0.000 ), - SYMBOL_LINE_TO( -0.400, 0.693 ), - SYMBOL_CLOSE_PATH() -}; +SYMBOL( triangleup, 1.0, 0.0, 0.1, 0.0, 0.9, 0.693 ); -SYMBOL(triangleup, 1.0, 0.0, 0.1, 0.0, 0.9, 0.693); +static const cairo_path_data_t symbol_triangleright_path_data[] = { SYMBOL_MOVE_TO( 0.100, 0.000 ), SYMBOL_LINE_TO( 0.000, 0.800 ), + SYMBOL_LINE_TO( 0.693, -0.400 ), SYMBOL_CLOSE_PATH() }; +SYMBOL( triangleright, 0.893, 0.0, 0.1, 0.0, 0.793, 0.8 ); -static const cairo_path_data_t symbol_triangleright_path_data[] = -{ - SYMBOL_MOVE_TO( 0.100, 0.000 ), - SYMBOL_LINE_TO( 0.000, 0.800 ), - SYMBOL_LINE_TO( 0.693, -0.400 ), - SYMBOL_CLOSE_PATH() -}; +static const cairo_path_data_t symbol_arrowleftdblfull_path_data[] = { + SYMBOL_MOVE_TO( 0.100, 0.370 ), SYMBOL_LINE_TO( 0.652, 0.500 ), SYMBOL_LINE_TO( 0.000, -0.250 ), SYMBOL_LINE_TO( 1.000, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.500 ), SYMBOL_LINE_TO( -1.000, 0.000 ), SYMBOL_LINE_TO( 0.000, -0.250 ), SYMBOL_CLOSE_PATH() }; -SYMBOL(triangleright, 0.893, 0.0, 0.1, 0.0, 0.793, 0.8); +SYMBOL( arrowleftdblfull, 1.852, 0.0, 0.1, -0.13, 1.752, 0.87 ); +static const cairo_path_data_t symbol_uparrowleft_path_data[] = { SYMBOL_MOVE_TO( 0.100, 0.500 ), SYMBOL_LINE_TO( 0.600, 0.200 ), + SYMBOL_LINE_TO( 0.000, -0.150 ), SYMBOL_LINE_TO( 0.500, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.550 ), SYMBOL_LINE_TO( -0.100, 0.000 ), + SYMBOL_LINE_TO( 0.000, 0.450 ), SYMBOL_LINE_TO( -0.400, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.150 ), SYMBOL_CLOSE_PATH() }; -static const cairo_path_data_t symbol_arrowleftdblfull_path_data[] = -{ - SYMBOL_MOVE_TO( 0.100, 0.370 ), - SYMBOL_LINE_TO( 0.652, 0.500 ), - SYMBOL_LINE_TO( 0.000, -0.250 ), - SYMBOL_LINE_TO( 1.000, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.500 ), - SYMBOL_LINE_TO( -1.000, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.250 ), - SYMBOL_CLOSE_PATH() -}; +SYMBOL( uparrowleft, 1.3, 0.0, 0.1, 0.0, 1.2, 0.7 ); -SYMBOL(arrowleftdblfull, 1.852, 0.0, 0.1, -0.13, 1.752, 0.87); +static const cairo_path_data_t symbol_uparrowright_path_data[] = { SYMBOL_MOVE_TO( 1.200, 0.500 ), SYMBOL_LINE_TO( -0.600, 0.200 ), + SYMBOL_LINE_TO( 0.000, -0.150 ), SYMBOL_LINE_TO( -0.500, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.550 ), SYMBOL_LINE_TO( 0.100, 0.000 ), + SYMBOL_LINE_TO( 0.000, 0.450 ), SYMBOL_LINE_TO( 0.400, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.150 ), SYMBOL_CLOSE_PATH() }; -static const cairo_path_data_t symbol_uparrowleft_path_data[] = -{ - SYMBOL_MOVE_TO( 0.100, 0.500 ), - SYMBOL_LINE_TO( 0.600, 0.200 ), - SYMBOL_LINE_TO( 0.000, -0.150 ), - SYMBOL_LINE_TO( 0.500, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.550 ), - SYMBOL_LINE_TO( -0.100, 0.000 ), - SYMBOL_LINE_TO( 0.000, 0.450 ), - SYMBOL_LINE_TO( -0.400, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.150 ), - SYMBOL_CLOSE_PATH() -}; +SYMBOL( uparrowright, 1.3, 0.0, 0.1, 0.0, 1.2, 0.7 ); -SYMBOL(uparrowleft, 1.3, 0.0, 0.1, 0.0, 1.2, 0.7); +static const cairo_path_data_t symbol_tick_path_data[] = { SYMBOL_MOVE_TO( 0.100, 0.400 ), SYMBOL_LINE_TO( 0.000, 0.450 ), + SYMBOL_LINE_TO( 0.150, 0.000 ), SYMBOL_LINE_TO( 0.000, -0.450 ), + SYMBOL_CLOSE_PATH() }; +SYMBOL( tick, 0.35, 0.0, 0.1, 0.4, 0.25, 0.85 ); -static const cairo_path_data_t symbol_uparrowright_path_data[] = -{ - SYMBOL_MOVE_TO( 1.200, 0.500 ), - SYMBOL_LINE_TO( -0.600, 0.200 ), - SYMBOL_LINE_TO( 0.000, -0.150 ), - SYMBOL_LINE_TO( -0.500, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.550 ), - SYMBOL_LINE_TO( 0.100, 0.000 ), - SYMBOL_LINE_TO( 0.000, 0.450 ), - SYMBOL_LINE_TO( 0.400, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.150 ), - SYMBOL_CLOSE_PATH() -}; +static const cairo_path_data_t symbol_radical_path_data[] = { SYMBOL_MOVE_TO( 0.000, 0.500 ), SYMBOL_LINE_TO( 0.214, 0.100 ), + SYMBOL_LINE_TO( 0.213, -0.456 ), SYMBOL_LINE_TO( 0.229, 0.856 ), + SYMBOL_LINE_TO( 0.077, 0.000 ), SYMBOL_LINE_TO( 0.000, -0.100 ), + SYMBOL_LINE_TO( -0.281, -1.050 ), SYMBOL_LINE_TO( -0.287, 0.616 ), + SYMBOL_LINE_TO( -0.123, -0.057 ), SYMBOL_CLOSE_PATH() }; -SYMBOL(uparrowright, 1.3, 0.0, 0.1, 0.0, 1.2, 0.7); +SYMBOL( radical, 0.733, 0.0, 0.0, -0.15, 0.733, 1.0 ); +static const cairo_path_data_t symbol_overscore_path_data[] = { SYMBOL_MOVE_TO( 0.000, 1.000 ), SYMBOL_LINE_TO( 0.900, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.100 ), SYMBOL_LINE_TO( -0.900, 0.000 ), + SYMBOL_CLOSE_PATH() }; -static const cairo_path_data_t symbol_tick_path_data[] = -{ - SYMBOL_MOVE_TO( 0.100, 0.400 ), - SYMBOL_LINE_TO( 0.000, 0.450 ), - SYMBOL_LINE_TO( 0.150, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.450 ), - SYMBOL_CLOSE_PATH() -}; +SYMBOL( overscore, 0.8, 0.0, 0.0, 0.9, 0.9, 1.0 ); -SYMBOL(tick, 0.35, 0.0, 0.1, 0.4, 0.25, 0.85); +static const cairo_path_data_t symbol_minus_path_data[] = { SYMBOL_MOVE_TO( 0.050, 0.312 ), SYMBOL_LINE_TO( 0.000, 0.118 ), + SYMBOL_LINE_TO( 0.500, 0.000 ), SYMBOL_LINE_TO( 0.000, -0.118 ), + SYMBOL_CLOSE_PATH() }; +SYMBOL( minus, 0.6, 0.0, 0.05, 0.312, 0.55, 0.430 ); -static const cairo_path_data_t symbol_radical_path_data[] = -{ - SYMBOL_MOVE_TO( 0.000, 0.500 ), - SYMBOL_LINE_TO( 0.214, 0.100 ), - SYMBOL_LINE_TO( 0.213, -0.456 ), - SYMBOL_LINE_TO( 0.229, 0.856 ), - SYMBOL_LINE_TO( 0.077, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.100 ), - SYMBOL_LINE_TO( -0.281, -1.050 ), - SYMBOL_LINE_TO( -0.287, 0.616 ), - SYMBOL_LINE_TO( -0.123, -0.057 ), - SYMBOL_CLOSE_PATH() -}; +static const cairo_path_data_t symbol_divide_path_data[] = { SYMBOL_MOVE_TO( 0.050, 0.312 ), + SYMBOL_LINE_TO( 0.000, 0.118 ), + SYMBOL_LINE_TO( 0.500, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.118 ), + SYMBOL_CLOSE_PATH(), + SYMBOL_MOVE_TO( 0.180, -0.108 ), + SYMBOL_LINE_TO( 0.135, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.135 ), + SYMBOL_LINE_TO( -0.135, 0.000 ), + SYMBOL_CLOSE_PATH(), + SYMBOL_MOVE_TO( 0.000, 0.334 ), + SYMBOL_LINE_TO( 0.000, 0.135 ), + SYMBOL_LINE_TO( 0.135, 0.000 ), + SYMBOL_LINE_TO( 0.000, -0.135 ), + SYMBOL_CLOSE_PATH() }; -SYMBOL(radical, 0.733, 0.0, 0.0, -0.15, 0.733, 1.0); +SYMBOL( divide, 0.6, 0.0, 0.05, 0.069, 0.55, 0.673 ); +static const cairo_path_data_t symbol_divisionslash_path_data[] = { SYMBOL_MOVE_TO( 0.050, 0.000 ), SYMBOL_LINE_TO( 0.345, 0.739 ), + SYMBOL_LINE_TO( 0.130, 0.000 ), SYMBOL_LINE_TO( -0.345, -0.739 ), + SYMBOL_CLOSE_PATH() }; -static const cairo_path_data_t symbol_overscore_path_data[] = -{ - SYMBOL_MOVE_TO( 0.000, 1.000 ), - SYMBOL_LINE_TO( 0.900, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.100 ), - SYMBOL_LINE_TO( -0.900, 0.000 ), - SYMBOL_CLOSE_PATH() -}; +SYMBOL( divisionslash, 0.575, 0.000, 0.050, 0.000, 0.525, 0.739 ); -SYMBOL(overscore, 0.8, 0.0, 0.0, 0.9, 0.9, 1.0); - - -static const cairo_path_data_t symbol_minus_path_data[] = -{ - SYMBOL_MOVE_TO( 0.050, 0.312 ), - SYMBOL_LINE_TO( 0.000, 0.118 ), - SYMBOL_LINE_TO( 0.500, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.118 ), - SYMBOL_CLOSE_PATH() -}; - -SYMBOL(minus, 0.6, 0.0, 0.05, 0.312, 0.55, 0.430); - - -static const cairo_path_data_t symbol_divide_path_data[] = -{ - SYMBOL_MOVE_TO( 0.050, 0.312 ), - SYMBOL_LINE_TO( 0.000, 0.118 ), - SYMBOL_LINE_TO( 0.500, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.118 ), - SYMBOL_CLOSE_PATH(), - SYMBOL_MOVE_TO( 0.180, -0.108 ), - SYMBOL_LINE_TO( 0.135, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.135 ), - SYMBOL_LINE_TO( -0.135, 0.000 ), - SYMBOL_CLOSE_PATH(), - SYMBOL_MOVE_TO( 0.000, 0.334 ), - SYMBOL_LINE_TO( 0.000, 0.135 ), - SYMBOL_LINE_TO( 0.135, 0.000 ), - SYMBOL_LINE_TO( 0.000, -0.135 ), - SYMBOL_CLOSE_PATH() -}; - -SYMBOL(divide, 0.6, 0.0, 0.05, 0.069, 0.55, 0.673); - - -static const cairo_path_data_t symbol_divisionslash_path_data[] = -{ - SYMBOL_MOVE_TO( 0.050, 0.000 ), - SYMBOL_LINE_TO( 0.345, 0.739 ), - SYMBOL_LINE_TO( 0.130, 0.000 ), - SYMBOL_LINE_TO( -0.345, -0.739 ), - SYMBOL_CLOSE_PATH() -}; - -SYMBOL(divisionslash, 0.575, 0.000, 0.050, 0.000, 0.525, 0.739); - - -CONTROL(beginsuperscript, 0.0, 0.5, 1.0, 0.8); -CONTROL(endsuperscript, 0.0, 0.5, 1.25, 1.0); - -CONTROL(kern_m1, -0.1, 0.0, 1.0, 1.0); -CONTROL(kern_m2, -0.2, 0.0, 1.0, 1.0); -CONTROL(kern_m3, -0.3, 0.0, 1.0, 1.0); -CONTROL(kern_m4, -0.4, 0.0, 1.0, 1.0); -CONTROL(kern_m5, -0.5, 0.0, 1.0, 1.0); -CONTROL(kern_m6, -0.6, 0.0, 1.0, 1.0); -CONTROL(kern_m7, -0.7, 0.0, 1.0, 1.0); -CONTROL(kern_m8, -0.8, 0.0, 1.0, 1.0); -CONTROL(kern_m9, -0.9, 0.0, 1.0, 1.0); +CONTROL( beginsuperscript, 0.0, 0.5, 1.0, 0.8 ); +CONTROL( endsuperscript, 0.0, 0.5, 1.25, 1.0 ); +CONTROL( kern_m1, -0.1, 0.0, 1.0, 1.0 ); +CONTROL( kern_m2, -0.2, 0.0, 1.0, 1.0 ); +CONTROL( kern_m3, -0.3, 0.0, 1.0, 1.0 ); +CONTROL( kern_m4, -0.4, 0.0, 1.0, 1.0 ); +CONTROL( kern_m5, -0.5, 0.0, 1.0, 1.0 ); +CONTROL( kern_m6, -0.6, 0.0, 1.0, 1.0 ); +CONTROL( kern_m7, -0.7, 0.0, 1.0, 1.0 ); +CONTROL( kern_m8, -0.8, 0.0, 1.0, 1.0 ); +CONTROL( kern_m9, -0.9, 0.0, 1.0, 1.0 ); typedef struct { - const char *name; - const x49gp_symbol_t *symbol; + const char* name; + const x49gp_symbol_t* symbol; } symbol_name_t; -static const symbol_name_t symbol_names[] = -{ - { ".notdef", &symbol_square }, - { "arrowleftdblfull", &symbol_arrowleftdblfull }, - { "divide", &symbol_divide }, - { "divisionslash", &symbol_divisionslash }, - { "minus", &symbol_minus }, - { "overscore", &symbol_overscore }, - { "radical", &symbol_radical }, - { "square", &symbol_square }, - { "tick", &symbol_tick }, - { "triangleright", &symbol_triangleright }, - { "triangleup", &symbol_triangleup }, - { "uparrowleft", &symbol_uparrowleft }, - { "uparrowright", &symbol_uparrowright }, - { "super", &symbol_beginsuperscript }, - { "/super", &symbol_endsuperscript }, - { "kern-1", &symbol_kern_m1 }, - { "kern-2", &symbol_kern_m2 }, - { "kern-3", &symbol_kern_m3 }, - { "kern-4", &symbol_kern_m4 }, - { "kern-5", &symbol_kern_m5 }, - { "kern-6", &symbol_kern_m6 }, - { "kern-7", &symbol_kern_m7 }, - { "kern-8", &symbol_kern_m8 }, - { "kern-9", &symbol_kern_m9 }, - { NULL, NULL } +static const symbol_name_t symbol_names[] = { + {".notdef", &symbol_square }, + {"arrowleftdblfull", &symbol_arrowleftdblfull}, + {"divide", &symbol_divide }, + {"divisionslash", &symbol_divisionslash }, + {"minus", &symbol_minus }, + {"overscore", &symbol_overscore }, + {"radical", &symbol_radical }, + {"square", &symbol_square }, + {"tick", &symbol_tick }, + {"triangleright", &symbol_triangleright }, + {"triangleup", &symbol_triangleup }, + {"uparrowleft", &symbol_uparrowleft }, + {"uparrowright", &symbol_uparrowright }, + {"super", &symbol_beginsuperscript}, + {"/super", &symbol_endsuperscript }, + {"kern-1", &symbol_kern_m1 }, + {"kern-2", &symbol_kern_m2 }, + {"kern-3", &symbol_kern_m3 }, + {"kern-4", &symbol_kern_m4 }, + {"kern-5", &symbol_kern_m5 }, + {"kern-6", &symbol_kern_m6 }, + {"kern-7", &symbol_kern_m7 }, + {"kern-8", &symbol_kern_m8 }, + {"kern-9", &symbol_kern_m9 }, + {NULL, NULL } }; -#define NR_SYMBOLS (sizeof(symbol_names) / sizeof(symbol_names[0]) - 1) +#define NR_SYMBOLS ( sizeof( symbol_names ) / sizeof( symbol_names[ 0 ] ) - 1 ) -int -symbol_lookup_glyph_by_name(const char *name, int namelen, gunichar *glyph) +int symbol_lookup_glyph_by_name( const char* name, int namelen, gunichar* glyph ) { - const symbol_name_t *symname; - int i = 0; + const symbol_name_t* symname; + int i = 0; - /* - * Code symbols as Unicode Private Use from U+E000 on... - */ + /* + * Code symbols as Unicode Private Use from U+E000 on... + */ - symname = symbol_names; - while (symname->name) { - if ((strlen(symname->name) == namelen) && - !strncmp(symname->name, name, namelen)) { - if (glyph) { - *glyph = 0xe000 + i; - } - return 1; - } + symname = symbol_names; + while ( symname->name ) { + if ( ( strlen( symname->name ) == namelen ) && !strncmp( symname->name, name, namelen ) ) { + if ( glyph ) { + *glyph = 0xe000 + i; + } + return 1; + } - symname++; - i++; - } + symname++; + i++; + } - return 0; + return 0; } -const x49gp_symbol_t * -symbol_get_by_glyph(gunichar glyph) +const x49gp_symbol_t* symbol_get_by_glyph( gunichar glyph ) { - int index = glyph - 0xe000; + int index = glyph - 0xe000; - if ((index >= 0) && (index < NR_SYMBOLS)) { - return symbol_names[index].symbol; - } + if ( ( index >= 0 ) && ( index < NR_SYMBOLS ) ) { + return symbol_names[ index ].symbol; + } - return NULL; + return NULL; } -const x49gp_symbol_t * -symbol_get_by_name(const char *name) +const x49gp_symbol_t* symbol_get_by_name( const char* name ) { - gunichar glyph; + gunichar glyph; - if (symbol_lookup_glyph_by_name(name, strlen(name), &glyph)) { - return symbol_get_by_glyph(glyph); - } + if ( symbol_lookup_glyph_by_name( name, strlen( name ), &glyph ) ) { + return symbol_get_by_glyph( glyph ); + } - return NULL; + return NULL; } diff --git a/src/symbol.h b/src/symbol.h index 53174f6..fb93d04 100644 --- a/src/symbol.h +++ b/src/symbol.h @@ -7,62 +7,73 @@ #include typedef struct { - const cairo_path_data_t *data; - int num_data; + const cairo_path_data_t* data; + int num_data; } symbol_path_t; typedef struct { - double x_advance; - double y_advance; - double llx; - double lly; - double urx; - double ury; - double prescale; - double postscale; - const symbol_path_t *path; + double x_advance; + double y_advance; + double llx; + double lly; + double urx; + double ury; + double prescale; + double postscale; + const symbol_path_t* path; } x49gp_symbol_t; -#define SYMBOL_MOVE_TO(x, y) \ - { header: { CAIRO_PATH_MOVE_TO, 2 } }, \ - { point: { x, y } } +#define SYMBOL_MOVE_TO( x, y ) \ + { \ + header : { CAIRO_PATH_MOVE_TO, 2 } \ +}, \ + { \ +point: \ + { \ + x, y \ + } \ + } -#define SYMBOL_LINE_TO(x, y) \ - { header: { CAIRO_PATH_LINE_TO, 2 } }, \ - { point: { x, y } } +#define SYMBOL_LINE_TO( x, y ) \ + { \ + header : { CAIRO_PATH_LINE_TO, 2 } \ + }, \ + { \ +point: \ + { \ + x, y \ + } \ + } -#define SYMBOL_CURVE_TO(x1, y1, x2, y2, x3, y3) \ - { header: { CAIRO_PATH_CURVE_TO, 4 } }, \ - { point: { x1, y1 } }, \ - { point: { x2, y2 } }, \ - { point: { x3, y3 } } +#define SYMBOL_CURVE_TO( x1, y1, x2, y2, x3, y3 ) \ + { \ + header : { CAIRO_PATH_CURVE_TO, 4 } \ + }, \ + { point : { x1, y1 } }, { point : { x2, y2 } }, \ + { \ +point: \ + { \ + x3, y3 \ + } \ + } -#define SYMBOL_CLOSE_PATH() \ - { header: { CAIRO_PATH_CLOSE_PATH, 1 } } +#define SYMBOL_CLOSE_PATH() \ + { \ + header : { CAIRO_PATH_CLOSE_PATH, 1 } \ + } -#define SYMBOL(name, x_advance, y_advance, llx, lly, urx, ury) \ -static const symbol_path_t symbol_##name##_path = \ -{ \ - symbol_##name##_path_data, \ - sizeof(symbol_##name##_path_data) / sizeof(cairo_path_data_t) \ -}; \ - \ -static const x49gp_symbol_t symbol_##name = \ -{ \ - x_advance, y_advance, llx, lly, urx, ury, 1.0, 1.0, \ - &symbol_##name##_path \ -} +#define SYMBOL( name, x_advance, y_advance, llx, lly, urx, ury ) \ + static const symbol_path_t symbol_##name##_path = { symbol_##name##_path_data, \ + sizeof( symbol_##name##_path_data ) / sizeof( cairo_path_data_t ) }; \ + \ + static const x49gp_symbol_t symbol_##name = { x_advance, y_advance, llx, lly, urx, ury, 1.0, 1.0, &symbol_##name##_path } -#define CONTROL(name, x_advance, y_advance, prescale, postscale) \ -static const x49gp_symbol_t symbol_##name = \ -{ \ - x_advance, y_advance, 0.0, 0.0, 0.0, 0.0, \ - prescale, postscale, NULL \ -} +#define CONTROL( name, x_advance, y_advance, prescale, postscale ) \ + static const x49gp_symbol_t symbol_##name = { x_advance, y_advance, 0.0, 0.0, 0.0, 0.0, prescale, postscale, NULL } -int symbol_lookup_glyph_by_name(const char *name, int namelen, gunichar *); +int symbol_lookup_glyph_by_name( const char* name, int namelen, gunichar* ); -const x49gp_symbol_t *symbol_get_by_name(const char *name); -const x49gp_symbol_t *symbol_get_by_glyph(gunichar glyph); +const x49gp_symbol_t* symbol_get_by_name( const char* name ); +const x49gp_symbol_t* symbol_get_by_glyph( gunichar glyph ); #endif /* !(_X49GP_SYMBOL_H) */ diff --git a/src/timer.c b/src/timer.c index 60d370b..d95929a 100644 --- a/src/timer.c +++ b/src/timer.c @@ -23,299 +23,254 @@ #include "gdbstub.h" typedef struct { - long type; + long type; } x49gp_clock_t; struct x49gp_timer_s { - long type; - int64_t expires; - x49gp_timer_cb_t cb; - void *user_data; - x49gp_timer_t *next; + long type; + int64_t expires; + x49gp_timer_cb_t cb; + void* user_data; + x49gp_timer_t* next; }; -typedef x49gp_timer_cb_t QEMUTimerCB; -typedef void * QEMUClock; -QEMUClock *rt_clock = (void *) X49GP_TIMER_REALTIME; -QEMUClock *vm_clock = (void *) X49GP_TIMER_VIRTUAL; -int64_t ticks_per_sec = 1000000; +typedef x49gp_timer_cb_t QEMUTimerCB; +typedef void* QEMUClock; +QEMUClock* rt_clock = ( void* )X49GP_TIMER_REALTIME; +QEMUClock* vm_clock = ( void* )X49GP_TIMER_VIRTUAL; +int64_t ticks_per_sec = 1000000; -static x49gp_timer_t *x49gp_timer_lists[2]; +static x49gp_timer_t* x49gp_timer_lists[ 2 ]; -int64_t -x49gp_get_clock(void) +int64_t x49gp_get_clock( void ) { - struct timeval tv; - int64_t us; + struct timeval tv; + int64_t us; - gettimeofday(&tv, NULL); + gettimeofday( &tv, NULL ); - us = tv.tv_sec * 1000000LL + tv.tv_usec; + us = tv.tv_sec * 1000000LL + tv.tv_usec; - return us; + return us; } -x49gp_timer_t * -x49gp_new_timer(long type, x49gp_timer_cb_t cb, void *user_data) +x49gp_timer_t* x49gp_new_timer( long type, x49gp_timer_cb_t cb, void* user_data ) { - x49gp_timer_t *ts; + x49gp_timer_t* ts; - ts = malloc(sizeof(x49gp_timer_t)); - if (NULL == ts) { - return NULL; - } - memset(ts, 0, sizeof(x49gp_timer_t)); + ts = malloc( sizeof( x49gp_timer_t ) ); + if ( NULL == ts ) { + return NULL; + } + memset( ts, 0, sizeof( x49gp_timer_t ) ); - ts->type = type; - ts->cb = cb; - ts->user_data = user_data; + ts->type = type; + ts->cb = cb; + ts->user_data = user_data; - return ts; + return ts; } -void -x49gp_free_timer(x49gp_timer_t *ts) +void x49gp_free_timer( x49gp_timer_t* ts ) { free( ts ); } + +void x49gp_del_timer( x49gp_timer_t* ts ) { - free(ts); + x49gp_timer_t **pt, *t; + + // printf("%s: ts %p\n", __FUNCTION__, ts); + pt = &x49gp_timer_lists[ ts->type ]; + while ( 1 ) { + t = *pt; + if ( NULL == t ) + break; + if ( t == ts ) { + *pt = t->next; + ts->next = NULL; + break; + } + pt = &t->next; + } } -void -x49gp_del_timer(x49gp_timer_t *ts) +void x49gp_mod_timer( x49gp_timer_t* ts, int64_t expires ) { - x49gp_timer_t **pt, *t; + x49gp_timer_t **pt, *t; -// printf("%s: ts %p\n", __FUNCTION__, ts); - pt = &x49gp_timer_lists[ts->type]; - while (1) { - t = *pt; - if (NULL == t) - break; - if (t == ts) { - *pt = t->next; - ts->next = NULL; - break; - } - pt = &t->next; - } + x49gp_del_timer( ts ); + + // printf("%s: ts %p, expires %lld\n", __FUNCTION__, ts, expires); + pt = &x49gp_timer_lists[ ts->type ]; + while ( 1 ) { + t = *pt; + if ( NULL == t ) + break; + if ( t->expires > expires ) + break; + pt = &t->next; + } + + ts->expires = expires; + ts->next = *pt; + *pt = ts; } -void -x49gp_mod_timer(x49gp_timer_t *ts, int64_t expires) +int x49gp_timer_pending( x49gp_timer_t* ts ) { - x49gp_timer_t **pt, *t; + x49gp_timer_t* t; - x49gp_del_timer(ts); + for ( t = x49gp_timer_lists[ ts->type ]; t; t = t->next ) { + if ( t == ts ) + return 1; + } -// printf("%s: ts %p, expires %lld\n", __FUNCTION__, ts, expires); - pt = &x49gp_timer_lists[ts->type]; - while (1) { - t = *pt; - if (NULL == t) - break; - if (t->expires > expires) - break; - pt = &t->next; - } - - ts->expires = expires; - ts->next = *pt; - *pt = ts; + return 0; } -int -x49gp_timer_pending(x49gp_timer_t *ts) +int64_t x49gp_timer_expires( x49gp_timer_t* ts ) { return ts->expires; } + +static int x49gp_timer_expired( x49gp_timer_t* timer_head, int64_t current_time ) { - x49gp_timer_t *t; - - for (t = x49gp_timer_lists[ts->type]; t; t = t->next) { - if (t == ts) - return 1; - } - - return 0; -} - -int64_t -x49gp_timer_expires(x49gp_timer_t *ts) -{ - return ts->expires; -} - -static int -x49gp_timer_expired(x49gp_timer_t *timer_head, int64_t current_time) -{ - if (NULL == timer_head) - return 0; - return (timer_head->expires <= current_time); + if ( NULL == timer_head ) + return 0; + return ( timer_head->expires <= current_time ); } /* LD TEMPO HACK */ -QEMUTimer * -qemu_new_timer(QEMUClock *clock, QEMUTimerCB cb, void *opaque) +QEMUTimer* qemu_new_timer( QEMUClock* clock, QEMUTimerCB cb, void* opaque ) { - return (void *) x49gp_new_timer((long) clock, cb, opaque); + return ( void* )x49gp_new_timer( ( long )clock, cb, opaque ); } -void -qemu_free_timer(QEMUTimer *ts) +void qemu_free_timer( QEMUTimer* ts ) { return x49gp_free_timer( ( void* )ts ); } + +void qemu_mod_timer( QEMUTimer* ts, int64_t expire_time ) { return x49gp_mod_timer( ( void* )ts, expire_time ); } + +void qemu_del_timer( QEMUTimer* ts ) { return x49gp_del_timer( ( void* )ts ); } + +int qemu_timer_pending( QEMUTimer* ts ) { return x49gp_timer_pending( ( void* )ts ); } + +int64_t qemu_get_clock( QEMUClock* clock ) { return x49gp_get_clock(); } + +static void x49gp_run_timers( x49gp_timer_t** ptimer_head, int64_t current_time ) { - return x49gp_free_timer((void *) ts); + x49gp_timer_t* ts; + + // printf("%s: now %lld\n", __FUNCTION__, current_time); + while ( 1 ) { + ts = *ptimer_head; + if ( NULL == ts || ts->expires > current_time ) + break; + + *ptimer_head = ts->next; + ts->next = NULL; + + // printf("%s: call ts %p\n", __FUNCTION__, ts); + ts->cb( ts->user_data ); + // printf("%s: ts %p done\n", __FUNCTION__, ts); + } + + // printf("%s: timers done\n", __FUNCTION__); } -void -qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) +static void x49gp_alarm_handler( int sig ) { - return x49gp_mod_timer((void *) ts, expire_time); + if ( x49gp_timer_expired( x49gp_timer_lists[ X49GP_TIMER_VIRTUAL ], x49gp_get_clock() ) || + x49gp_timer_expired( x49gp_timer_lists[ X49GP_TIMER_REALTIME ], x49gp_get_clock() ) ) { + if ( cpu_single_env && !cpu_single_env->exit_request ) { + cpu_exit( cpu_single_env ); + } + } } -void -qemu_del_timer(QEMUTimer *ts) +static void x49gp_main_loop_wait( x49gp_t* x49gp, int timeout ) { - return x49gp_del_timer((void *) ts); + // printf("%s: timeout: %d\n", __FUNCTION__, timeout); + + if ( gdb_poll( x49gp->env ) ) { + gdb_handlesig( x49gp->env, 0 ); + } else + poll( NULL, 0, timeout ); + + if ( x49gp->arm_idle != X49GP_ARM_OFF ) { + x49gp_run_timers( &x49gp_timer_lists[ X49GP_TIMER_VIRTUAL ], x49gp_get_clock() ); + } + + x49gp_run_timers( &x49gp_timer_lists[ X49GP_TIMER_REALTIME ], x49gp_get_clock() ); + + // printf("%s: done\n", __FUNCTION__); } -int -qemu_timer_pending(QEMUTimer *ts) +int x49gp_main_loop( x49gp_t* x49gp ) { - return x49gp_timer_pending((void *) ts); -} + int prev_idle; + int ret, timeout; -int64_t -qemu_get_clock(QEMUClock *clock) -{ - return x49gp_get_clock(); -} + while ( !x49gp->arm_exit ) { + prev_idle = x49gp->arm_idle; -static void -x49gp_run_timers(x49gp_timer_t **ptimer_head, int64_t current_time) -{ - x49gp_timer_t *ts; - -// printf("%s: now %lld\n", __FUNCTION__, current_time); - while (1) { - ts = *ptimer_head; - if (NULL == ts || ts->expires > current_time) - break; - - *ptimer_head = ts->next; - ts->next = NULL; - -// printf("%s: call ts %p\n", __FUNCTION__, ts); - ts->cb(ts->user_data); -// printf("%s: ts %p done\n", __FUNCTION__, ts); - } - -// printf("%s: timers done\n", __FUNCTION__); -} - -static void -x49gp_alarm_handler(int sig) -{ - if (x49gp_timer_expired(x49gp_timer_lists[X49GP_TIMER_VIRTUAL], - x49gp_get_clock()) || - x49gp_timer_expired(x49gp_timer_lists[X49GP_TIMER_REALTIME], - x49gp_get_clock())) { - if (cpu_single_env && ! cpu_single_env->exit_request) { - cpu_exit(cpu_single_env); - } - } -} - -static void -x49gp_main_loop_wait(x49gp_t *x49gp, int timeout) -{ -// printf("%s: timeout: %d\n", __FUNCTION__, timeout); - - if (gdb_poll(x49gp->env)) { - gdb_handlesig(x49gp->env, 0); - } else - poll(NULL, 0, timeout); - - if (x49gp->arm_idle != X49GP_ARM_OFF) { - x49gp_run_timers(&x49gp_timer_lists[X49GP_TIMER_VIRTUAL], - x49gp_get_clock()); - } - - x49gp_run_timers(&x49gp_timer_lists[X49GP_TIMER_REALTIME], - x49gp_get_clock()); - -// printf("%s: done\n", __FUNCTION__); -} - -int -x49gp_main_loop(x49gp_t *x49gp) -{ - int prev_idle; - int ret, timeout; - - while (! x49gp->arm_exit) { - prev_idle = x49gp->arm_idle; - - if (x49gp->arm_idle == X49GP_ARM_RUN) { + if ( x49gp->arm_idle == X49GP_ARM_RUN ) { #ifdef DEBUG_X49GP_TIMER_IDLE - printf("%lld: %s: call cpu_exec(%p)\n", (unsigned long long) x49gp_get_clock(), __FUNCTION__, x49gp->env); + printf( "%lld: %s: call cpu_exec(%p)\n", ( unsigned long long )x49gp_get_clock(), __FUNCTION__, x49gp->env ); #endif - ret = cpu_exec(x49gp->env); + ret = cpu_exec( x49gp->env ); #ifdef DEBUG_X49GP_TIMER_IDLE - printf("%lld: %s: cpu_exec(): %d, PC %08x\n", (unsigned long long) x49gp_get_clock(), __FUNCTION__, ret, x49gp->env->regs[15]); + printf( "%lld: %s: cpu_exec(): %d, PC %08x\n", ( unsigned long long )x49gp_get_clock(), __FUNCTION__, ret, + x49gp->env->regs[ 15 ] ); #endif -if (x49gp->env->regs[15] == 0x8620) { -printf("PC %08x: SRAM %08x: %08x %08x %08x <%08x>\n", x49gp->env->regs[15], 0x08000a0c, - * ((uint32_t *) &x49gp->sram[0x0a00]), - * ((uint32_t *) &x49gp->sram[0x0a04]), - * ((uint32_t *) &x49gp->sram[0x0a08]), - * ((uint32_t *) &x49gp->sram[0x0a0c])); -* ((uint32_t *) &x49gp->sram[0x0a0c]) = 0x00000000; + if ( x49gp->env->regs[ 15 ] == 0x8620 ) { + printf( "PC %08x: SRAM %08x: %08x %08x %08x <%08x>\n", x49gp->env->regs[ 15 ], 0x08000a0c, + *( ( uint32_t* )&x49gp->sram[ 0x0a00 ] ), *( ( uint32_t* )&x49gp->sram[ 0x0a04 ] ), + *( ( uint32_t* )&x49gp->sram[ 0x0a08 ] ), *( ( uint32_t* )&x49gp->sram[ 0x0a0c ] ) ); + *( ( uint32_t* )&x49gp->sram[ 0x0a0c ] ) = 0x00000000; + } + + if ( ret == EXCP_DEBUG ) { + gdb_handlesig( x49gp->env, SIGTRAP ); + continue; + } + + if ( x49gp->arm_idle != prev_idle ) { + if ( x49gp->arm_idle == X49GP_ARM_OFF ) { + x49gp_lcd_update( x49gp ); + cpu_reset( x49gp->env ); + } + } + + if ( ret == EXCP_HALTED ) { + timeout = 10; + } else { + timeout = 0; + } + } else { + timeout = 1; + } + + x49gp_main_loop_wait( x49gp, timeout ); + } + + return 0; } - if (ret == EXCP_DEBUG) { - gdb_handlesig(x49gp->env, SIGTRAP); - continue; - } - - if (x49gp->arm_idle != prev_idle) { - if (x49gp->arm_idle == X49GP_ARM_OFF) { - x49gp_lcd_update(x49gp); - cpu_reset(x49gp->env); - } - } - - if (ret == EXCP_HALTED) { - timeout = 10; - } else { - timeout = 0; - } - } else { - timeout = 1; - } - - x49gp_main_loop_wait(x49gp, timeout); - } - - return 0; -} - -int -x49gp_timer_init(x49gp_t *x49gp) +int x49gp_timer_init( x49gp_t* x49gp ) { - struct sigaction sa; - struct itimerval it; + struct sigaction sa; + struct itimerval it; - x49gp_timer_lists[X49GP_TIMER_VIRTUAL] = NULL; - x49gp_timer_lists[X49GP_TIMER_REALTIME] = NULL; + x49gp_timer_lists[ X49GP_TIMER_VIRTUAL ] = NULL; + x49gp_timer_lists[ X49GP_TIMER_REALTIME ] = NULL; - sigfillset(&sa.sa_mask); - sa.sa_flags = SA_RESTART; - sa.sa_handler = x49gp_alarm_handler; - sigaction(SIGALRM, &sa, NULL); + sigfillset( &sa.sa_mask ); + sa.sa_flags = SA_RESTART; + sa.sa_handler = x49gp_alarm_handler; + sigaction( SIGALRM, &sa, NULL ); - it.it_interval.tv_sec = 0; - it.it_interval.tv_usec = 1000; - it.it_value.tv_sec = 0; - it.it_value.tv_usec = 1000; + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 1000; + it.it_value.tv_sec = 0; + it.it_value.tv_usec = 1000; - setitimer(ITIMER_REAL, &it, NULL); - return 0; + setitimer( ITIMER_REAL, &it, NULL ); + return 0; } diff --git a/src/tiny_font.c b/src/tiny_font.c index 6dbce1f..97b34d4 100644 --- a/src/tiny_font.c +++ b/src/tiny_font.c @@ -94,113 +94,110 @@ #include "bitmaps/tiny__i.xbm" -const bitmap_font_t tiny_font = -{ - 7, - -3, - { - GLYPH(tiny, notdef), +const bitmap_font_t tiny_font = { + 7, + -3, + { GLYPH( tiny, notdef ), - SPACE("space", 4, 0), - GLYPH(tiny, quotedbl), - GLYPH(tiny, numbersign), - GLYPH(tiny, ampersand), - GLYPH(tiny, parenleft), - GLYPH(tiny, parenright), - GLYPH(tiny, comma), - GLYPH(tiny, hyphen), - GLYPH(tiny, period), - GLYPH(tiny, slash), + SPACE( "space", 4, 0 ), + GLYPH( tiny, quotedbl ), + GLYPH( tiny, numbersign ), + GLYPH( tiny, ampersand ), + GLYPH( tiny, parenleft ), + GLYPH( tiny, parenright ), + GLYPH( tiny, comma ), + GLYPH( tiny, hyphen ), + GLYPH( tiny, period ), + GLYPH( tiny, slash ), - GLYPH(tiny, zero), - GLYPH(tiny, one), - GLYPH(tiny, two), - GLYPH(tiny, three), + GLYPH( tiny, zero ), + GLYPH( tiny, one ), + GLYPH( tiny, two ), + GLYPH( tiny, three ), - GLYPH(tiny, colon), + GLYPH( tiny, colon ), - GLYPH(tiny, less), - GLYPH(tiny, equal), - GLYPH(tiny, greater), + GLYPH( tiny, less ), + GLYPH( tiny, equal ), + GLYPH( tiny, greater ), - GLYPH(tiny, A), - GLYPH(tiny, B), - GLYPH(tiny, C), - GLYPH(tiny, D), - GLYPH(tiny, E), - GLYPH(tiny, F), - GLYPH(tiny, G), - GLYPH(tiny, H), - GLYPH(tiny, I), - GLYPH(tiny, J), - GLYPH(tiny, K), - GLYPH(tiny, L), - GLYPH(tiny, M), - GLYPH(tiny, N), - GLYPH(tiny, O), - GLYPH(tiny, P), - GLYPH(tiny, Q), - GLYPH(tiny, R), - GLYPH(tiny, S), - GLYPH(tiny, T), - GLYPH(tiny, U), - GLYPH(tiny, V), - GLYPH(tiny, W), - GLYPH(tiny, X), - GLYPH(tiny, Y), - GLYPH(tiny, Z), + GLYPH( tiny, A ), + GLYPH( tiny, B ), + GLYPH( tiny, C ), + GLYPH( tiny, D ), + GLYPH( tiny, E ), + GLYPH( tiny, F ), + GLYPH( tiny, G ), + GLYPH( tiny, H ), + GLYPH( tiny, I ), + GLYPH( tiny, J ), + GLYPH( tiny, K ), + GLYPH( tiny, L ), + GLYPH( tiny, M ), + GLYPH( tiny, N ), + GLYPH( tiny, O ), + GLYPH( tiny, P ), + GLYPH( tiny, Q ), + GLYPH( tiny, R ), + GLYPH( tiny, S ), + GLYPH( tiny, T ), + GLYPH( tiny, U ), + GLYPH( tiny, V ), + GLYPH( tiny, W ), + GLYPH( tiny, X ), + GLYPH( tiny, Y ), + GLYPH( tiny, Z ), - GLYPH(tiny, bracketleft), - GLYPH(tiny, bracketright), - GLYPH(tiny, underscore), + GLYPH( tiny, bracketleft ), + GLYPH( tiny, bracketright ), + GLYPH( tiny, underscore ), - GLYPH(tiny, i), + GLYPH( tiny, i ), - GLYPH(tiny, overscore), - GLYPH(tiny, arrowleft), - GLYPH(tiny, arrowright), - GLYPH(tiny, guillemotleft), - GLYPH(tiny, guillemotright), + GLYPH( tiny, overscore ), + GLYPH( tiny, arrowleft ), + GLYPH( tiny, arrowright ), + GLYPH( tiny, guillemotleft ), + GLYPH( tiny, guillemotright ), - GLYPH(tiny, braceleft), - GLYPH(tiny, braceright), + GLYPH( tiny, braceleft ), + GLYPH( tiny, braceright ), - GLYPH(tiny, large_comma), + GLYPH( tiny, large_comma ), - GLYPH(tiny, xsuperior), - GLYPH(tiny, twosuperior), + GLYPH( tiny, xsuperior ), + GLYPH( tiny, twosuperior ), - GLYPH(tiny, math_e), - GLYPH(tiny, math_x), - GLYPH(tiny, math_y), - GLYPH(tiny, math_pi), - GLYPH(tiny, math_summation), - GLYPH(tiny, math_radical), - GLYPH(tiny, math_partialdiff), - GLYPH(tiny, math_integral), - GLYPH(tiny, math_infinity), + GLYPH( tiny, math_e ), + GLYPH( tiny, math_x ), + GLYPH( tiny, math_y ), + GLYPH( tiny, math_pi ), + GLYPH( tiny, math_summation ), + GLYPH( tiny, math_radical ), + GLYPH( tiny, math_partialdiff ), + GLYPH( tiny, math_integral ), + GLYPH( tiny, math_infinity ), - GLYPH(tiny, math_numbersign), - GLYPH(tiny, math_less), - GLYPH(tiny, math_greater), - GLYPH(tiny, math_lessequal), - GLYPH(tiny, math_greaterequal), - GLYPH(tiny, math_equal), - GLYPH(tiny, math_notequal), + GLYPH( tiny, math_numbersign ), + GLYPH( tiny, math_less ), + GLYPH( tiny, math_greater ), + GLYPH( tiny, math_lessequal ), + GLYPH( tiny, math_greaterequal ), + GLYPH( tiny, math_equal ), + GLYPH( tiny, math_notequal ), - GLYPH(tiny, math_arrowleft), - GLYPH(tiny, math_arrowright), - GLYPH(tiny, math_downarrowleft), - GLYPH(tiny, math_downarrowright), + GLYPH( tiny, math_arrowleft ), + GLYPH( tiny, math_arrowright ), + GLYPH( tiny, math_downarrowleft ), + GLYPH( tiny, math_downarrowright ), - SPACE("kern-1", -1, -1), - SPACE("kern-2", -2, -2), - SPACE("kern-3", -3, -3), - SPACE("kern-4", -4, -4), - SPACE("kern-5", -5, -5), - SPACE("kern-6", -6, -6), - SPACE("kern-7", -7, -7), + SPACE( "kern-1", -1, -1 ), + SPACE( "kern-2", -2, -2 ), + SPACE( "kern-3", -3, -3 ), + SPACE( "kern-4", -4, -4 ), + SPACE( "kern-5", -5, -5 ), + SPACE( "kern-6", -6, -6 ), + SPACE( "kern-7", -7, -7 ), - { NULL } - } + { NULL } } }; diff --git a/src/ui.c b/src/ui.c index c95f9e6..0d98f9c 100644 --- a/src/ui.c +++ b/src/ui.c @@ -1541,11 +1541,11 @@ static void x49gp_ui_color_init( GdkColor* color, u8 red, u8 green, u8 blue ) static void x49gp_ui_style_init( GtkStyle* style, GtkWidget* widget, GdkColor* fg, GdkColor* bg ) { for ( int i = 0; i < 5; i++ ) { - style->fg[ i ] = *fg; - style->bg[ i ] = *bg; + style->fg[ i ] = *fg; + style->bg[ i ] = *bg; - style->text[ i ] = style->fg[ i ]; - style->base[ i ] = style->bg[ i ]; + style->text[ i ] = style->fg[ i ]; + style->base[ i ] = style->bg[ i ]; } style->xthickness = 0; @@ -1571,14 +1571,14 @@ static GdkPixbuf* x49gp_pixbuf_new_from_inline( gint data_length, const guint8* magic = ntohl( pbd->magic ); if ( magic != PIXBUF_MAGIC ) - return NULL; + return NULL; length = ntohl( pbd->length ); /* The data comes from a C string, which contains a trailing \0. */ /* This will be counted by sizeof(), but the embedded header */ /* specifies the true size. Compensate by adding 1 to it. */ if ( length + 1 != data_length ) - return NULL; + return NULL; width = ntohl( pbd->width ); height = ntohl( pbd->height ); @@ -1599,33 +1599,33 @@ static int x49gp_ui_button_pixmaps_init( x49gp_t* x49gp, x49gp_ui_button_t* butt x49gp_ui_style_init( style, button->button, &ui->colors[ button->key->color ], &ui->colors[ UI_COLOR_BLACK ] ); for ( int i = 0; i < 5; i++ ) { - style->bg_pixmap[ i ] = gdk_pixmap_new( ui->window->window, button->key->width, button->key->height, -1 ); + style->bg_pixmap[ i ] = gdk_pixmap_new( ui->window->window, button->key->width, button->key->height, -1 ); - if ( i == GTK_STATE_ACTIVE ) { - if ( color == UI_COLOR_SILVER ) { - src = gdk_pixbuf_new_subpixbuf( ui->bg_pixbuf, ui->kb_x_offset + button->key->x, ui->kb_y_offset + button->key->y, - button->key->width, button->key->height ); - dst = gdk_pixbuf_copy( src ); - g_object_unref( src ); + if ( i == GTK_STATE_ACTIVE ) { + if ( color == UI_COLOR_SILVER ) { + src = gdk_pixbuf_new_subpixbuf( ui->bg_pixbuf, ui->kb_x_offset + button->key->x, ui->kb_y_offset + button->key->y, + button->key->width, button->key->height ); + dst = gdk_pixbuf_copy( src ); + g_object_unref( src ); - src = x49gp_pixbuf_new_from_inline( sizeof( button_round ), button_round ); + src = x49gp_pixbuf_new_from_inline( sizeof( button_round ), button_round ); - gdk_pixbuf_composite( src, dst, 0, 0, button->key->width, button->key->height, 0.0, 0.0, 1.0, 1.0, GDK_INTERP_HYPER, 0xff ); + gdk_pixbuf_composite( src, dst, 0, 0, button->key->width, button->key->height, 0.0, 0.0, 1.0, 1.0, GDK_INTERP_HYPER, 0xff ); - g_object_unref( src ); - src = dst; - } else - src = gdk_pixbuf_new_subpixbuf( ui->bg_pixbuf, ui->kb_x_offset + button->key->x, ui->kb_y_offset + button->key->y + 1, - button->key->width, button->key->height ); + g_object_unref( src ); + src = dst; + } else + src = gdk_pixbuf_new_subpixbuf( ui->bg_pixbuf, ui->kb_x_offset + button->key->x, ui->kb_y_offset + button->key->y + 1, + button->key->width, button->key->height ); - } else - src = gdk_pixbuf_new_subpixbuf( ui->bg_pixbuf, ui->kb_x_offset + button->key->x, ui->kb_y_offset + button->key->y, - button->key->width, button->key->height ); + } else + src = gdk_pixbuf_new_subpixbuf( ui->bg_pixbuf, ui->kb_x_offset + button->key->x, ui->kb_y_offset + button->key->y, + button->key->width, button->key->height ); - gdk_draw_pixbuf( style->bg_pixmap[ i ], ui->window->style->black_gc, src, 0, 0, 0, 0, button->key->width, button->key->height, - GDK_RGB_DITHER_NORMAL, 0, 0 ); + gdk_draw_pixbuf( style->bg_pixmap[ i ], ui->window->style->black_gc, src, 0, 0, 0, 0, button->key->width, button->key->height, + GDK_RGB_DITHER_NORMAL, 0, 0 ); - g_object_unref( src ); + g_object_unref( src ); } gtk_widget_set_style( button->button, style ); @@ -1640,57 +1640,57 @@ static void x49gp_ui_symbol_path( cairo_t* cr, double size, double xoffset, doub path = symbol->path; if ( NULL == path ) - return; + return; cairo_move_to( cr, xoffset, yoffset ); for ( int i = 0; i < path->num_data; i += path->data[ i ].header.length ) { - data = &path->data[ i ]; + data = &path->data[ i ]; - switch ( data->header.type ) { - case CAIRO_PATH_MOVE_TO: - cairo_rel_move_to( cr, size * data[ 1 ].point.x, -size * data[ 1 ].point.y ); - break; - case CAIRO_PATH_LINE_TO: - cairo_rel_line_to( cr, size * data[ 1 ].point.x, -size * data[ 1 ].point.y ); - break; - case CAIRO_PATH_CURVE_TO: - cairo_rel_curve_to( cr, size * data[ 1 ].point.x, -size * data[ 1 ].point.y, size * data[ 2 ].point.x, - -size * data[ 2 ].point.y, size * data[ 3 ].point.x, -size * data[ 3 ].point.y ); - break; - case CAIRO_PATH_CLOSE_PATH: - cairo_close_path( cr ); - break; - } + switch ( data->header.type ) { + case CAIRO_PATH_MOVE_TO: + cairo_rel_move_to( cr, size * data[ 1 ].point.x, -size * data[ 1 ].point.y ); + break; + case CAIRO_PATH_LINE_TO: + cairo_rel_line_to( cr, size * data[ 1 ].point.x, -size * data[ 1 ].point.y ); + break; + case CAIRO_PATH_CURVE_TO: + cairo_rel_curve_to( cr, size * data[ 1 ].point.x, -size * data[ 1 ].point.y, size * data[ 2 ].point.x, + -size * data[ 2 ].point.y, size * data[ 3 ].point.x, -size * data[ 3 ].point.y ); + break; + case CAIRO_PATH_CLOSE_PATH: + cairo_close_path( cr ); + break; + } } } static void x49gp_ui_draw_symbol( cairo_t* cr, GdkColor* color, double size, double line_width, gboolean fill, double xoffset, - double yoffset, const x49gp_symbol_t* symbol ) + double yoffset, const x49gp_symbol_t* symbol ) { cairo_set_line_cap( cr, CAIRO_LINE_CAP_BUTT ); cairo_set_line_join( cr, CAIRO_LINE_JOIN_MITER ); cairo_set_line_width( cr, line_width ); cairo_set_source_rgb( cr, ( ( double )color->red ) / 65535.0, ( ( double )color->green ) / 65535.0, - ( ( double )color->blue ) / 65535.0 ); + ( ( double )color->blue ) / 65535.0 ); x49gp_ui_symbol_path( cr, size, xoffset, yoffset, symbol ); if ( fill ) - cairo_fill( cr ); + cairo_fill( cr ); else - cairo_stroke( cr ); + cairo_stroke( cr ); } static int x49gp_ui_lookup_glyph( const char* name, int namelen, gunichar* glyph ) { for ( int i = 0; i < NR_GLYPHNAMES; i++ ) { - if ( ( strlen( x49gp_glyphs[ i ].name ) == namelen ) && !strncmp( x49gp_glyphs[ i ].name, name, namelen ) ) { - if ( glyph ) - *glyph = x49gp_glyphs[ i ].unichar; + if ( ( strlen( x49gp_glyphs[ i ].name ) == namelen ) && !strncmp( x49gp_glyphs[ i ].name, name, namelen ) ) { + if ( glyph ) + *glyph = x49gp_glyphs[ i ].unichar; - return 1; - } + return 1; + } } return 0; @@ -1705,43 +1705,43 @@ static int x49gp_text_strlen( const char* text ) p = text; while ( ( c = *p++ ) ) { - if ( c != '\\' ) { - n++; - continue; - } + if ( c != '\\' ) { + n++; + continue; + } - q = p; - while ( *q ) { - if ( ( *q == '\\' ) || ( *q == ' ' ) ) - break; - q++; - } - if ( q == p ) { - n++; - p++; - continue; - } - namelen = q - p; - if ( *q == ' ' ) - q++; + q = p; + while ( *q ) { + if ( ( *q == '\\' ) || ( *q == ' ' ) ) + break; + q++; + } + if ( q == p ) { + n++; + p++; + continue; + } + namelen = q - p; + if ( *q == ' ' ) + q++; - if ( symbol_lookup_glyph_by_name( p, namelen, NULL ) ) { - p = q; - n++; - continue; - } + if ( symbol_lookup_glyph_by_name( p, namelen, NULL ) ) { + p = q; + n++; + continue; + } - if ( x49gp_ui_lookup_glyph( p, namelen, NULL ) ) { - p = q; - n++; - continue; - } + if ( x49gp_ui_lookup_glyph( p, namelen, NULL ) ) { + p = q; + n++; + continue; + } - /* - * Insert symbol .notdef here... - */ - p = q; - n++; + /* + * Insert symbol .notdef here... + */ + p = q; + n++; } return n; @@ -1758,7 +1758,7 @@ static int x49gp_text_to_ucs4( const char* text, gunichar** ucs4p ) int n = x49gp_text_strlen( text ); if ( n <= 0 ) - return n; + return n; ucs4 = malloc( n * sizeof( gunichar ) ); @@ -1766,47 +1766,47 @@ static int x49gp_text_to_ucs4( const char* text, gunichar** ucs4p ) p = text; while ( ( c = *p++ ) ) { - if ( i == n ) { - free( ucs4 ); - return -1; - } + if ( i == n ) { + free( ucs4 ); + return -1; + } - if ( c != '\\' ) { - ucs4[ i++ ] = c; - continue; - } + if ( c != '\\' ) { + ucs4[ i++ ] = c; + continue; + } - q = p; - while ( *q ) { - if ( ( *q == '\\' ) || ( *q == ' ' ) ) - break; - q++; - } - if ( q == p ) { - ucs4[ i++ ] = *p++; - continue; - } - namelen = q - p; - if ( *q == ' ' ) - q++; + q = p; + while ( *q ) { + if ( ( *q == '\\' ) || ( *q == ' ' ) ) + break; + q++; + } + if ( q == p ) { + ucs4[ i++ ] = *p++; + continue; + } + namelen = q - p; + if ( *q == ' ' ) + q++; - if ( symbol_lookup_glyph_by_name( p, namelen, &glyph ) ) { - ucs4[ i++ ] = glyph; - p = q; - continue; - } + if ( symbol_lookup_glyph_by_name( p, namelen, &glyph ) ) { + ucs4[ i++ ] = glyph; + p = q; + continue; + } - if ( x49gp_ui_lookup_glyph( p, namelen, &glyph ) ) { - ucs4[ i++ ] = glyph; - p = q; - continue; - } + if ( x49gp_ui_lookup_glyph( p, namelen, &glyph ) ) { + ucs4[ i++ ] = glyph; + p = q; + continue; + } - /* - * Insert symbol .notdef here... - */ - ucs4[ i++ ] = 0xe000; - p = q; + /* + * Insert symbol .notdef here... + */ + ucs4[ i++ ] = 0xe000; + p = q; } *ucs4p = ucs4; @@ -1827,61 +1827,61 @@ static void x49gp_ui_vtext_path( cairo_t* cr, const char* family, double size, d int len; for ( int i = 0; i < n; i++ ) { - slant = va_arg( ap, cairo_font_slant_t ); - weight = va_arg( ap, cairo_font_weight_t ); - text = va_arg( ap, const char* ); + slant = va_arg( ap, cairo_font_slant_t ); + weight = va_arg( ap, cairo_font_weight_t ); + text = va_arg( ap, const char* ); - cairo_select_font_face( cr, family, slant, weight ); - cairo_set_font_size( cr, size ); + cairo_select_font_face( cr, family, slant, weight ); + cairo_set_font_size( cr, size ); - ucs4 = NULL; - len = x49gp_text_to_ucs4( text, &ucs4 ); - if ( len <= 0 ) - continue; + ucs4 = NULL; + len = x49gp_text_to_ucs4( text, &ucs4 ); + if ( len <= 0 ) + continue; - for ( int j = 0; j < len; j++ ) { - if ( g_unichar_type( ucs4[ j ] ) == G_UNICODE_PRIVATE_USE ) { - /* - * Draw Symbol, Increment x... - */ - symbol = symbol_get_by_glyph( ucs4[ j ] ); - if ( NULL == symbol ) - symbol = symbol_get_by_glyph( 0xe000 ); + for ( int j = 0; j < len; j++ ) { + if ( g_unichar_type( ucs4[ j ] ) == G_UNICODE_PRIVATE_USE ) { + /* + * Draw Symbol, Increment x... + */ + symbol = symbol_get_by_glyph( ucs4[ j ] ); + if ( NULL == symbol ) + symbol = symbol_get_by_glyph( 0xe000 ); - size *= symbol->prescale; + size *= symbol->prescale; - x49gp_ui_symbol_path( cr, size, x, y, symbol ); + x49gp_ui_symbol_path( cr, size, x, y, symbol ); - x += size * symbol->x_advance; - y -= size * symbol->y_advance; + x += size * symbol->x_advance; + y -= size * symbol->y_advance; - size *= symbol->postscale; + size *= symbol->postscale; - if ( symbol->prescale * symbol->postscale != 1. ) - cairo_set_font_size( cr, size ); + if ( symbol->prescale * symbol->postscale != 1. ) + cairo_set_font_size( cr, size ); - continue; - } + continue; + } - bytes = g_unichar_to_utf8( ucs4[ j ], out ); - out[ bytes ] = '\0'; + bytes = g_unichar_to_utf8( ucs4[ j ], out ); + out[ bytes ] = '\0'; - cairo_text_extents( cr, out, &extents ); + cairo_text_extents( cr, out, &extents ); - cairo_move_to( cr, x, y ); + cairo_move_to( cr, x, y ); - cairo_text_path( cr, out ); + cairo_text_path( cr, out ); - x += extents.x_advance; - y += extents.y_advance; - } + x += extents.x_advance; + y += extents.y_advance; + } - free( ucs4 ); + free( ucs4 ); } } static void x49gp_ui_text_size( cairo_t* cr, const char* family, double size, double* x_bearing, double* y_bearing, double* width, - double* height, double* ascent, double* descent, int n, ... ) + double* height, double* ascent, double* descent, int n, ... ) { va_list ap0, ap1; cairo_font_extents_t font_extents; @@ -1891,7 +1891,7 @@ static void x49gp_ui_text_size( cairo_t* cr, const char* family, double size, do const char* text; if ( n < 1 ) - return; + return; va_start( ap0, n ); va_copy( ap1, ap0 ); @@ -1903,30 +1903,30 @@ static void x49gp_ui_text_size( cairo_t* cr, const char* family, double size, do cairo_fill_extents( cr, &x1, &y1, &x2, &y2 ); if ( y2 < 0.0 ) - y2 = 0.0; + y2 = 0.0; a = 0.0; d = 0.0; for ( int i = 0; i < n; i++ ) { - slant = va_arg( ap1, cairo_font_slant_t ); - weight = va_arg( ap1, cairo_font_weight_t ); - text = va_arg( ap1, const char* ); - ( void )text; + slant = va_arg( ap1, cairo_font_slant_t ); + weight = va_arg( ap1, cairo_font_weight_t ); + text = va_arg( ap1, const char* ); + ( void )text; - cairo_select_font_face( cr, family, slant, weight ); - cairo_set_font_size( cr, size ); + cairo_select_font_face( cr, family, slant, weight ); + cairo_set_font_size( cr, size ); - cairo_font_extents( cr, &font_extents ); + cairo_font_extents( cr, &font_extents ); - /* - * Cairo seems to return overall height in ascent, - * so fix this by calculating ascent = height - descent. - */ - if ( font_extents.ascent - font_extents.descent > a ) - a = font_extents.ascent - font_extents.descent; - if ( font_extents.descent > -d ) - d = -font_extents.descent; + /* + * Cairo seems to return overall height in ascent, + * so fix this by calculating ascent = height - descent. + */ + if ( font_extents.ascent - font_extents.descent > a ) + a = font_extents.ascent - font_extents.descent; + if ( font_extents.descent > -d ) + d = -font_extents.descent; } *x_bearing = x1; @@ -1940,25 +1940,25 @@ static void x49gp_ui_text_size( cairo_t* cr, const char* family, double size, do } static void x49gp_ui_draw_text( cairo_t* cr, GdkColor* color, const char* family, double size, double line_width, int xoffset, int yoffset, - int n, ... ) + int n, ... ) { va_list ap; if ( n < 1 ) - return; + return; va_start( ap, n ); cairo_set_line_width( cr, line_width ); cairo_set_source_rgb( cr, ( ( double )color->red ) / 65535.0, ( ( double )color->green ) / 65535.0, - ( ( double )color->blue ) / 65535.0 ); + ( ( double )color->blue ) / 65535.0 ); x49gp_ui_vtext_path( cr, family, size, xoffset, yoffset, n, ap ); if ( line_width == 0.0 ) - cairo_fill( cr ); + cairo_fill( cr ); else - cairo_stroke( cr ); + cairo_stroke( cr ); va_end( ap ); } @@ -2017,8 +2017,8 @@ x49gp_ui_dump_path(cairo_t *cr, const char *family, int n, ...) static unsigned char bitmap_font_lookup_glyph( const bitmap_font_t* font, const char* name, int namelen ) { for ( int i = 0; font->glyphs[ i ].name; i++ ) - if ( ( strlen( font->glyphs[ i ].name ) == namelen ) && !strncmp( font->glyphs[ i ].name, name, namelen ) ) - return i; + if ( ( strlen( font->glyphs[ i ].name ) == namelen ) && !strncmp( font->glyphs[ i ].name, name, namelen ) ) + return i; return 0; } @@ -2029,140 +2029,140 @@ static unsigned char bitmap_font_lookup_ascii( const bitmap_font_t* font, char c char* name; switch ( c ) { - case ' ': - name = "space"; - break; - case '!': - name = "exclam"; - break; - case '"': - name = "quotedbl"; - break; - case '#': - name = "numbersign"; - break; - case '$': - name = "dollar"; - break; - case '%': - name = "percent"; - break; - case '&': - name = "ampersand"; - break; - case '(': - name = "parenleft"; - break; - case ')': - name = "parenright"; - break; - case '*': - name = "asterisk"; - break; - case '+': - name = "plus"; - break; - case ',': - name = "comma"; - break; - case '-': - name = "hyphen"; - break; - case '.': - name = "period"; - break; - case '/': - name = "slash"; - break; - case '0': - name = "zero"; - break; - case '1': - name = "one"; - break; - case '2': - name = "two"; - break; - case '3': - name = "three"; - break; - case '4': - name = "four"; - break; - case '5': - name = "five"; - break; - case '6': - name = "six"; - break; - case '7': - name = "seven"; - break; - case '8': - name = "eight"; - break; - case '9': - name = "nine"; - break; - case ':': - name = "colon"; - break; - case ';': - name = "semicolon"; - break; - case '<': - name = "less"; - break; - case '=': - name = "equal"; - break; - case '>': - name = "greater"; - break; - case '?': - name = "question"; - break; - case '@': - name = "at"; - break; - case '[': - name = "bracketleft"; - break; - case '\\': - name = "backslash"; - break; - case ']': - name = "bracketright"; - break; - case '^': - name = "asciicircum"; - break; - case '_': - name = "underscore"; - break; - case '`': - name = "quoteleft"; - break; - case '{': - name = "braceleft"; - break; - case '|': - name = "bar"; - break; - case '}': - name = "braceright"; - break; - case '~': - name = "asciitilde"; - break; - default: - name = &c; - namelen = 1; - break; + case ' ': + name = "space"; + break; + case '!': + name = "exclam"; + break; + case '"': + name = "quotedbl"; + break; + case '#': + name = "numbersign"; + break; + case '$': + name = "dollar"; + break; + case '%': + name = "percent"; + break; + case '&': + name = "ampersand"; + break; + case '(': + name = "parenleft"; + break; + case ')': + name = "parenright"; + break; + case '*': + name = "asterisk"; + break; + case '+': + name = "plus"; + break; + case ',': + name = "comma"; + break; + case '-': + name = "hyphen"; + break; + case '.': + name = "period"; + break; + case '/': + name = "slash"; + break; + case '0': + name = "zero"; + break; + case '1': + name = "one"; + break; + case '2': + name = "two"; + break; + case '3': + name = "three"; + break; + case '4': + name = "four"; + break; + case '5': + name = "five"; + break; + case '6': + name = "six"; + break; + case '7': + name = "seven"; + break; + case '8': + name = "eight"; + break; + case '9': + name = "nine"; + break; + case ':': + name = "colon"; + break; + case ';': + name = "semicolon"; + break; + case '<': + name = "less"; + break; + case '=': + name = "equal"; + break; + case '>': + name = "greater"; + break; + case '?': + name = "question"; + break; + case '@': + name = "at"; + break; + case '[': + name = "bracketleft"; + break; + case '\\': + name = "backslash"; + break; + case ']': + name = "bracketright"; + break; + case '^': + name = "asciicircum"; + break; + case '_': + name = "underscore"; + break; + case '`': + name = "quoteleft"; + break; + case '{': + name = "braceleft"; + break; + case '|': + name = "bar"; + break; + case '}': + name = "braceright"; + break; + case '~': + name = "asciitilde"; + break; + default: + name = &c; + namelen = 1; + break; } if ( 0 == namelen ) - namelen = strlen( name ); + namelen = strlen( name ); return bitmap_font_lookup_glyph( font, name, namelen ); } @@ -2175,27 +2175,27 @@ static int bitmap_font_strlen( const char* text ) p = text; while ( ( c = *p++ ) ) { - if ( c != '\\' ) { - n++; - continue; - } + if ( c != '\\' ) { + n++; + continue; + } - q = p; - while ( *q ) { - if ( ( *q == '\\' ) || ( *q == ' ' ) ) - break; - q++; - } - if ( q == p ) { - n++; - p++; - continue; - } - if ( *q == ' ' ) - q++; + q = p; + while ( *q ) { + if ( ( *q == '\\' ) || ( *q == ' ' ) ) + break; + q++; + } + if ( q == p ) { + n++; + p++; + continue; + } + if ( *q == ' ' ) + q++; - n++; - p = q; + n++; + p = q; } return n; @@ -2211,7 +2211,7 @@ static int bitmap_font_text_to_glyphs( const bitmap_font_t* font, const char* te n = bitmap_font_strlen( text ); if ( n <= 0 ) - return n; + return n; glyphs = malloc( n ); @@ -2219,32 +2219,32 @@ static int bitmap_font_text_to_glyphs( const bitmap_font_t* font, const char* te p = text; while ( ( c = *p++ ) ) { - if ( i == n ) { - free( glyphs ); - return -1; - } + if ( i == n ) { + free( glyphs ); + return -1; + } - if ( c != '\\' ) { - glyphs[ i++ ] = bitmap_font_lookup_ascii( font, c ); - continue; - } + if ( c != '\\' ) { + glyphs[ i++ ] = bitmap_font_lookup_ascii( font, c ); + continue; + } - q = p; - while ( *q ) { - if ( ( *q == '\\' ) || ( *q == ' ' ) ) - break; - q++; - } - if ( q == p ) { - glyphs[ i++ ] = bitmap_font_lookup_ascii( font, *p++ ); - continue; - } - namelen = q - p; - if ( *q == ' ' ) - q++; + q = p; + while ( *q ) { + if ( ( *q == '\\' ) || ( *q == ' ' ) ) + break; + q++; + } + if ( q == p ) { + glyphs[ i++ ] = bitmap_font_lookup_ascii( font, *p++ ); + continue; + } + namelen = q - p; + if ( *q == ' ' ) + q++; - glyphs[ i++ ] = bitmap_font_lookup_glyph( font, p, namelen ); - p = q; + glyphs[ i++ ] = bitmap_font_lookup_glyph( font, p, namelen ); + p = q; } *glyphp = glyphs; @@ -2264,14 +2264,14 @@ static void bitmap_font_text_size( const bitmap_font_t* font, const char* text, n = bitmap_font_text_to_glyphs( font, text, &glyphs ); for ( int i = 0; i < n; i++ ) { - glyph = &font->glyphs[ glyphs[ i ] ]; + glyph = &font->glyphs[ glyphs[ i ] ]; - w += glyph->width; + w += glyph->width; - if ( glyph->ascent > a ) - a = glyph->ascent; - if ( glyph->descent < d ) - d = glyph->descent; + if ( glyph->ascent > a ) + a = glyph->ascent; + if ( glyph->descent < d ) + d = glyph->descent; } *width = w - 1; @@ -2280,7 +2280,7 @@ static void bitmap_font_text_size( const bitmap_font_t* font, const char* text, *descent = d; if ( n > 0 ) - free( glyphs ); + free( glyphs ); } static void bitmap_font_draw_text( GdkDrawable* drawable, GdkColor* color, const bitmap_font_t* font, int x, int y, const char* text ) @@ -2297,33 +2297,33 @@ static void bitmap_font_draw_text( GdkDrawable* drawable, GdkColor* color, const n = bitmap_font_text_to_glyphs( font, text, &glyphs ); for ( int i = 0; i < n; i++ ) { - glyph = &font->glyphs[ glyphs[ i ] ]; + glyph = &font->glyphs[ glyphs[ i ] ]; - w = glyph->width - glyph->kern; - h = glyph->ascent - glyph->descent; + w = glyph->width - glyph->kern; + h = glyph->ascent - glyph->descent; - if ( w <= 0 || h <= 0 ) { - x += glyph->width; - continue; - } + if ( w <= 0 || h <= 0 ) { + x += glyph->width; + continue; + } - bitmap = gdk_bitmap_create_from_data( NULL, ( char* )glyph->bits, w, h ); + bitmap = gdk_bitmap_create_from_data( NULL, ( char* )glyph->bits, w, h ); - gdk_gc_set_ts_origin( gc, x + glyph->kern, y + font->ascent - glyph->ascent ); - gdk_gc_set_stipple( gc, bitmap ); - gdk_gc_set_fill( gc, GDK_STIPPLED ); + gdk_gc_set_ts_origin( gc, x + glyph->kern, y + font->ascent - glyph->ascent ); + gdk_gc_set_stipple( gc, bitmap ); + gdk_gc_set_fill( gc, GDK_STIPPLED ); - gdk_draw_rectangle( drawable, gc, TRUE, x + glyph->kern, y + font->ascent - glyph->ascent, w, h ); + gdk_draw_rectangle( drawable, gc, TRUE, x + glyph->kern, y + font->ascent - glyph->ascent, w, h ); - g_object_unref( bitmap ); + g_object_unref( bitmap ); - x += glyph->width; + x += glyph->width; } g_object_unref( gc ); if ( n > 0 ) - free( glyphs ); + free( glyphs ); } void x49gp_ui_show_error( x49gp_t* x49gp, const char* text ) @@ -2332,7 +2332,7 @@ void x49gp_ui_show_error( x49gp_t* x49gp, const char* text ) x49gp_ui_t* ui = x49gp->ui; dialog = - gtk_message_dialog_new( GTK_WINDOW( ui->window ), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", text ); + gtk_message_dialog_new( GTK_WINDOW( ui->window ), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", text ); gtk_dialog_run( GTK_DIALOG( dialog ) ); gtk_widget_destroy( dialog ); @@ -2344,15 +2344,15 @@ static void x49gp_ui_choose_file( x49gp_t* x49gp, const char* prompt, GtkFileCho x49gp_ui_t* ui = x49gp->ui; dialog = gtk_file_chooser_dialog_new( prompt, GTK_WINDOW( ui->window ), action, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, - GTK_RESPONSE_ACCEPT, NULL ); + GTK_RESPONSE_ACCEPT, NULL ); gtk_file_chooser_set_local_only( GTK_FILE_CHOOSER( dialog ), TRUE ); gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER( dialog ), FALSE ); if ( gtk_dialog_run( GTK_DIALOG( dialog ) ) == GTK_RESPONSE_ACCEPT ) - *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) ); + *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) ); else - *filename = NULL; + *filename = NULL; gtk_widget_destroy( dialog ); } @@ -2374,35 +2374,35 @@ static gboolean x49gp_ui_button_press( GtkWidget* widget, GdkEventButton* event, #endif if ( event->type != GDK_BUTTON_PRESS ) - return FALSE; + return FALSE; switch ( event->button ) { - case 1: - ui->buttons_down++; - if ( button->down ) - return FALSE; - button->down = TRUE; - break; - case 3: - button->hold = TRUE; - if ( button->down ) - return FALSE; - gtk_button_pressed( GTK_BUTTON( button->button ) ); - button->down = TRUE; - break; - default: - return TRUE; + case 1: + ui->buttons_down++; + if ( button->down ) + return FALSE; + button->down = TRUE; + break; + case 3: + button->hold = TRUE; + if ( button->down ) + return FALSE; + gtk_button_pressed( GTK_BUTTON( button->button ) ); + button->down = TRUE; + break; + default: + return TRUE; } #ifdef DEBUG_X49GP_UI printf( "%s: button %u: col %u, row %u, eint %u\n", __FUNCTION__, event->button, button->key->column, button->key->row, - button->key->eint ); + button->key->eint ); #endif if ( key->rowbit ) - s3c2410_io_port_g_update( x49gp, key->column, key->row, key->columnbit, key->rowbit, 1 ); + s3c2410_io_port_g_update( x49gp, key->column, key->row, key->columnbit, key->rowbit, 1 ); else - s3c2410_io_port_f_set_bit( x49gp, key->eint, 1 ); + s3c2410_io_port_f_set_bit( x49gp, key->eint, 1 ); return FALSE; } @@ -2415,7 +2415,7 @@ static void x49gp_release_single_button( x49gp_ui_button_t* button, x49gp_ui_but #ifdef DEBUG_X49GP_UI printf( "%s: button %u: col %u, row %u, eint %u\n", __FUNCTION__, event->button, button->key->column, button->key->row, - button->key->eint ); + button->key->eint ); #endif button->down = FALSE; @@ -2424,15 +2424,15 @@ static void x49gp_release_single_button( x49gp_ui_button_t* button, x49gp_ui_but gtkbutton = GTK_BUTTON( button->button ); if ( button != cause ) - gtkbutton->in_button = FALSE; + gtkbutton->in_button = FALSE; gtk_button_released( gtkbutton ); key = button->key; if ( key->rowbit ) - s3c2410_io_port_g_update( x49gp, key->column, key->row, key->columnbit, key->rowbit, 0 ); + s3c2410_io_port_g_update( x49gp, key->column, key->row, key->columnbit, key->rowbit, 0 ); else - s3c2410_io_port_f_set_bit( x49gp, key->eint, 0 ); + s3c2410_io_port_f_set_bit( x49gp, key->eint, 0 ); } static void x49gp_release_all_buttons( x49gp_t* x49gp, x49gp_ui_button_t* cause ) @@ -2441,12 +2441,12 @@ static void x49gp_release_all_buttons( x49gp_t* x49gp, x49gp_ui_button_t* cause x49gp_ui_t* ui = x49gp->ui; for ( int i = 0; i < ui->nr_buttons; i++ ) { - button = &ui->buttons[ i ]; + button = &ui->buttons[ i ]; - if ( !button->down ) - continue; + if ( !button->down ) + continue; - x49gp_release_single_button( button, cause ); + x49gp_release_single_button( button, cause ); } } @@ -2457,22 +2457,22 @@ static gboolean x49gp_ui_button_release( GtkWidget* widget, GdkEventButton* even x49gp_ui_t* ui = x49gp->ui; if ( event->type != GDK_BUTTON_RELEASE ) - return FALSE; + return FALSE; switch ( event->button ) { - case 1: - break; - default: - return TRUE; + case 1: + break; + default: + return TRUE; } if ( ui->buttons_down > 0 ) - ui->buttons_down--; + ui->buttons_down--; if ( ui->buttons_down == 0 ) - x49gp_release_all_buttons( x49gp, button ); + x49gp_release_all_buttons( x49gp, button ); else - x49gp_release_single_button( button, button ); + x49gp_release_single_button( button, button ); return FALSE; } @@ -2484,11 +2484,11 @@ static gboolean x49gp_ui_show_menu( GtkWidget* widget, GdkEventButton* event, gp gtk_widget_set_sensitive( ui->menu_unmount, s3c2410_sdi_is_mounted( x49gp ) ); if ( ui->menu_debug ) - gtk_widget_set_sensitive( ui->menu_debug, !gdbserver_isactive() ); + gtk_widget_set_sensitive( ui->menu_debug, !gdbserver_isactive() ); if ( event->type == GDK_BUTTON_PRESS && event->button == 3 ) { - gtk_menu_popup( GTK_MENU( ui->menu ), NULL, NULL, NULL, NULL, event->button, event->time ); - return TRUE; + gtk_menu_popup( GTK_MENU( ui->menu ), NULL, NULL, NULL, NULL, event->button, event->time ); + return TRUE; } return FALSE; @@ -2499,10 +2499,10 @@ static gboolean x49gp_ui_button_leave( GtkWidget* widget, GdkEventCrossing* even x49gp_ui_button_t* button = user_data; if ( event->type != GDK_LEAVE_NOTIFY ) - return FALSE; + return FALSE; if ( !button->hold ) - return FALSE; + return FALSE; return TRUE; } @@ -2513,7 +2513,7 @@ static gboolean x49gp_ui_focus_lost( GtkWidget* widget, GdkEventFocus* event, gp x49gp_ui_t* ui = x49gp->ui; if ( event->type != GDK_FOCUS_CHANGE ) - return FALSE; + return FALSE; ui->buttons_down = 0; x49gp_release_all_buttons( x49gp, NULL ); @@ -2537,7 +2537,7 @@ static void x49gp_ui_mount_sd_folder( GtkMenuItem* menuitem, gpointer user_data x49gp_ui_choose_file( x49gp, "Choose SD folder ...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, &filename ); if ( filename != NULL ) - s3c2410_sdi_mount( x49gp, filename ); + s3c2410_sdi_mount( x49gp, filename ); } static void x49gp_ui_mount_sd_image( GtkMenuItem* menuitem, gpointer user_data ) @@ -2547,7 +2547,7 @@ static void x49gp_ui_mount_sd_image( GtkMenuItem* menuitem, gpointer user_data ) x49gp_ui_choose_file( x49gp, "Choose SD image ...", GTK_FILE_CHOOSER_ACTION_OPEN, &filename ); if ( filename != NULL ) - s3c2410_sdi_mount( x49gp, filename ); + s3c2410_sdi_mount( x49gp, filename ); } static void x49gp_ui_debug( GtkMenuItem* menuitem, gpointer user_data ) @@ -2555,8 +2555,8 @@ static void x49gp_ui_debug( GtkMenuItem* menuitem, gpointer user_data ) x49gp_t* x49gp = user_data; if ( x49gp->debug_port != 0 && !gdbserver_isactive() ) { - gdbserver_start( x49gp->debug_port ); - gdb_handlesig( x49gp->env, 0 ); + gdbserver_start( x49gp->debug_port ); + gdb_handlesig( x49gp->env, 0 ); } } @@ -2586,259 +2586,259 @@ static gboolean x49gp_ui_key_event( GtkWidget* widget, GdkEventKey* event, gpoin /* However, there is one modifier we do care about: NumLock, */ /* which normally is represented by MOD2. */ if ( !gdk_keymap_translate_keyboard_state( gdk_keymap_get_default(), event->hardware_keycode, event->state & GDK_MOD2_MASK, - event->group, &keyval, NULL, NULL, NULL ) ) - return FALSE; + event->group, &keyval, NULL, NULL, NULL ) ) + return FALSE; #ifdef DEBUG_X49GP_UI fprintf( stderr, "%s:%u: state %u, base keyval %04x\n", __FUNCTION__, __LINE__, event->state, keyval ); #endif switch ( keyval ) { - case GDK_KEY_a: - case GDK_KEY_F1: - index = 0; - break; - case GDK_KEY_b: - case GDK_KEY_F2: - index = 1; - break; - case GDK_KEY_c: - case GDK_KEY_F3: - index = 2; - break; - case GDK_KEY_d: - case GDK_KEY_F4: - index = 3; - break; - case GDK_KEY_e: - case GDK_KEY_F5: - index = 4; - break; - case GDK_KEY_f: - case GDK_KEY_F6: - index = 5; - break; - case GDK_KEY_g: - index = 6; - break; - case GDK_KEY_h: - index = 7; - break; - case GDK_KEY_i: - index = 8; - break; - case GDK_KEY_j: - index = 9; - break; - case GDK_KEY_k: - index = 10; - break; - case GDK_KEY_l: - index = 11; - break; - case GDK_KEY_Up: - case GDK_KEY_KP_Up: - index = 12; - break; - case GDK_KEY_Left: - case GDK_KEY_KP_Left: - index = 13; - break; - case GDK_KEY_Down: - case GDK_KEY_KP_Down: - index = 14; - break; - case GDK_KEY_Right: - case GDK_KEY_KP_Right: - index = 15; - break; - case GDK_KEY_m: - index = 16; - break; - case GDK_KEY_n: - index = 17; - break; - case GDK_KEY_o: - case GDK_KEY_apostrophe: - index = 18; - break; - case GDK_KEY_p: - index = 19; - break; - case GDK_KEY_BackSpace: - case GDK_KEY_Delete: - case GDK_KEY_KP_Delete: - index = 20; - break; - case GDK_KEY_dead_circumflex: - case GDK_KEY_asciicircum: - case GDK_KEY_q: - case GDK_KEY_caret: - index = 21; - break; - case GDK_KEY_r: - index = 22; - break; - case GDK_KEY_s: - index = 23; - break; - case GDK_KEY_t: - index = 24; - break; - case GDK_KEY_u: - index = 25; - break; - case GDK_KEY_v: - index = 26; - break; - case GDK_KEY_w: - index = 27; - break; - case GDK_KEY_x: - index = 28; - break; - case GDK_KEY_y: - index = 29; - break; - case GDK_KEY_z: - case GDK_KEY_slash: - case GDK_KEY_KP_Divide: - index = 30; - break; - case GDK_KEY_Tab: - index = 31; - break; + case GDK_KEY_a: + case GDK_KEY_F1: + index = 0; + break; + case GDK_KEY_b: + case GDK_KEY_F2: + index = 1; + break; + case GDK_KEY_c: + case GDK_KEY_F3: + index = 2; + break; + case GDK_KEY_d: + case GDK_KEY_F4: + index = 3; + break; + case GDK_KEY_e: + case GDK_KEY_F5: + index = 4; + break; + case GDK_KEY_f: + case GDK_KEY_F6: + index = 5; + break; + case GDK_KEY_g: + index = 6; + break; + case GDK_KEY_h: + index = 7; + break; + case GDK_KEY_i: + index = 8; + break; + case GDK_KEY_j: + index = 9; + break; + case GDK_KEY_k: + index = 10; + break; + case GDK_KEY_l: + index = 11; + break; + case GDK_KEY_Up: + case GDK_KEY_KP_Up: + index = 12; + break; + case GDK_KEY_Left: + case GDK_KEY_KP_Left: + index = 13; + break; + case GDK_KEY_Down: + case GDK_KEY_KP_Down: + index = 14; + break; + case GDK_KEY_Right: + case GDK_KEY_KP_Right: + index = 15; + break; + case GDK_KEY_m: + index = 16; + break; + case GDK_KEY_n: + index = 17; + break; + case GDK_KEY_o: + case GDK_KEY_apostrophe: + index = 18; + break; + case GDK_KEY_p: + index = 19; + break; + case GDK_KEY_BackSpace: + case GDK_KEY_Delete: + case GDK_KEY_KP_Delete: + index = 20; + break; + case GDK_KEY_dead_circumflex: + case GDK_KEY_asciicircum: + case GDK_KEY_q: + case GDK_KEY_caret: + index = 21; + break; + case GDK_KEY_r: + index = 22; + break; + case GDK_KEY_s: + index = 23; + break; + case GDK_KEY_t: + index = 24; + break; + case GDK_KEY_u: + index = 25; + break; + case GDK_KEY_v: + index = 26; + break; + case GDK_KEY_w: + index = 27; + break; + case GDK_KEY_x: + index = 28; + break; + case GDK_KEY_y: + index = 29; + break; + case GDK_KEY_z: + case GDK_KEY_slash: + case GDK_KEY_KP_Divide: + index = 30; + break; + case GDK_KEY_Tab: + index = 31; + break; #ifndef __APPLE__ - // case GDK_KEY_Alt_L: case GDK_KEY_Alt_R: - // case GDK_KEY_Meta_L: case GDK_KEY_Meta_R: - case GDK_KEY_Mode_switch: - index = 31; - break; + // case GDK_KEY_Alt_L: case GDK_KEY_Alt_R: + // case GDK_KEY_Meta_L: case GDK_KEY_Meta_R: + case GDK_KEY_Mode_switch: + index = 31; + break; #endif - case GDK_KEY_7: - case GDK_KEY_KP_7: - index = 32; - break; - case GDK_KEY_8: - case GDK_KEY_KP_8: - index = 33; - break; - case GDK_KEY_9: - case GDK_KEY_KP_9: - index = 34; - break; - case GDK_KEY_multiply: - case GDK_KEY_KP_Multiply: - index = 35; - break; - case GDK_KEY_Shift_L: - case GDK_KEY_Shift_R: - index = 36; - break; - case GDK_KEY_4: - case GDK_KEY_KP_4: - index = 37; - break; - case GDK_KEY_5: - case GDK_KEY_KP_5: - index = 38; - break; - case GDK_KEY_6: - case GDK_KEY_KP_6: - index = 39; - break; - case GDK_KEY_minus: - case GDK_KEY_KP_Subtract: - index = 40; - break; - case GDK_KEY_Control_L: - case GDK_KEY_Control_R: - index = 41; - break; - case GDK_KEY_1: - case GDK_KEY_KP_1: - index = 42; - break; - case GDK_KEY_2: - case GDK_KEY_KP_2: - index = 43; - break; - case GDK_KEY_3: - case GDK_KEY_KP_3: - index = 44; - break; - case GDK_KEY_plus: - case GDK_KEY_KP_Add: - index = 45; - break; - case GDK_KEY_Escape: - index = 46; - break; - case GDK_KEY_0: - case GDK_KEY_KP_0: - index = 47; - break; - case GDK_KEY_period: - case GDK_KEY_comma: - case GDK_KEY_KP_Decimal: - case GDK_KP_Separator: - index = 48; - break; - case GDK_KEY_space: - case GDK_KEY_KP_Space: - index = 49; - break; - case GDK_KEY_Return: - case GDK_KEY_KP_Enter: - index = 50; - break; + case GDK_KEY_7: + case GDK_KEY_KP_7: + index = 32; + break; + case GDK_KEY_8: + case GDK_KEY_KP_8: + index = 33; + break; + case GDK_KEY_9: + case GDK_KEY_KP_9: + index = 34; + break; + case GDK_KEY_multiply: + case GDK_KEY_KP_Multiply: + index = 35; + break; + case GDK_KEY_Shift_L: + case GDK_KEY_Shift_R: + index = 36; + break; + case GDK_KEY_4: + case GDK_KEY_KP_4: + index = 37; + break; + case GDK_KEY_5: + case GDK_KEY_KP_5: + index = 38; + break; + case GDK_KEY_6: + case GDK_KEY_KP_6: + index = 39; + break; + case GDK_KEY_minus: + case GDK_KEY_KP_Subtract: + index = 40; + break; + case GDK_KEY_Control_L: + case GDK_KEY_Control_R: + index = 41; + break; + case GDK_KEY_1: + case GDK_KEY_KP_1: + index = 42; + break; + case GDK_KEY_2: + case GDK_KEY_KP_2: + index = 43; + break; + case GDK_KEY_3: + case GDK_KEY_KP_3: + index = 44; + break; + case GDK_KEY_plus: + case GDK_KEY_KP_Add: + index = 45; + break; + case GDK_KEY_Escape: + index = 46; + break; + case GDK_KEY_0: + case GDK_KEY_KP_0: + index = 47; + break; + case GDK_KEY_period: + case GDK_KEY_comma: + case GDK_KEY_KP_Decimal: + case GDK_KP_Separator: + index = 48; + break; + case GDK_KEY_space: + case GDK_KEY_KP_Space: + index = 49; + break; + case GDK_KEY_Return: + case GDK_KEY_KP_Enter: + index = 50; + break; - /* QWERTY compat: US English, UK English, International English */ - case GDK_KEY_backslash: - index = 35; - break; - case GDK_KEY_equal: - index = 45; - break; + /* QWERTY compat: US English, UK English, International English */ + case GDK_KEY_backslash: + index = 35; + break; + case GDK_KEY_equal: + index = 45; + break; - /* QWERTZ compat: German */ - case GDK_KEY_ssharp: - index = 30; - break; - case GDK_KEY_numbersign: - index = 35; - break; + /* QWERTZ compat: German */ + case GDK_KEY_ssharp: + index = 30; + break; + case GDK_KEY_numbersign: + index = 35; + break; - case GDK_KEY_F7: - case GDK_KEY_F10: - x49gp->arm_exit = 1; - cpu_exit( x49gp->env ); - return FALSE; + case GDK_KEY_F7: + case GDK_KEY_F10: + x49gp->arm_exit = 1; + cpu_exit( x49gp->env ); + return FALSE; - case GDK_KEY_F12: - switch ( event->type ) { - case GDK_KEY_PRESS: - x49gp_modules_reset( x49gp, X49GP_RESET_POWER_ON ); - cpu_reset( x49gp->env ); - x49gp_set_idle( x49gp, 1 ); - break; - case GDK_KEY_RELEASE: - x49gp_set_idle( x49gp, 0 ); - break; - default: - break; - } - return FALSE; + case GDK_KEY_F12: + switch ( event->type ) { + case GDK_KEY_PRESS: + x49gp_modules_reset( x49gp, X49GP_RESET_POWER_ON ); + cpu_reset( x49gp->env ); + x49gp_set_idle( x49gp, 1 ); + break; + case GDK_KEY_RELEASE: + x49gp_set_idle( x49gp, 0 ); + break; + default: + break; + } + return FALSE; - case GDK_KEY_Menu: - gtk_widget_set_sensitive( ui->menu_unmount, s3c2410_sdi_is_mounted( x49gp ) ); - if ( ui->menu_debug ) - gtk_widget_set_sensitive( ui->menu_debug, !gdbserver_isactive() ); + case GDK_KEY_Menu: + gtk_widget_set_sensitive( ui->menu_unmount, s3c2410_sdi_is_mounted( x49gp ) ); + if ( ui->menu_debug ) + gtk_widget_set_sensitive( ui->menu_debug, !gdbserver_isactive() ); - gtk_menu_popup( GTK_MENU( ui->menu ), NULL, NULL, x49gp_ui_popup_at_widget, ui->lcd_canvas, 0, event->time ); - return FALSE; + gtk_menu_popup( GTK_MENU( ui->menu ), NULL, NULL, x49gp_ui_popup_at_widget, ui->lcd_canvas, 0, event->time ); + return FALSE; - default: - return FALSE; + default: + return FALSE; } button = &ui->buttons[ index ]; @@ -2852,22 +2852,22 @@ static gboolean x49gp_ui_key_event( GtkWidget* widget, GdkEventKey* event, gpoin save_in = GTK_BUTTON( button->button )->in_button; switch ( event->type ) { - case GDK_KEY_PRESS: - bev.type = GDK_BUTTON_PRESS; - x49gp_ui_button_press( button->button, &bev, button ); - GTK_BUTTON( button->button )->in_button = TRUE; - gtk_button_pressed( GTK_BUTTON( button->button ) ); - GTK_BUTTON( button->button )->in_button = save_in; - break; - case GDK_KEY_RELEASE: - bev.type = GDK_BUTTON_RELEASE; - GTK_BUTTON( button->button )->in_button = TRUE; - gtk_button_released( GTK_BUTTON( button->button ) ); - GTK_BUTTON( button->button )->in_button = save_in; - x49gp_ui_button_release( button->button, &bev, button ); - break; - default: - return FALSE; + case GDK_KEY_PRESS: + bev.type = GDK_BUTTON_PRESS; + x49gp_ui_button_press( button->button, &bev, button ); + GTK_BUTTON( button->button )->in_button = TRUE; + gtk_button_pressed( GTK_BUTTON( button->button ) ); + GTK_BUTTON( button->button )->in_button = save_in; + break; + case GDK_KEY_RELEASE: + bev.type = GDK_BUTTON_RELEASE; + GTK_BUTTON( button->button )->in_button = TRUE; + gtk_button_released( GTK_BUTTON( button->button ) ); + GTK_BUTTON( button->button )->in_button = save_in; + x49gp_ui_button_release( button->button, &bev, button ); + break; + default: + return FALSE; } return TRUE; @@ -2882,10 +2882,10 @@ static int x49gp_button_expose_event( GtkWidget* widget, GdkEventExpose* event, y = widget->allocation.y; if ( GTK_WIDGET_STATE( widget ) == GTK_STATE_ACTIVE ) - y -= 1; + y -= 1; gdk_draw_drawable( widget->window, widget->style->black_gc, button->pixmap, 0, 0, x, y, widget->allocation.width, - widget->allocation.height ); + widget->allocation.height ); return FALSE; } @@ -2908,7 +2908,7 @@ static void x49gp_button_realize( GtkWidget* widget, gpointer user_data ) button->pixmap = gdk_pixmap_new( widget->style->bg_pixmap[ 0 ], w, h, -1 ); gdk_draw_drawable( button->pixmap, widget->style->black_gc, widget->style->bg_pixmap[ 0 ], xoffset, yoffset, 0, 0, button->key->width, - button->key->height ); + button->key->height ); xoffset += 2; yoffset += 2; @@ -2931,39 +2931,39 @@ static void x49gp_button_realize( GtkWidget* widget, gpointer user_data ) #endif if ( key->letter ) { - x49gp_ui_text_size( cr, X49GP_UI_NORMAL_FONT, key->letter_size, &xoff, &yoff, &width, &height, &ascent, &descent, 1, - CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->letter ); + x49gp_ui_text_size( cr, X49GP_UI_NORMAL_FONT, key->letter_size, &xoff, &yoff, &width, &height, &ascent, &descent, 1, + CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->letter ); - switch ( key->layout ) { - case UI_LAYOUT_LEFT: - default: - x = ( int )floor( w - 1.0 - width - xoff + 0.5 ); - y = ( int )floor( ( h - 1.0 + ascent ) / 2.0 + 0.5 ); - w -= width; - break; - case UI_LAYOUT_LEFT_NO_SPACE: - x = ( int )floor( w - 1.0 - width - xoff + 0.5 ); - y = ( int )floor( ( h - 1.0 + ascent ) / 2.0 + 0.5 ); - break; - case UI_LAYOUT_BELOW: - x = ( int )floor( ( w - 1.0 - width ) / 2.0 - xoff + 0.5 ); - y = ( int )h - 1.0; - h -= ascent; - break; - } + switch ( key->layout ) { + case UI_LAYOUT_LEFT: + default: + x = ( int )floor( w - 1.0 - width - xoff + 0.5 ); + y = ( int )floor( ( h - 1.0 + ascent ) / 2.0 + 0.5 ); + w -= width; + break; + case UI_LAYOUT_LEFT_NO_SPACE: + x = ( int )floor( w - 1.0 - width - xoff + 0.5 ); + y = ( int )floor( ( h - 1.0 + ascent ) / 2.0 + 0.5 ); + break; + case UI_LAYOUT_BELOW: + x = ( int )floor( ( w - 1.0 - width ) / 2.0 - xoff + 0.5 ); + y = ( int )h - 1.0; + h -= ascent; + break; + } - x49gp_ui_draw_text( cr, &ui->colors[ UI_COLOR_YELLOW ], X49GP_UI_NORMAL_FONT, key->letter_size, 0.0, x + xoffset, y + yoffset, 1, - CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->letter ); + x49gp_ui_draw_text( cr, &ui->colors[ UI_COLOR_YELLOW ], X49GP_UI_NORMAL_FONT, key->letter_size, 0.0, x + xoffset, y + yoffset, 1, + CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->letter ); } x49gp_ui_text_size( cr, X49GP_UI_NORMAL_FONT, key->font_size, &xoff, &yoff, &width, &height, &ascent, &descent, 1, - CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->label ); + CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->label ); x = ( int )floor( ( w - 1.0 - width ) / 2.0 - xoff + 0.5 ); y = ( int )floor( ( h - 1.0 + ascent ) / 2.0 + 0.5 ); x49gp_ui_draw_text( cr, &widget->style->text[ 0 ], X49GP_UI_NORMAL_FONT, key->font_size, 0.0, x + xoffset, y + yoffset, 1, - CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->label ); + CAIRO_FONT_SLANT_NORMAL, key->font_weight, key->label ); cairo_destroy( cr ); } @@ -2977,8 +2977,8 @@ static int x49gp_lcd_expose_event( GtkWidget* widget, GdkEventExpose* event, gpo gdk_region_get_rectangles( event->region, &rects, &n ); for ( int i = 0; i < n; i++ ) { - gdk_draw_drawable( widget->window, widget->style->black_gc, ui->lcd_pixmap, rects[ i ].x, rects[ i ].y, rects[ i ].x, rects[ i ].y, - rects[ i ].width, rects[ i ].height ); + gdk_draw_drawable( widget->window, widget->style->black_gc, ui->lcd_pixmap, rects[ i ].x, rects[ i ].y, rects[ i ].x, rects[ i ].y, + rects[ i ].width, rects[ i ].height ); } g_free( rects ); @@ -2992,13 +2992,13 @@ static int x49gp_lcd_configure_event( GtkWidget* widget, GdkEventConfigure* even x49gp_ui_t* ui = x49gp->ui; if ( NULL != ui->lcd_pixmap ) - return FALSE; + return FALSE; ui->ann_left = gdk_bitmap_create_from_data( ui->lcd_canvas->window, ( char* )ann_left_bits, ann_left_width, ann_left_height ); ui->ann_right = gdk_bitmap_create_from_data( ui->lcd_canvas->window, ( char* )ann_right_bits, ann_right_width, ann_right_height ); ui->ann_alpha = gdk_bitmap_create_from_data( ui->lcd_canvas->window, ( char* )ann_alpha_bits, ann_alpha_width, ann_alpha_height ); ui->ann_battery = - gdk_bitmap_create_from_data( ui->lcd_canvas->window, ( char* )ann_battery_bits, ann_battery_width, ann_battery_height ); + gdk_bitmap_create_from_data( ui->lcd_canvas->window, ( char* )ann_battery_bits, ann_battery_width, ann_battery_height ); ui->ann_busy = gdk_bitmap_create_from_data( ui->lcd_canvas->window, ( char* )ann_busy_bits, ann_busy_width, ann_busy_height ); ui->ann_io = gdk_bitmap_create_from_data( ui->lcd_canvas->window, ( char* )ann_io_bits, ann_io_width, ann_io_height ); @@ -3065,7 +3065,7 @@ static int x49gp_lcd_configure_event( GtkWidget* widget, GdkEventConfigure* even #endif gdk_draw_drawable( ui->lcd_pixmap, widget->style->black_gc, ui->bg_pixmap, ui->lcd_x_offset, ui->lcd_y_offset, 0, 0, ui->lcd_width, - ui->lcd_height ); + ui->lcd_height ); return FALSE; } @@ -3085,103 +3085,103 @@ static int x49gp_window_configure_event( GtkWidget* widget, GdkEventConfigure* e int dl = 0, dr = 0; if ( NULL != ui->bg_pixmap ) - return FALSE; + return FALSE; ui->bg_pixmap = gdk_pixmap_new( widget->window, ui->width, ui->height, -1 ); gdk_draw_pixbuf( ui->bg_pixmap, widget->style->black_gc, ui->bg_pixbuf, 0, 0, 0, 0, ui->width, ui->height, GDK_RGB_DITHER_NORMAL, 0, - 0 ); + 0 ); cr = gdk_cairo_create( ui->bg_pixmap ); cairo_set_line_cap( cr, CAIRO_LINE_CAP_BUTT ); cairo_set_line_join( cr, CAIRO_LINE_JOIN_MITER ); switch ( ui->calculator ) { - case UI_CALCULATOR_HP49GP: - case UI_CALCULATOR_HP49GP_NEWRPL: - x49gp_ui_draw_text( cr, &widget->style->black, X49GP_UI_NORMAL_FONT, 15.0, 0.0, 14 /* 38 */, 20 /* 42 */, 2, - CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD, "hp", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, - " 49g+" ); + case UI_CALCULATOR_HP49GP: + case UI_CALCULATOR_HP49GP_NEWRPL: + x49gp_ui_draw_text( cr, &widget->style->black, X49GP_UI_NORMAL_FONT, 15.0, 0.0, 14 /* 38 */, 20 /* 42 */, 2, + CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD, "hp", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, + " 49g+" ); - x49gp_ui_draw_text( cr, &widget->style->black, X49GP_UI_NORMAL_FONT, 13.0, 0.0, 14 /* 38 */, 34 /* 56 */, 1, - CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, "graphing calculator" ); + x49gp_ui_draw_text( cr, &widget->style->black, X49GP_UI_NORMAL_FONT, 13.0, 0.0, 14 /* 38 */, 34 /* 56 */, 1, + CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, "graphing calculator" ); - x49gp_ui_draw_symbol( cr, &widget->style->black, 10.0, 0.0, TRUE, 114 /* 138 */, 8 /* 25 */, - symbol_get_by_name( "triangleup" ) ); + x49gp_ui_draw_symbol( cr, &widget->style->black, 10.0, 0.0, TRUE, 114 /* 138 */, 8 /* 25 */, + symbol_get_by_name( "triangleup" ) ); - left_color = UI_COLOR_GREEN; - right_color = UI_COLOR_RED; - below_color = UI_COLOR_BLACK; - break; + left_color = UI_COLOR_GREEN; + right_color = UI_COLOR_RED; + below_color = UI_COLOR_BLACK; + break; - default: - ui->calculator = UI_CALCULATOR_HP50G; - /* fall through */ + default: + ui->calculator = UI_CALCULATOR_HP50G; + /* fall through */ - case UI_CALCULATOR_HP50G: - case UI_CALCULATOR_HP50G_NEWRPL: - x49gp_ui_draw_text( cr, &widget->style->white, X49GP_UI_NORMAL_FONT, 15.0, 0.0, 14 /* 38 */, 20 /* 42 */, 2, - CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, "HP", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, - " 50g" ); + case UI_CALCULATOR_HP50G: + case UI_CALCULATOR_HP50G_NEWRPL: + x49gp_ui_draw_text( cr, &widget->style->white, X49GP_UI_NORMAL_FONT, 15.0, 0.0, 14 /* 38 */, 20 /* 42 */, 2, + CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, "HP", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, + " 50g" ); - x49gp_ui_draw_text( cr, &widget->style->white, X49GP_UI_NORMAL_FONT, 13.0, 0.0, 14 /* 38 */, 34 /* 56 */, 1, - CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, "Graphing Calculator" ); + x49gp_ui_draw_text( cr, &widget->style->white, X49GP_UI_NORMAL_FONT, 13.0, 0.0, 14 /* 38 */, 34 /* 56 */, 1, + CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL, "Graphing Calculator" ); - x49gp_ui_draw_symbol( cr, &widget->style->white, 10.0, 0.0, TRUE, 134 /* 168 */, 8 /* 25 */, - symbol_get_by_name( "triangleup" ) ); + x49gp_ui_draw_symbol( cr, &widget->style->white, 10.0, 0.0, TRUE, 134 /* 168 */, 8 /* 25 */, + symbol_get_by_name( "triangleup" ) ); - left_color = UI_COLOR_WHITE; - right_color = UI_COLOR_ORANGE; - below_color = UI_COLOR_BLUE; - break; + left_color = UI_COLOR_WHITE; + right_color = UI_COLOR_ORANGE; + below_color = UI_COLOR_BLUE; + break; } cairo_destroy( cr ); for ( int i = 0; i < ui->nr_buttons; i++ ) { - key = ui->buttons[ i ].key; + key = ui->buttons[ i ].key; - if ( key->left ) { - bitmap_font_text_size( &tiny_font, key->left, &wl, &hl, &a, &dl ); - if ( !key->right ) { - xl = key->x + ( key->width - wl ) / 2; - bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ left_color ], &tiny_font, ui->kb_x_offset + xl, - ui->kb_y_offset + key->y - hl + dl + 1, key->left ); - } - } + if ( key->left ) { + bitmap_font_text_size( &tiny_font, key->left, &wl, &hl, &a, &dl ); + if ( !key->right ) { + xl = key->x + ( key->width - wl ) / 2; + bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ left_color ], &tiny_font, ui->kb_x_offset + xl, + ui->kb_y_offset + key->y - hl + dl + 1, key->left ); + } + } - if ( key->right ) { - bitmap_font_text_size( &tiny_font, key->right, &wr, &hr, &a, &dr ); - if ( !key->left ) { - xr = key->x + ( key->width - wr ) / 2; - bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ right_color ], &tiny_font, ui->kb_x_offset + xr, - ui->kb_y_offset + key->y - hr + dr + 1, key->right ); - } - } + if ( key->right ) { + bitmap_font_text_size( &tiny_font, key->right, &wr, &hr, &a, &dr ); + if ( !key->left ) { + xr = key->x + ( key->width - wr ) / 2; + bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ right_color ], &tiny_font, ui->kb_x_offset + xr, + ui->kb_y_offset + key->y - hr + dr + 1, key->right ); + } + } - if ( key->left && key->right ) { - xl = key->x; - xr = key->x + key->width - 1 - wr; + if ( key->left && key->right ) { + xl = key->x; + xr = key->x + key->width - 1 - wr; - if ( wl + wr > key->width - 4 ) { - xl += ( key->width - 4 - ( wl + wr ) ) / 2; - xr -= ( key->width - 4 - ( wl + wr ) ) / 2; - } + if ( wl + wr > key->width - 4 ) { + xl += ( key->width - 4 - ( wl + wr ) ) / 2; + xr -= ( key->width - 4 - ( wl + wr ) ) / 2; + } - bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ left_color ], &tiny_font, ui->kb_x_offset + xl, - ui->kb_y_offset + key->y - hl + dl + 1, key->left ); + bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ left_color ], &tiny_font, ui->kb_x_offset + xl, + ui->kb_y_offset + key->y - hl + dl + 1, key->left ); - bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ right_color ], &tiny_font, ui->kb_x_offset + xr, - ui->kb_y_offset + key->y - hr + dr + 1, key->right ); - } + bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ right_color ], &tiny_font, ui->kb_x_offset + xr, + ui->kb_y_offset + key->y - hr + dr + 1, key->right ); + } - if ( key->below ) { - bitmap_font_text_size( &tiny_font, key->below, &wl, &hl, &a, &dl ); - xl = key->x + ( key->width - wl ) / 2; + if ( key->below ) { + bitmap_font_text_size( &tiny_font, key->below, &wl, &hl, &a, &dl ); + xl = key->x + ( key->width - wl ) / 2; - bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ below_color ], &tiny_font, ui->kb_x_offset + xl, - ui->kb_y_offset + key->y + key->height + 2, key->below ); - } + bitmap_font_draw_text( ui->bg_pixmap, &ui->colors[ below_color ], &tiny_font, ui->kb_x_offset + xl, + ui->kb_y_offset + key->y + key->height + 2, key->below ); + } #if 0 /* Debug Button Layout */ gdk_draw_rectangle(ui->bg_pixmap, ui->window->style->white_gc, @@ -3207,10 +3207,10 @@ static gboolean x49gp_window_button_press( GtkWidget* widget, GdkEventButton* ev gdk_window_raise( widget->window ); if ( event->type != GDK_BUTTON_PRESS ) - return FALSE; + return FALSE; if ( event->button != 1 ) - return FALSE; + return FALSE; gdk_window_begin_move_drag( widget->window, event->button, event->x_root, event->y_root, event->time ); @@ -3237,17 +3237,17 @@ static int gui_init( x49gp_module_t* module ) ui = malloc( sizeof( x49gp_ui_t ) ); if ( NULL == ui ) { - fprintf( stderr, "%s: %s:%u: Out of memory\n", x49gp->progname, __FUNCTION__, __LINE__ ); - return -ENOMEM; + fprintf( stderr, "%s: %s:%u: Out of memory\n", x49gp->progname, __FUNCTION__, __LINE__ ); + return -ENOMEM; } memset( ui, 0, sizeof( x49gp_ui_t ) ); ui->nr_buttons = X49GP_UI_NR_KEYS; ui->buttons = malloc( ui->nr_buttons * sizeof( x49gp_ui_button_t ) ); if ( NULL == ui->buttons ) { - fprintf( stderr, "%s: %s:%u: Out of memory\n", x49gp->progname, __FUNCTION__, __LINE__ ); - free( ui ); - return -ENOMEM; + fprintf( stderr, "%s: %s:%u: Out of memory\n", x49gp->progname, __FUNCTION__, __LINE__ ); + free( ui ); + return -ENOMEM; } memset( ui->buttons, 0, ui->nr_buttons * sizeof( x49gp_ui_button_t ) ); @@ -3278,32 +3278,32 @@ static int gui_load( x49gp_module_t* module, GKeyFile* keyfile ) x49gp_module_get_string( module, keyfile, "type", "hp50g", &typestr ); if ( !strcmp( typestr, "hp49g+" ) ) - ui->calculator = UI_CALCULATOR_HP49GP; + ui->calculator = UI_CALCULATOR_HP49GP; else if ( !strcmp( typestr, "hp49g+/newrpl" ) ) - ui->calculator = UI_CALCULATOR_HP49GP_NEWRPL; + ui->calculator = UI_CALCULATOR_HP49GP_NEWRPL; else if ( !strcmp( typestr, "hp50g" ) ) - ui->calculator = UI_CALCULATOR_HP50G; + ui->calculator = UI_CALCULATOR_HP50G; else if ( !strcmp( typestr, "hp50g/newrpl" ) ) - ui->calculator = UI_CALCULATOR_HP50G_NEWRPL; + ui->calculator = UI_CALCULATOR_HP50G_NEWRPL; else { - fprintf( stderr, "Invalid calculator type, reverting to default\n" ); - ui->calculator = UI_CALCULATOR_HP50G; + fprintf( stderr, "Invalid calculator type, reverting to default\n" ); + ui->calculator = UI_CALCULATOR_HP50G; } x49gp_module_get_string( module, keyfile, "name", - ui->calculator == UI_CALCULATOR_HP49GP ? "HP 49g+" - : ui->calculator == UI_CALCULATOR_HP49GP_NEWRPL ? "HP 49g+ / newRPL" - : ui->calculator == UI_CALCULATOR_HP50G ? "HP 50g" - : "HP 50g / newRPL", - &( ui->name ) ); + ui->calculator == UI_CALCULATOR_HP49GP ? "HP 49g+" + : ui->calculator == UI_CALCULATOR_HP49GP_NEWRPL ? "HP 49g+ / newRPL" + : ui->calculator == UI_CALCULATOR_HP50G ? "HP 50g" + : "HP 50g / newRPL", + &( ui->name ) ); fd = x49gp_module_open_rodata( module, - ui->calculator == UI_CALCULATOR_HP49GP || ui->calculator == UI_CALCULATOR_HP49GP_NEWRPL - ? "hp49g+-cropped.png" - : "hp50g-cropped.png", - &imagefile ); + ui->calculator == UI_CALCULATOR_HP49GP || ui->calculator == UI_CALCULATOR_HP49GP_NEWRPL + ? "hp49g+-cropped.png" + : "hp50g-cropped.png", + &imagefile ); if ( fd < 0 ) - return fd; + return fd; gdk_pixbuf_get_file_info( imagefile, &ui->width, &ui->height ); @@ -3342,15 +3342,15 @@ static int gui_load( x49gp_module_t* module, GKeyFile* keyfile ) gtk_widget_realize( ui->window ); ui->shapes[ UI_SHAPE_BUTTON_TINY ] = - gdk_bitmap_create_from_data( NULL, ( char* )button_tiny_bits, button_tiny_width, button_tiny_height ); + gdk_bitmap_create_from_data( NULL, ( char* )button_tiny_bits, button_tiny_width, button_tiny_height ); ui->shapes[ UI_SHAPE_BUTTON_SMALL ] = - gdk_bitmap_create_from_data( NULL, ( char* )button_small_bits, button_small_width, button_small_height ); + gdk_bitmap_create_from_data( NULL, ( char* )button_small_bits, button_small_width, button_small_height ); ui->shapes[ UI_SHAPE_BUTTON_NORMAL ] = - gdk_bitmap_create_from_data( NULL, ( char* )button_normal_bits, button_normal_width, button_normal_height ); + gdk_bitmap_create_from_data( NULL, ( char* )button_normal_bits, button_normal_width, button_normal_height ); ui->shapes[ UI_SHAPE_BUTTON_LARGE ] = - gdk_bitmap_create_from_data( NULL, ( char* )button_large_bits, button_large_width, button_large_height ); + gdk_bitmap_create_from_data( NULL, ( char* )button_large_bits, button_large_width, button_large_height ); ui->shapes[ UI_SHAPE_BUTTON_ROUND ] = - gdk_bitmap_create_from_data( NULL, ( char* )button_round_bits, button_round_width, button_round_height ); + gdk_bitmap_create_from_data( NULL, ( char* )button_round_bits, button_round_width, button_round_height ); ui->fixed = gtk_fixed_new(); gtk_container_add( GTK_CONTAINER( ui->window ), ui->fixed ); @@ -3394,60 +3394,60 @@ static int gui_load( x49gp_module_t* module, GKeyFile* keyfile ) x49gp_ui_place_at( x49gp, GTK_FIXED( ui->fixed ), screen_box, ui->lcd_x_offset, ui->lcd_y_offset, ui->lcd_width, ui->lcd_height ); for ( int i = 0; i < ui->nr_buttons; i++ ) { - switch ( ui->calculator ) { - case UI_CALCULATOR_HP49GP: - key = &x49gp_ui_keys[ i ]; - break; - case UI_CALCULATOR_HP49GP_NEWRPL: - key = &x49gp_newrpl_ui_keys[ i ]; - break; - default: - ui->calculator = UI_CALCULATOR_HP50G; - /* fall through */ - case UI_CALCULATOR_HP50G: - key = &x50g_ui_keys[ i ]; - break; - case UI_CALCULATOR_HP50G_NEWRPL: - key = &x50g_newrpl_ui_keys[ i ]; - break; - } - button = &ui->buttons[ i ]; + switch ( ui->calculator ) { + case UI_CALCULATOR_HP49GP: + key = &x49gp_ui_keys[ i ]; + break; + case UI_CALCULATOR_HP49GP_NEWRPL: + key = &x49gp_newrpl_ui_keys[ i ]; + break; + default: + ui->calculator = UI_CALCULATOR_HP50G; + /* fall through */ + case UI_CALCULATOR_HP50G: + key = &x50g_ui_keys[ i ]; + break; + case UI_CALCULATOR_HP50G_NEWRPL: + key = &x50g_newrpl_ui_keys[ i ]; + break; + } + button = &ui->buttons[ i ]; - button->x49gp = x49gp; - button->key = key; + button->x49gp = x49gp; + button->key = key; - button->button = gtk_button_new(); - gtk_widget_set_size_request( button->button, key->width, key->height ); - gtk_widget_set( button->button, "can-focus", FALSE, NULL ); + button->button = gtk_button_new(); + gtk_widget_set_size_request( button->button, key->width, key->height ); + gtk_widget_set( button->button, "can-focus", FALSE, NULL ); - x49gp_ui_button_pixmaps_init( x49gp, button, key->color ); + x49gp_ui_button_pixmaps_init( x49gp, button, key->color ); - if ( key->label ) { - button->label = gtk_label_new( "" ); - gtk_widget_set_style( button->label, button->button->style ); - gtk_container_add( GTK_CONTAINER( button->button ), button->label ); + if ( key->label ) { + button->label = gtk_label_new( "" ); + gtk_widget_set_style( button->label, button->button->style ); + gtk_container_add( GTK_CONTAINER( button->button ), button->label ); - g_signal_connect( G_OBJECT( button->label ), "expose-event", G_CALLBACK( x49gp_button_expose_event ), button ); + g_signal_connect( G_OBJECT( button->label ), "expose-event", G_CALLBACK( x49gp_button_expose_event ), button ); - g_signal_connect_after( G_OBJECT( button->label ), "realize", G_CALLBACK( x49gp_button_realize ), button ); - } + g_signal_connect_after( G_OBJECT( button->label ), "realize", G_CALLBACK( x49gp_button_realize ), button ); + } - button->box = gtk_event_box_new(); - gtk_event_box_set_visible_window( GTK_EVENT_BOX( button->box ), TRUE ); - gtk_event_box_set_above_child( GTK_EVENT_BOX( button->box ), FALSE ); - gtk_widget_shape_combine_mask( button->box, ui->shapes[ key->shape ], 0, 0 ); - gtk_container_add( GTK_CONTAINER( button->box ), button->button ); + button->box = gtk_event_box_new(); + gtk_event_box_set_visible_window( GTK_EVENT_BOX( button->box ), TRUE ); + gtk_event_box_set_above_child( GTK_EVENT_BOX( button->box ), FALSE ); + gtk_widget_shape_combine_mask( button->box, ui->shapes[ key->shape ], 0, 0 ); + gtk_container_add( GTK_CONTAINER( button->box ), button->button ); - x49gp_ui_place_at( x49gp, GTK_FIXED( ui->fixed ), button->box, ui->kb_x_offset + key->x, ui->kb_y_offset + key->y, key->width, - key->height ); + x49gp_ui_place_at( x49gp, GTK_FIXED( ui->fixed ), button->box, ui->kb_x_offset + key->x, ui->kb_y_offset + key->y, key->width, + key->height ); - g_signal_connect( G_OBJECT( button->button ), "button-press-event", G_CALLBACK( x49gp_ui_button_press ), button ); + g_signal_connect( G_OBJECT( button->button ), "button-press-event", G_CALLBACK( x49gp_ui_button_press ), button ); - g_signal_connect( G_OBJECT( button->button ), "button-release-event", G_CALLBACK( x49gp_ui_button_release ), button ); + g_signal_connect( G_OBJECT( button->button ), "button-release-event", G_CALLBACK( x49gp_ui_button_release ), button ); - g_signal_connect( G_OBJECT( button->button ), "leave-notify-event", G_CALLBACK( x49gp_ui_button_leave ), button ); + g_signal_connect( G_OBJECT( button->button ), "leave-notify-event", G_CALLBACK( x49gp_ui_button_leave ), button ); - gtk_widget_add_events( button->button, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK ); + gtk_widget_add_events( button->button, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK ); } ui->menu = gtk_menu_new(); @@ -3464,13 +3464,13 @@ static int gui_load( x49gp_module_t* module, GKeyFile* keyfile ) ui->menu_unmount = menu_unmount; if ( x49gp->debug_port != 0 ) { - gtk_menu_shell_append( GTK_MENU_SHELL( ui->menu ), gtk_separator_menu_item_new() ); - menu_debug = gtk_menu_item_new_with_label( "Start debugger" ); - gtk_menu_shell_append( GTK_MENU_SHELL( ui->menu ), menu_debug ); - g_signal_connect( G_OBJECT( menu_debug ), "activate", G_CALLBACK( x49gp_ui_debug ), x49gp ); - ui->menu_debug = menu_debug; + gtk_menu_shell_append( GTK_MENU_SHELL( ui->menu ), gtk_separator_menu_item_new() ); + menu_debug = gtk_menu_item_new_with_label( "Start debugger" ); + gtk_menu_shell_append( GTK_MENU_SHELL( ui->menu ), menu_debug ); + g_signal_connect( G_OBJECT( menu_debug ), "activate", G_CALLBACK( x49gp_ui_debug ), x49gp ); + ui->menu_debug = menu_debug; } else - ui->menu_debug = NULL; + ui->menu_debug = NULL; gtk_menu_shell_append( GTK_MENU_SHELL( ui->menu ), gtk_separator_menu_item_new() ); menu_reset = gtk_menu_item_new_with_label( "Reset" ); @@ -3510,10 +3510,10 @@ static int gui_save( x49gp_module_t* module, GKeyFile* keyfile ) x49gp_ui_t* ui = module->user_data; x49gp_module_set_string( module, keyfile, "type", - ui->calculator == UI_CALCULATOR_HP49GP ? "hp49g+" - : ui->calculator == UI_CALCULATOR_HP49GP_NEWRPL ? "hp49g+/newrpl" - : ui->calculator == UI_CALCULATOR_HP50G ? "hp50g" - : "hp50g/newrpl" ); + ui->calculator == UI_CALCULATOR_HP49GP ? "hp49g+" + : ui->calculator == UI_CALCULATOR_HP49GP_NEWRPL ? "hp49g+/newrpl" + : ui->calculator == UI_CALCULATOR_HP50G ? "hp50g" + : "hp50g/newrpl" ); x49gp_module_set_string( module, keyfile, "name", ui->name ); return 0; @@ -3524,7 +3524,7 @@ int x49gp_ui_init( x49gp_t* x49gp ) x49gp_module_t* module; if ( x49gp_module_init( x49gp, "gui", gui_init, gui_exit, gui_reset, gui_load, gui_save, NULL, &module ) ) - return -1; + return -1; return x49gp_module_register( module ); } diff --git a/src/x49gp.h b/src/x49gp.h index 201452f..814d329 100644 --- a/src/x49gp.h +++ b/src/x49gp.h @@ -20,148 +20,115 @@ #include "list.h" /* LD TEMPO HACK */ -extern uint8_t *phys_ram_base; +extern uint8_t* phys_ram_base; extern int phys_ram_size; -typedef enum { - X49GP_ARM_RUN = 0, - X49GP_ARM_SLEEP, - X49GP_ARM_OFF -} x49gp_arm_idle_t; +typedef enum { X49GP_ARM_RUN = 0, X49GP_ARM_SLEEP, X49GP_ARM_OFF } x49gp_arm_idle_t; -typedef enum { - X49GP_RESET_POWER_ON = 0, - X49GP_RESET_POWER_OFF, - X49GP_RESET_WATCHDOG -} x49gp_reset_t; +typedef enum { X49GP_RESET_POWER_ON = 0, X49GP_RESET_POWER_OFF, X49GP_RESET_WATCHDOG } x49gp_reset_t; struct __x49gp_module_s__; typedef struct __x49gp_module_s__ x49gp_module_t; struct __x49gp_module_s__ { - const char *name; + const char* name; - int (*init) (x49gp_module_t *); - int (*exit) (x49gp_module_t *); + int ( *init )( x49gp_module_t* ); + int ( *exit )( x49gp_module_t* ); - int (*reset) (x49gp_module_t *, x49gp_reset_t); + int ( *reset )( x49gp_module_t*, x49gp_reset_t ); - int (*load) (x49gp_module_t *, GKeyFile *); - int (*save) (x49gp_module_t *, GKeyFile *); + int ( *load )( x49gp_module_t*, GKeyFile* ); + int ( *save )( x49gp_module_t*, GKeyFile* ); - void *user_data; + void* user_data; - x49gp_t *x49gp; - struct list_head list; + x49gp_t* x49gp; + struct list_head list; }; -typedef enum { - X49GP_REINIT_NONE = 0, - X49GP_REINIT_REBOOT_ONLY, - X49GP_REINIT_FLASH, - X49GP_REINIT_FLASH_FULL -} x49gp_reinit_t; +typedef enum { X49GP_REINIT_NONE = 0, X49GP_REINIT_REBOOT_ONLY, X49GP_REINIT_FLASH, X49GP_REINIT_FLASH_FULL } x49gp_reinit_t; struct __x49gp_s__ { - CPUARMState *env; + CPUARMState* env; - struct list_head modules; + struct list_head modules; - void *s3c2410_lcd; - void *s3c2410_timer; - void *s3c2410_watchdog; - void *s3c2410_intc; - void *s3c2410_io_port; - void *s3c2410_sdi; + void* s3c2410_lcd; + void* s3c2410_timer; + void* s3c2410_watchdog; + void* s3c2410_intc; + void* s3c2410_io_port; + void* s3c2410_sdi; - void *timer; - uint8_t *sram; + void* timer; + uint8_t* sram; - uint32_t MCLK; - uint32_t UCLK; + uint32_t MCLK; + uint32_t UCLK; - uint32_t FCLK; - uint32_t HCLK; - uint32_t PCLK; - int PCLK_ratio; + uint32_t FCLK; + uint32_t HCLK; + uint32_t PCLK; + int PCLK_ratio; - clock_t clk_tck; - unsigned long emulator_fclk; + clock_t clk_tck; + unsigned long emulator_fclk; - unsigned char keybycol[8]; - unsigned char keybyrow[8]; + unsigned char keybycol[ 8 ]; + unsigned char keybyrow[ 8 ]; - x49gp_timer_t *gtk_timer; - x49gp_timer_t *lcd_timer; + x49gp_timer_t* gtk_timer; + x49gp_timer_t* lcd_timer; - x49gp_arm_idle_t arm_idle; - int arm_exit; + x49gp_arm_idle_t arm_idle; + int arm_exit; - x49gp_ui_t *ui; + x49gp_ui_t* ui; - GKeyFile *config; - const char *progname; - const char *progpath; - const char *basename; - int debug_port; - x49gp_reinit_t startup_reinit; - char *firmware; + GKeyFile* config; + const char* progname; + const char* progpath; + const char* basename; + int debug_port; + x49gp_reinit_t startup_reinit; + char* firmware; }; -extern void x49gp_set_idle(x49gp_t *, x49gp_arm_idle_t idle); +extern void x49gp_set_idle( x49gp_t*, x49gp_arm_idle_t idle ); -extern int x49gp_module_init(x49gp_t *x49gp, const char *name, - int (*init)(x49gp_module_t *), - int (*exit)(x49gp_module_t *), - int (*reset)(x49gp_module_t *, x49gp_reset_t), - int (*load)(x49gp_module_t *, GKeyFile *), - int (*save)(x49gp_module_t *, GKeyFile *), - void *user_data, x49gp_module_t **module); +extern int x49gp_module_init( x49gp_t* x49gp, const char* name, int ( *init )( x49gp_module_t* ), int ( *exit )( x49gp_module_t* ), + int ( *reset )( x49gp_module_t*, x49gp_reset_t ), int ( *load )( x49gp_module_t*, GKeyFile* ), + int ( *save )( x49gp_module_t*, GKeyFile* ), void* user_data, x49gp_module_t** module ); -extern int x49gp_module_register(x49gp_module_t *module); -extern int x49gp_module_unregister(x49gp_module_t *module); +extern int x49gp_module_register( x49gp_module_t* module ); +extern int x49gp_module_unregister( x49gp_module_t* module ); -extern int x49gp_module_get_filename(x49gp_module_t *module, GKeyFile *, - const char *, char *, char **, - char **); -extern int x49gp_module_set_filename(x49gp_module_t *module, GKeyFile *, - const char *, const char *); -extern int x49gp_module_get_int(x49gp_module_t *module, GKeyFile *, - const char *, int, int *); -extern int x49gp_module_set_int(x49gp_module_t *module, GKeyFile *, - const char *, int); -extern int x49gp_module_get_uint(x49gp_module_t *module, GKeyFile *, - const char *, - unsigned int, unsigned int *); -extern int x49gp_module_set_uint(x49gp_module_t *module, GKeyFile *, - const char *, unsigned int); -extern int x49gp_module_get_u32(x49gp_module_t *module, GKeyFile *, - const char *, uint32_t, uint32_t *); -extern int x49gp_module_set_u32(x49gp_module_t *module, GKeyFile *, - const char *, uint32_t); -extern int x49gp_module_get_u64(x49gp_module_t *module, GKeyFile *, - const char *, uint64_t, uint64_t *); -extern int x49gp_module_set_u64(x49gp_module_t *module, GKeyFile *, - const char *, uint64_t); -extern int x49gp_module_get_string(x49gp_module_t *module, GKeyFile *, - const char *, char *, char **); -extern int x49gp_module_set_string(x49gp_module_t *module, GKeyFile *, - const char *, const char *); -extern int x49gp_module_open_rodata(x49gp_module_t *module, - const char *name, - char **path); +extern int x49gp_module_get_filename( x49gp_module_t* module, GKeyFile*, const char*, char*, char**, char** ); +extern int x49gp_module_set_filename( x49gp_module_t* module, GKeyFile*, const char*, const char* ); +extern int x49gp_module_get_int( x49gp_module_t* module, GKeyFile*, const char*, int, int* ); +extern int x49gp_module_set_int( x49gp_module_t* module, GKeyFile*, const char*, int ); +extern int x49gp_module_get_uint( x49gp_module_t* module, GKeyFile*, const char*, unsigned int, unsigned int* ); +extern int x49gp_module_set_uint( x49gp_module_t* module, GKeyFile*, const char*, unsigned int ); +extern int x49gp_module_get_u32( x49gp_module_t* module, GKeyFile*, const char*, uint32_t, uint32_t* ); +extern int x49gp_module_set_u32( x49gp_module_t* module, GKeyFile*, const char*, uint32_t ); +extern int x49gp_module_get_u64( x49gp_module_t* module, GKeyFile*, const char*, uint64_t, uint64_t* ); +extern int x49gp_module_set_u64( x49gp_module_t* module, GKeyFile*, const char*, uint64_t ); +extern int x49gp_module_get_string( x49gp_module_t* module, GKeyFile*, const char*, char*, char** ); +extern int x49gp_module_set_string( x49gp_module_t* module, GKeyFile*, const char*, const char* ); +extern int x49gp_module_open_rodata( x49gp_module_t* module, const char* name, char** path ); -extern void s3c2410_sdi_unmount(x49gp_t *x49gp); -extern int s3c2410_sdi_mount(x49gp_t *x49gp, char *filename); -extern int s3c2410_sdi_is_mounted(x49gp_t *x49gp); +extern void s3c2410_sdi_unmount( x49gp_t* x49gp ); +extern int s3c2410_sdi_mount( x49gp_t* x49gp, char* filename ); +extern int s3c2410_sdi_is_mounted( x49gp_t* x49gp ); -extern int x49gp_modules_init(x49gp_t *); -extern int x49gp_modules_exit(x49gp_t *); -extern int x49gp_modules_reset(x49gp_t *, x49gp_reset_t); -extern int x49gp_modules_load(x49gp_t *, const char *); -extern int x49gp_modules_save(x49gp_t *, const char *); +extern int x49gp_modules_init( x49gp_t* ); +extern int x49gp_modules_exit( x49gp_t* ); +extern int x49gp_modules_reset( x49gp_t*, x49gp_reset_t ); +extern int x49gp_modules_load( x49gp_t*, const char* ); +extern int x49gp_modules_save( x49gp_t*, const char* ); -extern int x49gp_flash_init(x49gp_t *); -extern int x49gp_sram_init(x49gp_t *); +extern int x49gp_flash_init( x49gp_t* ); +extern int x49gp_sram_init( x49gp_t* ); #endif /* !(_X49GP_H) */ diff --git a/src/x49gp_timer.h b/src/x49gp_timer.h index 109f736..827a6cb 100644 --- a/src/x49gp_timer.h +++ b/src/x49gp_timer.h @@ -8,26 +8,26 @@ #include "list.h" -#define X49GP_TIMER_VIRTUAL 0 -#define X49GP_TIMER_REALTIME 1 +#define X49GP_TIMER_VIRTUAL 0 +#define X49GP_TIMER_REALTIME 1 -int64_t x49gp_get_clock(void); +int64_t x49gp_get_clock( void ); -typedef void (*x49gp_timer_cb_t) (void *); +typedef void ( *x49gp_timer_cb_t )( void* ); typedef struct x49gp_timer_s x49gp_timer_t; -x49gp_timer_t *x49gp_new_timer(long type, x49gp_timer_cb_t, void *user_data); -void x49gp_free_timer(x49gp_timer_t *); +x49gp_timer_t* x49gp_new_timer( long type, x49gp_timer_cb_t, void* user_data ); +void x49gp_free_timer( x49gp_timer_t* ); -void x49gp_mod_timer(x49gp_timer_t *, int64_t expires); -void x49gp_del_timer(x49gp_timer_t *); -int x49gp_timer_pending(x49gp_timer_t *); -int64_t x49gp_timer_expires(x49gp_timer_t *); +void x49gp_mod_timer( x49gp_timer_t*, int64_t expires ); +void x49gp_del_timer( x49gp_timer_t* ); +int x49gp_timer_pending( x49gp_timer_t* ); +int64_t x49gp_timer_expires( x49gp_timer_t* ); -#define X49GP_GTK_REFRESH_INTERVAL 30000LL -#define X49GP_LCD_REFRESH_INTERVAL 50000LL +#define X49GP_GTK_REFRESH_INTERVAL 30000LL +#define X49GP_LCD_REFRESH_INTERVAL 50000LL -int x49gp_main_loop(x49gp_t *); -int x49gp_timer_init(x49gp_t *); +int x49gp_main_loop( x49gp_t* ); +int x49gp_timer_init( x49gp_t* ); #endif /* !(_X49GP_TIMER_H) */ diff --git a/src/x49gp_types.h b/src/x49gp_types.h index f6b52d7..36ba3df 100644 --- a/src/x49gp_types.h +++ b/src/x49gp_types.h @@ -15,9 +15,9 @@ typedef unsigned int u32; typedef unsigned long long u64; struct __x49gp_s__; -typedef struct __x49gp_s__ x49gp_t; +typedef struct __x49gp_s__ x49gp_t; struct __x49gp_ui_s__; -typedef struct __x49gp_ui_s__ x49gp_ui_t; +typedef struct __x49gp_ui_s__ x49gp_ui_t; #endif /* !(_X49GP_TYPES_H) */ diff --git a/src/x49gp_ui.h b/src/x49gp_ui.h index 106dd3b..f3557e7 100644 --- a/src/x49gp_ui.h +++ b/src/x49gp_ui.h @@ -7,145 +7,139 @@ #include "x49gp_types.h" typedef enum { - UI_COLOR_BLACK = 0, - UI_COLOR_WHITE, - UI_COLOR_YELLOW, - UI_COLOR_RED, - UI_COLOR_GREEN, - UI_COLOR_SILVER, - UI_COLOR_ORANGE, - UI_COLOR_BLUE, - UI_COLOR_GRAYSCALE_0, - UI_COLOR_GRAYSCALE_1, - UI_COLOR_GRAYSCALE_2, - UI_COLOR_GRAYSCALE_3, - UI_COLOR_GRAYSCALE_4, - UI_COLOR_GRAYSCALE_5, - UI_COLOR_GRAYSCALE_6, - UI_COLOR_GRAYSCALE_7, - UI_COLOR_GRAYSCALE_8, - UI_COLOR_GRAYSCALE_9, - UI_COLOR_GRAYSCALE_10, - UI_COLOR_GRAYSCALE_11, - UI_COLOR_GRAYSCALE_12, - UI_COLOR_GRAYSCALE_13, - UI_COLOR_GRAYSCALE_14, - UI_COLOR_GRAYSCALE_15, - UI_COLOR_MAX + UI_COLOR_BLACK = 0, + UI_COLOR_WHITE, + UI_COLOR_YELLOW, + UI_COLOR_RED, + UI_COLOR_GREEN, + UI_COLOR_SILVER, + UI_COLOR_ORANGE, + UI_COLOR_BLUE, + UI_COLOR_GRAYSCALE_0, + UI_COLOR_GRAYSCALE_1, + UI_COLOR_GRAYSCALE_2, + UI_COLOR_GRAYSCALE_3, + UI_COLOR_GRAYSCALE_4, + UI_COLOR_GRAYSCALE_5, + UI_COLOR_GRAYSCALE_6, + UI_COLOR_GRAYSCALE_7, + UI_COLOR_GRAYSCALE_8, + UI_COLOR_GRAYSCALE_9, + UI_COLOR_GRAYSCALE_10, + UI_COLOR_GRAYSCALE_11, + UI_COLOR_GRAYSCALE_12, + UI_COLOR_GRAYSCALE_13, + UI_COLOR_GRAYSCALE_14, + UI_COLOR_GRAYSCALE_15, + UI_COLOR_MAX } x49gp_ui_color_t; typedef enum { - UI_SHAPE_BUTTON_TINY = 0, - UI_SHAPE_BUTTON_SMALL, - UI_SHAPE_BUTTON_NORMAL, - UI_SHAPE_BUTTON_LARGE, - UI_SHAPE_BUTTON_ROUND, - UI_SHAPE_MAX + UI_SHAPE_BUTTON_TINY = 0, + UI_SHAPE_BUTTON_SMALL, + UI_SHAPE_BUTTON_NORMAL, + UI_SHAPE_BUTTON_LARGE, + UI_SHAPE_BUTTON_ROUND, + UI_SHAPE_MAX } x49gp_ui_shape_t; -typedef enum { - UI_LAYOUT_LEFT = 0, - UI_LAYOUT_LEFT_NO_SPACE, - UI_LAYOUT_BELOW, - UI_LAYOUT_MAX -} x49gp_ui_layout_t; +typedef enum { UI_LAYOUT_LEFT = 0, UI_LAYOUT_LEFT_NO_SPACE, UI_LAYOUT_BELOW, UI_LAYOUT_MAX } x49gp_ui_layout_t; typedef enum { - UI_CALCULATOR_HP49GP = 0, - UI_CALCULATOR_HP49GP_NEWRPL, - UI_CALCULATOR_HP50G, - UI_CALCULATOR_HP50G_NEWRPL + UI_CALCULATOR_HP49GP = 0, + UI_CALCULATOR_HP49GP_NEWRPL, + UI_CALCULATOR_HP50G, + UI_CALCULATOR_HP50G_NEWRPL } x49gp_ui_calculator_t; - typedef struct { - const char *label; - const char *letter; - const char *left; - const char *right; - const char *below; - x49gp_ui_color_t color; - double font_size; - cairo_font_weight_t font_weight; - x49gp_ui_shape_t shape; - double letter_size; - x49gp_ui_layout_t layout; - int x; - int y; - int width; - int height; - int column; - int row; - unsigned char columnbit; - unsigned char rowbit; - int eint; + const char* label; + const char* letter; + const char* left; + const char* right; + const char* below; + x49gp_ui_color_t color; + double font_size; + cairo_font_weight_t font_weight; + x49gp_ui_shape_t shape; + double letter_size; + x49gp_ui_layout_t layout; + int x; + int y; + int width; + int height; + int column; + int row; + unsigned char columnbit; + unsigned char rowbit; + int eint; } x49gp_ui_key_t; typedef struct { - x49gp_t *x49gp; - const x49gp_ui_key_t *key; - GtkWidget *button; - GtkWidget *label; - GtkWidget *box; - GdkPixmap *pixmap; - gboolean down; - gboolean hold; + x49gp_t* x49gp; + const x49gp_ui_key_t* key; + GtkWidget* button; + GtkWidget* label; + GtkWidget* box; + GdkPixmap* pixmap; + gboolean down; + gboolean hold; } x49gp_ui_button_t; struct __x49gp_ui_s__ { - GtkWidget *window; - GtkWidget *fixed; - GtkWidget *menu; - GtkWidget *menu_unmount; - GtkWidget *menu_debug; + GtkWidget* window; + GtkWidget* fixed; + GtkWidget* menu; + GtkWidget* menu_unmount; + GtkWidget* menu_debug; - GdkPixbuf *bg_pixbuf; - GdkPixmap *bg_pixmap; - GtkWidget *background; + GdkPixbuf* bg_pixbuf; + GdkPixmap* bg_pixmap; + GtkWidget* background; - GdkColor colors[UI_COLOR_MAX]; - GdkBitmap *shapes[UI_SHAPE_MAX]; + GdkColor colors[ UI_COLOR_MAX ]; + GdkBitmap* shapes[ UI_SHAPE_MAX ]; - x49gp_ui_calculator_t calculator; + x49gp_ui_calculator_t calculator; - x49gp_ui_button_t *buttons; - unsigned int nr_buttons; - unsigned int buttons_down; + x49gp_ui_button_t* buttons; + unsigned int nr_buttons; + unsigned int buttons_down; - char *name; + char* name; - GtkWidget *lcd_canvas; - GdkPixmap *lcd_pixmap; + GtkWidget* lcd_canvas; + GdkPixmap* lcd_pixmap; - GdkGC *ann_left_gc; - GdkGC *ann_right_gc; - GdkGC *ann_alpha_gc; - GdkGC *ann_battery_gc; - GdkGC *ann_busy_gc; - GdkGC *ann_io_gc; + GdkGC* ann_left_gc; + GdkGC* ann_right_gc; + GdkGC* ann_alpha_gc; + GdkGC* ann_battery_gc; + GdkGC* ann_busy_gc; + GdkGC* ann_io_gc; - GdkBitmap *ann_left; - GdkBitmap *ann_right; - GdkBitmap *ann_alpha; - GdkBitmap *ann_battery; - GdkBitmap *ann_busy; - GdkBitmap *ann_io; + GdkBitmap* ann_left; + GdkBitmap* ann_right; + GdkBitmap* ann_alpha; + GdkBitmap* ann_battery; + GdkBitmap* ann_busy; + GdkBitmap* ann_io; - gint width; - gint height; + gint width; + gint height; - gint kb_x_offset; - gint kb_y_offset; + gint kb_x_offset; + gint kb_y_offset; - gint lcd_x_offset; - gint lcd_y_offset; - gint lcd_width; - gint lcd_height; - gint lcd_top_margin; + gint lcd_x_offset; + gint lcd_y_offset; + gint lcd_width; + gint lcd_height; + gint lcd_top_margin; }; -int x49gp_ui_init(x49gp_t *x49gp); -void x49gp_ui_show_error(x49gp_t *x49gp, const char *text); -void x49gp_ui_open_firmware(x49gp_t *x49gp, char **filename); +int x49gp_ui_init( x49gp_t* x49gp ); +void x49gp_ui_show_error( x49gp_t* x49gp, const char* text ); +void x49gp_ui_open_firmware( x49gp_t* x49gp, char** filename ); #endif /* !(_X49GP_UI_H) */