diff options
Diffstat (limited to 'util/cbfstool')
-rw-r--r-- | util/cbfstool/cbfs-mkstage.c | 6 | ||||
-rw-r--r-- | util/cbfstool/cbfs.h | 2 | ||||
-rw-r--r-- | util/cbfstool/cbfs_image.c | 166 | ||||
-rw-r--r-- | util/cbfstool/cbfstool.c | 40 | ||||
-rw-r--r-- | util/cbfstool/common.c | 11 | ||||
-rw-r--r-- | util/cbfstool/common.h | 3 | ||||
-rw-r--r-- | util/cbfstool/ifittool.c | 6 | ||||
-rw-r--r-- | util/cbfstool/swab.h | 56 |
8 files changed, 111 insertions, 179 deletions
diff --git a/util/cbfstool/cbfs-mkstage.c b/util/cbfstool/cbfs-mkstage.c index 7f4a38bef6..be8573861b 100644 --- a/util/cbfstool/cbfs-mkstage.c +++ b/util/cbfstool/cbfs-mkstage.c @@ -80,9 +80,9 @@ static int fill_cbfs_stageheader(struct cbfs_file_attr_stageheader *stageheader, return -1; } - stageheader->loadaddr = htonll(loadaddr); - stageheader->memlen = htonl(memsize); - stageheader->entry_offset = htonl(entry - loadaddr); + stageheader->loadaddr = htobe64(loadaddr); + stageheader->memlen = htobe32(memsize); + stageheader->entry_offset = htobe32(entry - loadaddr); return 0; } diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h index 67dc6163f0..b04dfeca6c 100644 --- a/util/cbfstool/cbfs.h +++ b/util/cbfstool/cbfs.h @@ -54,7 +54,7 @@ static struct typedesc_t filetypes[] unused = { {0, NULL} }; -#define CBFS_SUBHEADER(_p) ( (void *) ((((uint8_t *) (_p)) + ntohl((_p)->offset))) ) +#define CBFS_SUBHEADER(_p) ( (void *) ((((uint8_t *) (_p)) + be32toh((_p)->offset))) ) static inline size_t cbfs_file_attr_hash_size(enum vb2_hash_algorithm algo) { diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c index 1fb19bacd6..ada59fe87e 100644 --- a/util/cbfstool/cbfs_image.c +++ b/util/cbfstool/cbfs_image.c @@ -86,7 +86,7 @@ static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc) entry && cbfs_is_valid_entry(image, entry); entry = cbfs_find_next_entry(image, entry)) { /* Is the header guarded by a CBFS file entry? Then exit */ - if (((char *)entry) + ntohl(entry->offset) == hdr_loc) { + if (((char *)entry) + be32toh(entry->offset) == hdr_loc) { return 0; } last = entry; @@ -95,7 +95,7 @@ static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc) (char *)entry > (char *)hdr_loc) { WARN("CBFS image was created with old cbfstool with size bug. " "Fixing size in last entry...\n"); - last->len = htonl(ntohl(last->len) - image->header.align); + last->len = htobe32(be32toh(last->len) - image->header.align); DEBUG("Last entry has been changed from 0x%x to 0x%x.\n", cbfs_get_entry_addr(image, entry), cbfs_get_entry_addr(image, @@ -141,17 +141,17 @@ static int cbfs_file_get_compression_info(struct cbfs_file *entry, { unsigned int compression = CBFS_COMPRESS_NONE; if (decompressed_size) - *decompressed_size = ntohl(entry->len); + *decompressed_size = be32toh(entry->len); for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); attr != NULL; attr = cbfs_file_next_attr(entry, attr)) { - if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) { + if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) { struct cbfs_file_attr_compression *ac = (struct cbfs_file_attr_compression *)attr; - compression = ntohl(ac->compression); + compression = be32toh(ac->compression); if (decompressed_size) *decompressed_size = - ntohl(ac->decompressed_size); + be32toh(ac->decompressed_size); } } return compression; @@ -165,11 +165,11 @@ static struct cbfs_file_attr_hash *cbfs_file_get_next_hash( attr = cbfs_file_first_attr(entry); if (attr == NULL) return NULL; - if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) + if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) return (struct cbfs_file_attr_hash *)attr; } while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) { - if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) + if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) return (struct cbfs_file_attr_hash *)attr; }; return NULL; @@ -375,12 +375,12 @@ int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst) src_entry = cbfs_find_next_entry(image, src_entry)) { size_t entry_size; - if ((src_entry->type == htonl(CBFS_TYPE_NULL)) || - (src_entry->type == htonl(CBFS_TYPE_CBFSHEADER)) || - (src_entry->type == htonl(CBFS_TYPE_DELETED))) + if ((src_entry->type == htobe32(CBFS_TYPE_NULL)) || + (src_entry->type == htobe32(CBFS_TYPE_CBFSHEADER)) || + (src_entry->type == htobe32(CBFS_TYPE_DELETED))) continue; - entry_size = htonl(src_entry->len) + htonl(src_entry->offset); + entry_size = htobe32(src_entry->len) + htobe32(src_entry->offset); memcpy(dst_entry, src_entry, entry_size); dst_entry = (struct cbfs_file *)( (uintptr_t)dst_entry + align_up(entry_size, align)); @@ -473,8 +473,8 @@ int cbfs_truncate_space(struct buffer *region, uint32_t *size) * maximum size. */ if ((strlen(trailer->filename) != 0) && - (trailer->type != htonl(CBFS_TYPE_NULL)) && - (trailer->type != htonl(CBFS_TYPE_DELETED))) { + (trailer->type != htobe32(CBFS_TYPE_NULL)) && + (trailer->type != htobe32(CBFS_TYPE_DELETED))) { /* nothing to truncate. Return de-facto CBFS size in case it * was already truncated. */ *size = (uint8_t *)entry - (uint8_t *)buffer_get(region); @@ -488,12 +488,12 @@ int cbfs_truncate_space(struct buffer *region, uint32_t *size) static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f) { - return ntohl(f->offset); + return be32toh(f->offset); } static size_t cbfs_file_entry_data_size(const struct cbfs_file *f) { - return ntohl(f->len); + return be32toh(f->len); } static size_t cbfs_file_entry_size(const struct cbfs_file *f) @@ -525,11 +525,11 @@ int cbfs_compact_instance(struct cbfs_image *image) size_t cur_size; size_t empty_metadata_size; size_t spill_size; - uint32_t type = htonl(cur->type); + uint32_t type = htobe32(cur->type); /* Current entry is empty. Kepp track of it. */ - if ((type == htonl(CBFS_TYPE_NULL)) || - (type == htonl(CBFS_TYPE_DELETED))) { + if ((type == htobe32(CBFS_TYPE_NULL)) || + (type == htobe32(CBFS_TYPE_DELETED))) { prev = cur; continue; } @@ -631,7 +631,7 @@ static int cbfs_add_entry_at(struct cbfs_image *image, uint32_t len, header_offset; uint32_t align = image->has_header ? image->header.align : CBFS_ALIGNMENT; - uint32_t header_size = ntohl(header->offset); + uint32_t header_size = be32toh(header->offset); header_offset = content_offset - header_size; if (header_offset % align) @@ -660,8 +660,8 @@ static int cbfs_add_entry_at(struct cbfs_image *image, * to file data. Move attributes forward so the end of the * attribute list still matches the end of the metadata. */ - uint32_t offset = ntohl(entry->offset); - uint32_t attrs = ntohl(entry->attributes_offset); + uint32_t offset = be32toh(entry->offset); + uint32_t attrs = be32toh(entry->attributes_offset); DEBUG("|..|header|content|... <use offset to create entry>\n"); DEBUG("before: attr_offset=0x%x, offset=0x%x\n", attrs, offset); if (attrs == 0) { @@ -671,10 +671,10 @@ static int cbfs_add_entry_at(struct cbfs_image *image, memmove(p + len, p, offset - attrs); memset(p, 0, len); attrs += len; - entry->attributes_offset = htonl(attrs); + entry->attributes_offset = htobe32(attrs); } offset += len; - entry->offset = htonl(offset); + entry->offset = htobe32(offset); DEBUG("after: attr_offset=0x%x, offset=0x%x\n", attrs, offset); } @@ -684,14 +684,14 @@ static int cbfs_add_entry_at(struct cbfs_image *image, image->buffer.data)); assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data == (ptrdiff_t)content_offset); - memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len)); + memcpy(CBFS_SUBHEADER(entry), data, be32toh(entry->len)); if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); // Align the length to a multiple of len_align if (len_align && - ((ntohl(entry->offset) + ntohl(entry->len)) % len_align)) { - size_t off = (ntohl(entry->offset) + ntohl(entry->len)) % len_align; - entry->len = htonl(ntohl(entry->len) + len_align - off); + ((be32toh(entry->offset) + be32toh(entry->len)) % len_align)) { + size_t off = (be32toh(entry->offset) + be32toh(entry->len)) % len_align; + entry->len = htobe32(be32toh(entry->len) + len_align - off); } // Process buffer AFTER entry. @@ -738,7 +738,7 @@ int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, uint32_t addr, addr_next; struct cbfs_file *entry, *next; uint32_t need_size; - uint32_t header_size = ntohl(header->offset); + uint32_t header_size = be32toh(header->offset); need_size = header_size + buffer->size; DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n", @@ -752,7 +752,7 @@ int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, entry && cbfs_is_valid_entry(image, entry); entry = cbfs_find_next_entry(image, entry)) { - entry_type = ntohl(entry->type); + entry_type = be32toh(entry->type); if (entry_type != CBFS_TYPE_NULL) continue; @@ -975,7 +975,7 @@ static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch, struct cbfs_file_attr_stageheader *stage = NULL; for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); attr != NULL; attr = cbfs_file_next_attr(entry, attr)) { - if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) { + if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) { stage = (struct cbfs_file_attr_stageheader *)attr; break; } @@ -1000,7 +1000,7 @@ static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch, /* Rmodule couldn't do anything with the data. Continue on with SELF. */ - ehdr.e_entry = ntohll(stage->loadaddr) + ntohl(stage->entry_offset); + ehdr.e_entry = be64toh(stage->loadaddr) + be32toh(stage->entry_offset); ew = elf_writer_init(&ehdr); if (ew == NULL) { @@ -1011,9 +1011,9 @@ static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch, memset(&shdr, 0, sizeof(shdr)); shdr.sh_type = SHT_PROGBITS; shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; - shdr.sh_addr = ntohll(stage->loadaddr); + shdr.sh_addr = be64toh(stage->loadaddr); shdr.sh_size = buffer_size(buff); - empty_sz = ntohl(stage->memlen) - buffer_size(buff); + empty_sz = be32toh(stage->memlen) - buffer_size(buff); if (elf_writer_add_section(ew, &shdr, buff, ".program")) { ERROR("Unable to add ELF section: .program\n"); @@ -1028,7 +1028,7 @@ static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch, memset(&shdr, 0, sizeof(shdr)); shdr.sh_type = SHT_NOBITS; shdr.sh_flags = SHF_WRITE | SHF_ALLOC; - shdr.sh_addr = ntohl(stage->loadaddr) + buffer_size(buff); + shdr.sh_addr = be32toh(stage->loadaddr) + buffer_size(buff); shdr.sh_size = empty_sz; if (elf_writer_add_section(ew, &shdr, &b, ".empty")) { ERROR("Unable to add ELF section: .empty\n"); @@ -1230,7 +1230,7 @@ int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, return -1; } - unsigned int compressed_size = ntohl(entry->len); + unsigned int compressed_size = be32toh(entry->len); unsigned int decompressed_size = 0; unsigned int compression = cbfs_file_get_compression_info(entry, &decompressed_size); @@ -1252,7 +1252,7 @@ int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n", entry_name, cbfs_get_entry_addr(image, entry), - get_cbfs_entry_type_name(ntohl(entry->type)), compressed_size, + get_cbfs_entry_type_name(be32toh(entry->type)), compressed_size, decompressed_size); buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0); @@ -1274,7 +1274,7 @@ int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, if (do_processing) { int (*make_elf)(struct buffer *, uint32_t, struct cbfs_file *) = NULL; - switch (ntohl(entry->type)) { + switch (be32toh(entry->type)) { case CBFS_TYPE_STAGE: make_elf = cbfs_stage_make_elf; break; @@ -1312,7 +1312,7 @@ int cbfs_remove_entry(struct cbfs_image *image, const char *name) } DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", entry->filename, cbfs_get_entry_addr(image, entry)); - entry->type = htonl(CBFS_TYPE_DELETED); + entry->type = htobe32(CBFS_TYPE_DELETED); cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL); return 0; } @@ -1340,7 +1340,7 @@ static int cbfs_print_stage_info(struct cbfs_file *entry, FILE* fp) struct cbfs_file_attr_stageheader *stage = NULL; for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); attr != NULL; attr = cbfs_file_next_attr(entry, attr)) { - if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) { + if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) { stage = (struct cbfs_file_attr_stageheader *)attr; break; } @@ -1354,9 +1354,9 @@ static int cbfs_print_stage_info(struct cbfs_file *entry, FILE* fp) fprintf(fp, " entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " "memlen: %d\n", - ntohll(stage->loadaddr) + ntohl(stage->entry_offset), - ntohll(stage->loadaddr), - ntohl(stage->memlen)); + be64toh(stage->loadaddr) + be32toh(stage->entry_offset), + be64toh(stage->loadaddr), + be32toh(stage->memlen)); return 0; } @@ -1434,16 +1434,16 @@ int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n", *name ? name : "(empty)", cbfs_get_entry_addr(image, entry), - get_cbfs_entry_type_name(ntohl(entry->type)), - ntohl(entry->len), + get_cbfs_entry_type_name(be32toh(entry->type)), + be32toh(entry->len), compression_name ); else fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n", *name ? name : "(empty)", cbfs_get_entry_addr(image, entry), - get_cbfs_entry_type_name(ntohl(entry->type)), - ntohl(entry->len), + get_cbfs_entry_type_name(be32toh(entry->type)), + be32toh(entry->len), compression_name, decompressed_size ); @@ -1461,7 +1461,7 @@ int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, } char *hash_str = bintohex(attr->hash.raw, hash_len); int valid = vb2_hash_verify(CBFS_SUBHEADER(entry), - ntohl(entry->len), &attr->hash) == VB2_SUCCESS; + be32toh(entry->len), &attr->hash) == VB2_SUCCESS; const char *valid_str = valid ? "valid" : "invalid"; fprintf(fp, " hash %s:%s %s\n", @@ -1471,12 +1471,12 @@ int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, } DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", - cbfs_get_entry_addr(image, entry), ntohl(entry->offset), - cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), - ntohl(entry->len)); + cbfs_get_entry_addr(image, entry), be32toh(entry->offset), + cbfs_get_entry_addr(image, entry) + be32toh(entry->offset), + be32toh(entry->len)); /* note the components of the subheader may be in host order ... */ - switch (ntohl(entry->type)) { + switch (be32toh(entry->type)) { case CBFS_TYPE_STAGE: cbfs_print_stage_info(entry, fp); break; @@ -1521,9 +1521,9 @@ static int cbfs_print_parseable_entry_info(struct cbfs_image *image, name = entry->filename; if (*name == '\0') name = "(empty)"; - type = get_cbfs_entry_type_name(ntohl(entry->type)), - metadata_size = ntohl(entry->offset); - data_size = ntohl(entry->len); + type = get_cbfs_entry_type_name(be32toh(entry->type)), + metadata_size = be32toh(entry->offset); + data_size = be32toh(entry->len); offset = cbfs_get_entry_addr(image, entry); fprintf(fp, "%s%s", name, sep); @@ -1549,7 +1549,7 @@ static int cbfs_print_parseable_entry_info(struct cbfs_image *image, continue; char *hash_str = bintohex(attr->hash.raw, hash_len); int valid = vb2_hash_verify(CBFS_SUBHEADER(entry), - ntohl(entry->len), &attr->hash) == VB2_SUCCESS; + be32toh(entry->len), &attr->hash) == VB2_SUCCESS; fprintf(fp, "%shash:%s:%s:%s", sep, vb2_get_hash_algorithm_name(attr->hash.algo), hash_str, valid ? "valid" : "invalid"); @@ -1600,8 +1600,8 @@ int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, /* Loop until non-empty entry is found, starting from the current entry. After the loop, next_addr points to the next non-empty entry. */ next = entry; - while (ntohl(next->type) == CBFS_TYPE_DELETED || - ntohl(next->type) == CBFS_TYPE_NULL) { + while (be32toh(next->type) == CBFS_TYPE_DELETED || + be32toh(next->type) == CBFS_TYPE_NULL) { next = cbfs_find_next_entry(image, next); if (!next) break; @@ -1644,10 +1644,10 @@ int cbfs_legacy_walk(struct cbfs_image *image, cbfs_entry_callback callback, static int cbfs_header_valid(struct cbfs_header *header) { - if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && - ((ntohl(header->version) == CBFS_HEADER_VERSION1) || - (ntohl(header->version) == CBFS_HEADER_VERSION2)) && - (ntohl(header->offset) < ntohl(header->romsize))) + if ((be32toh(header->magic) == CBFS_HEADER_MAGIC) && + ((be32toh(header->version) == CBFS_HEADER_VERSION1) || + (be32toh(header->version) == CBFS_HEADER_VERSION2)) && + (be32toh(header->offset) < be32toh(header->romsize))) return 1; return 0; } @@ -1718,7 +1718,7 @@ struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, uint32_t addr = cbfs_get_entry_addr(image, entry); int align = image->has_header ? image->header.align : CBFS_ALIGNMENT; assert(entry && cbfs_is_valid_entry(image, entry)); - addr += ntohl(entry->offset) + ntohl(entry->len); + addr += be32toh(entry->offset) + be32toh(entry->len); addr = align_up(addr, align); return (struct cbfs_file *)(image->buffer.data + addr); } @@ -1760,11 +1760,11 @@ struct cbfs_file *cbfs_create_file_header(int type, struct cbfs_file *entry = malloc(CBFS_METADATA_MAX_SIZE); memset(entry, CBFS_CONTENT_DEFAULT_VALUE, CBFS_METADATA_MAX_SIZE); memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); - entry->type = htonl(type); - entry->len = htonl(len); + entry->type = htobe32(type); + entry->len = htobe32(len); entry->attributes_offset = 0; - entry->offset = htonl(cbfs_calculate_file_header_size(name)); - memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry)); + entry->offset = htobe32(cbfs_calculate_file_header_size(name)); + memset(entry->filename, 0, be32toh(entry->offset) - sizeof(*entry)); strcpy(entry->filename, name); return entry; } @@ -1773,7 +1773,7 @@ int cbfs_create_empty_entry(struct cbfs_file *entry, int type, size_t len, const char *name) { struct cbfs_file *tmp = cbfs_create_file_header(type, len, name); - memcpy(entry, tmp, ntohl(tmp->offset)); + memcpy(entry, tmp, be32toh(tmp->offset)); free(tmp); memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); return 0; @@ -1783,17 +1783,17 @@ struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file) { /* attributes_offset should be 0 when there is no attribute, but all * values that point into the cbfs_file header are invalid, too. */ - if (ntohl(file->attributes_offset) <= sizeof(*file)) + if (be32toh(file->attributes_offset) <= sizeof(*file)) return NULL; /* There needs to be enough space for the file header and one * attribute header for this to make sense. */ - if (ntohl(file->offset) <= + if (be32toh(file->offset) <= sizeof(*file) + sizeof(struct cbfs_file_attribute)) return NULL; return (struct cbfs_file_attribute *) - (((uint8_t *)file) + ntohl(file->attributes_offset)); + (((uint8_t *)file) + be32toh(file->attributes_offset)); } struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, @@ -1804,17 +1804,17 @@ struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, return NULL; /* Is there enough space for another attribute? */ - if ((uint8_t *)attr + ntohl(attr->len) + + if ((uint8_t *)attr + be32toh(attr->len) + sizeof(struct cbfs_file_attribute) > - (uint8_t *)file + ntohl(file->offset)) + (uint8_t *)file + be32toh(file->offset)) return NULL; struct cbfs_file_attribute *next = (struct cbfs_file_attribute *) - (((uint8_t *)attr) + ntohl(attr->len)); + (((uint8_t *)attr) + be32toh(attr->len)); /* If any, "unused" attributes must come last. */ - if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED) + if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED) return NULL; - if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) + if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) return NULL; return next; @@ -1831,7 +1831,7 @@ struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header, attr = next; next = cbfs_file_next_attr(header, attr); } while (next != NULL); - uint32_t header_size = ntohl(header->offset) + size; + uint32_t header_size = be32toh(header->offset) + size; if (header_size > CBFS_METADATA_MAX_SIZE) { DEBUG("exceeding allocated space for cbfs_file headers"); return NULL; @@ -1847,20 +1847,20 @@ struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header, header->attributes_offset = header->offset; attr = (struct cbfs_file_attribute *) (((uint8_t *)header) + - ntohl(header->attributes_offset)); + be32toh(header->attributes_offset)); } else { attr = (struct cbfs_file_attribute *) (((uint8_t *)attr) + - ntohl(attr->len)); + be32toh(attr->len)); } - header->offset = htonl(header_size); + header->offset = htobe32(header_size); /* Attributes are expected to be small (much smaller than a flash page) and not really meant to be overwritten in-place. To avoid surprising values in reserved fields of attribute structures, initialize them to 0, not 0xff. */ memset(attr, 0, size); - attr->tag = htonl(tag); - attr->len = htonl(size); + attr->tag = htobe32(tag); + attr->len = htobe32(size); return attr; } @@ -1965,7 +1965,7 @@ int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size, entry && cbfs_is_valid_entry(image, entry); entry = cbfs_find_next_entry(image, entry)) { - uint32_t type = ntohl(entry->type); + uint32_t type = be32toh(entry->type); if (type != CBFS_TYPE_NULL) continue; diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 608cd32a3d..b76534057a 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -169,11 +169,11 @@ static struct mh_cache *get_mh_cache(void) if (cbfs_image_from_buffer(&cbfs, &buffer, param.headeroffset)) goto no_metadata_hash; bootblock = cbfs_get_entry(&cbfs, "bootblock"); - if (!bootblock || ntohl(bootblock->type) != CBFS_TYPE_BOOTBLOCK) + if (!bootblock || be32toh(bootblock->type) != CBFS_TYPE_BOOTBLOCK) goto no_metadata_hash; - offset = (void *)bootblock + ntohl(bootblock->offset) - + offset = (void *)bootblock + be32toh(bootblock->offset) - buffer_get(&cbfs.buffer); - size = ntohl(bootblock->len); + size = be32toh(bootblock->len); } /* Find and validate the metadata hash anchor inside the bootblock and @@ -664,7 +664,7 @@ static int update_master_header_loc_topswap(struct cbfs_image *image, * Check if the existing topswap boundary matches with * the one provided. */ - if (param.topswap_size != ntohl(entry->len)/2) { + if (param.topswap_size != be32toh(entry->len)/2) { ERROR("Top swap boundary does not match\n"); return 1; } @@ -701,16 +701,16 @@ static int cbfs_add_master_header(void) return 1; struct cbfs_header *h = (struct cbfs_header *)buffer.data; - h->magic = htonl(CBFS_HEADER_MAGIC); - h->version = htonl(CBFS_HEADER_VERSION); + h->magic = htobe32(CBFS_HEADER_MAGIC); + h->version = htobe32(CBFS_HEADER_VERSION); /* The 4 bytes are left out for two reasons: * 1. the cbfs master header pointer resides there * 2. some cbfs implementations assume that an image that resides * below 4GB has a bootblock and get confused when the end of the * image is at 4GB == 0. */ - h->bootblocksize = htonl(4); - h->align = htonl(CBFS_ALIGNMENT); + h->bootblocksize = htobe32(4); + h->align = htobe32(CBFS_ALIGNMENT); /* The offset and romsize fields within the master header are absolute * values within the boot media. As such, romsize needs to relfect * the end 'offset' for a CBFS. To achieve that the current buffer @@ -720,9 +720,9 @@ static int cbfs_add_master_header(void) offset = buffer_get(param.image_region) - buffer_get_original_backing(param.image_region); size = buffer_size(param.image_region); - h->romsize = htonl(size + offset); - h->offset = htonl(offset); - h->architecture = htonl(CBFS_ARCHITECTURE_UNKNOWN); + h->romsize = htobe32(size + offset); + h->offset = htobe32(offset); + h->architecture = htobe32(CBFS_ARCHITECTURE_UNKNOWN); /* Never add a hash attribute to the master header. */ header = cbfs_create_file_header(CBFS_TYPE_CBFSHEADER, @@ -925,7 +925,7 @@ static int cbfs_add_component(const char *filename, sizeof(struct cbfs_file_attr_position)); if (attrs == NULL) goto error; - attrs->position = htonl(offset); + attrs->position = htobe32(offset); } /* Add alignment attribute if used */ if (param.alignment) { @@ -936,7 +936,7 @@ static int cbfs_add_component(const char *filename, sizeof(struct cbfs_file_attr_align)); if (attrs == NULL) goto error; - attrs->alignment = htonl(param.alignment); + attrs->alignment = htobe32(param.alignment); } } @@ -1024,15 +1024,15 @@ static int cbfstool_convert_raw(struct buffer *buffer, free(compressed); return -1; } - attrs->compression = htonl(param.compression); - attrs->decompressed_size = htonl(decompressed_size); + attrs->compression = htobe32(param.compression); + attrs->decompressed_size = htobe32(decompressed_size); free(buffer->data); buffer->data = compressed; buffer->size = compressed_size; out: - header->len = htonl(buffer->size); + header->len = htobe32(buffer->size); return 0; } @@ -1152,7 +1152,7 @@ static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset, /* Special care must be taken for LZ4-compressed stages that the BSS is large enough to provide scratch space for in-place decompression. */ if (!param.precompression && param.compression == CBFS_COMPRESS_LZ4) { - size_t memlen = ntohl(stageheader->memlen); + size_t memlen = be32toh(stageheader->memlen); size_t compressed_size = buffer_size(&output); uint8_t *compare_buffer = malloc(memlen); uint8_t *start = compare_buffer + memlen - compressed_size; @@ -1196,7 +1196,7 @@ static int cbfstool_convert_mkpayload(struct buffer *buffer, if (ret != 0) { ret = parse_fit_to_payload(buffer, &output, param.compression); if (ret == 0) - header->type = htonl(CBFS_TYPE_FIT); + header->type = htobe32(CBFS_TYPE_FIT); } /* If it's not an FIT, see if it's a UEFI FV */ @@ -1218,7 +1218,7 @@ static int cbfstool_convert_mkpayload(struct buffer *buffer, buffer_delete(buffer); // Direct assign, no dupe. memcpy(buffer, &output, sizeof(*buffer)); - header->len = htonl(output.size); + header->len = htobe32(output.size); return 0; } @@ -1235,7 +1235,7 @@ static int cbfstool_convert_mkflatpayload(struct buffer *buffer, buffer_delete(buffer); // Direct assign, no dupe. memcpy(buffer, &output, sizeof(*buffer)); - header->len = htonl(output.size); + header->len = htobe32(output.size); return 0; } diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c index 5889e2a937..ea22bd6460 100644 --- a/util/cbfstool/common.c +++ b/util/cbfstool/common.c @@ -15,17 +15,6 @@ /* Utilities */ int verbose = 0; -/* Small, OS/libc independent runtime check for endianness */ -int is_big_endian(void) -{ - static const uint32_t inttest = 0x12345678; - const uint8_t inttest_lsb = *(const uint8_t *)&inttest; - if (inttest_lsb == 0x12) { - return 1; - } - return 0; -} - static off_t get_file_size(FILE *f) { off_t fsize; diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h index b23c8d21b0..d39f8fbfbd 100644 --- a/util/cbfstool/common.h +++ b/util/cbfstool/common.h @@ -10,11 +10,10 @@ #include <assert.h> #include <commonlib/bsd/cbfs_serialized.h> +#include <commonlib/bsd/sysincludes.h> #include <commonlib/helpers.h> #include <console/console.h> -#include "swab.h" - /* * There are two address spaces that this tool deals with - SPI flash address space and host * address space. This macros checks if the address is greater than 2GiB under the assumption diff --git a/util/cbfstool/ifittool.c b/util/cbfstool/ifittool.c index 264f538a22..c2a5221f0c 100644 --- a/util/cbfstool/ifittool.c +++ b/util/cbfstool/ifittool.c @@ -356,11 +356,11 @@ int main(int argc, char *argv[]) return 1; } - len = ntohl(cbfs_file->len); + len = be32toh(cbfs_file->len); offset = offset_to_ptr(convert_to_from_top_aligned, &image.buffer, cbfs_get_entry_addr(&image, cbfs_file) + - ntohl(cbfs_file->offset)); + be32toh(cbfs_file->offset)); if (fit_add_entry(fit, offset, len, fit_type, @@ -384,7 +384,7 @@ int main(int argc, char *argv[]) fit_address = offset_to_ptr(convert_to_from_top_aligned, &image.buffer, cbfs_get_entry_addr(&image, cbfs_file) - + ntohl(cbfs_file->offset)); + + be32toh(cbfs_file->offset)); if (set_fit_pointer(&bootblock, fit_address, convert_to_from_top_aligned, diff --git a/util/cbfstool/swab.h b/util/cbfstool/swab.h deleted file mode 100644 index a45a7673d9..0000000000 --- a/util/cbfstool/swab.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef _SWAB_H -#define _SWAB_H - -/* - * linux/byteorder/swab.h - * Byte-swapping, independently from CPU endianness - * swabXX[ps]?(foo) - * - * Francois-Rene Rideau <fare@tunes.org> 19971205 - * separated swab functions from cpu_to_XX, - * to clean up support for bizarre-endian architectures. - * - * See asm-i386/byteorder.h and suches for examples of how to provide - * architecture-dependent optimized versions - * - */ - -#if !defined(__APPLE__) && !defined(__NetBSD__) -#define ntohl(x) (is_big_endian() ? (uint32_t)(x) : swab32(x)) -#define htonl(x) (is_big_endian() ? (uint32_t)(x) : swab32(x)) -#else -#include <arpa/inet.h> -#endif -#define ntohll(x) (is_big_endian() ? (uint64_t)(x) : swab64(x)) -#define htonll(x) (is_big_endian() ? (uint64_t)(x) : swab64(x)) - -/* casts are necessary for constants, because we never know how for sure - * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way. - */ -#define swab16(x) \ - ((unsigned short)( \ - (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \ - (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) )) - -#define swab32(x) \ - ((unsigned int)( \ - (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \ - (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \ - (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \ - (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) )) - -#define swab64(x) \ - ((uint64_t)( \ - (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ - (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ - (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ - (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ - (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ - (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ - (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ - (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) )) - -/* common.c */ -int is_big_endian(void); - -#endif /* _SWAB_H */ |