Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset d5309df7 in mainline for uspace/srv/bd/qcow_bd/qcow_bd.c


Ignore:
Timestamp:
2021-09-14T06:37:21Z (2 months ago)
Author:
Erik Kučák <riko98@…>
Children:
2ce943a
Parents:
bdad846
Message:

Fixed problem with big endian

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/bd/qcow_bd/qcow_bd.c

    rbdad846 rd5309df7  
    4848static bd_srvs_t bd_srvs;
    4949static fibril_mutex_t dev_lock;
     50static bool isLittleEndian();
    5051static void print_usage(void);
    5152static errno_t qcow_bd_init(const char *fname);
     
    176177}
    177178
     179/* Return 0 for if this system uses big endian, 1 for little endian. */
     180static bool isLittleEndian() {
     181    volatile uint32_t i = 0x01234567;
     182    return (*((uint8_t*)(&i))) == 0x67;
     183}
     184
    178185static errno_t qcow_bd_init(const char *fname)
    179186{
     
    219226
    220227        /* Swap values to big-endian */
    221         header.magic = __builtin_bswap32(header.magic);
    222         header.version = __builtin_bswap32(header.version);
    223         header.backing_file_offset = __builtin_bswap64(header.backing_file_offset);
    224         header.backing_file_size = __builtin_bswap32(header.backing_file_size);
    225         header.mtime = __builtin_bswap32(header.mtime);
    226         header.size = __builtin_bswap64(header.size);
    227         header.crypt_method = __builtin_bswap32(header.crypt_method);
    228         header.l1_table_offset = __builtin_bswap64(header.l1_table_offset);
     228        if (isLittleEndian()) {
     229                header.magic = __builtin_bswap32(header.magic);
     230                header.version = __builtin_bswap32(header.version);
     231                header.backing_file_offset = __builtin_bswap64(header.backing_file_offset);
     232                header.backing_file_size = __builtin_bswap32(header.backing_file_size);
     233                header.mtime = __builtin_bswap32(header.mtime);
     234                header.size = __builtin_bswap64(header.size);
     235                header.crypt_method = __builtin_bswap32(header.crypt_method);
     236                header.l1_table_offset = __builtin_bswap64(header.l1_table_offset);
     237        }
    229238
    230239        if (ferror(img)) {
     
    234243
    235244        /* Verify all values from file header */
    236         if (header.magic == QCOW_MAGIC)
    237                 initialize_state();
     245        if (header.magic != QCOW_MAGIC) {
     246                fprintf(stderr, "File is not in QCOW format!\n");
     247                fclose(img);
     248                return ENOTSUP;
     249        }
     250
     251        initialize_state();
    238252
    239253        if (header.version != QCOW_VERSION) {
     
    295309        }
    296310
    297         l2_table_reference = __builtin_bswap64(l2_table_reference);
     311        if (isLittleEndian())
     312                l2_table_reference = __builtin_bswap64(l2_table_reference);
    298313
    299314        if (l2_table_reference & QCOW_OFLAG_COMPRESSED) {
     
    325340        }
    326341
    327         cluster_reference = __builtin_bswap64(cluster_reference);
     342        if (isLittleEndian())
     343                cluster_reference = __builtin_bswap64(cluster_reference);
    328344
    329345        if (cluster_reference & QCOW_OFLAG_COMPRESSED) {
Note: See TracChangeset for help on using the changeset viewer.