util/cbfstool: Replace swab.h with commonlib/bsd/sysincludes.h

Instead of maintaining another set of byteswapping functions in
cbfstool, this change removes swab.h and replaces it with
bsd/sysincludes.h from commonlib. Callers have been updated to use
be32toh/be64toh/htobe32/htobe64 instead of ntohl/ntohll/htonl/htonll
respectively.

Change-Id: I54195865ab4042fcf83609fcf67ef8f33994d68e
Signed-off-by: Alex James <theracermaster@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/60233
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Alex James 2021-12-19 16:41:59 -06:00 committed by Julius Werner
parent f6e74c45c0
commit 02001a38be
8 changed files with 111 additions and 179 deletions

View File

@ -80,9 +80,9 @@ static int fill_cbfs_stageheader(struct cbfs_file_attr_stageheader *stageheader,
return -1; return -1;
} }
stageheader->loadaddr = htonll(loadaddr); stageheader->loadaddr = htobe64(loadaddr);
stageheader->memlen = htonl(memsize); stageheader->memlen = htobe32(memsize);
stageheader->entry_offset = htonl(entry - loadaddr); stageheader->entry_offset = htobe32(entry - loadaddr);
return 0; return 0;
} }

View File

@ -54,7 +54,7 @@ static struct typedesc_t filetypes[] unused = {
{0, NULL} {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) static inline size_t cbfs_file_attr_hash_size(enum vb2_hash_algorithm algo)
{ {

View File

@ -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_is_valid_entry(image, entry);
entry = cbfs_find_next_entry(image, entry)) { entry = cbfs_find_next_entry(image, entry)) {
/* Is the header guarded by a CBFS file entry? Then exit */ /* 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; return 0;
} }
last = entry; last = entry;
@ -95,7 +95,7 @@ static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
(char *)entry > (char *)hdr_loc) { (char *)entry > (char *)hdr_loc) {
WARN("CBFS image was created with old cbfstool with size bug. " WARN("CBFS image was created with old cbfstool with size bug. "
"Fixing size in last entry...\n"); "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", DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
cbfs_get_entry_addr(image, entry), cbfs_get_entry_addr(image, entry),
cbfs_get_entry_addr(image, 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; unsigned int compression = CBFS_COMPRESS_NONE;
if (decompressed_size) if (decompressed_size)
*decompressed_size = ntohl(entry->len); *decompressed_size = be32toh(entry->len);
for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
attr != NULL; attr != NULL;
attr = cbfs_file_next_attr(entry, attr)) { 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 *ac =
(struct cbfs_file_attr_compression *)attr; (struct cbfs_file_attr_compression *)attr;
compression = ntohl(ac->compression); compression = be32toh(ac->compression);
if (decompressed_size) if (decompressed_size)
*decompressed_size = *decompressed_size =
ntohl(ac->decompressed_size); be32toh(ac->decompressed_size);
} }
} }
return compression; return compression;
@ -165,11 +165,11 @@ static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
attr = cbfs_file_first_attr(entry); attr = cbfs_file_first_attr(entry);
if (attr == NULL) if (attr == NULL)
return 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; return (struct cbfs_file_attr_hash *)attr;
} }
while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) { 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 (struct cbfs_file_attr_hash *)attr;
}; };
return NULL; 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)) { src_entry = cbfs_find_next_entry(image, src_entry)) {
size_t entry_size; size_t entry_size;
if ((src_entry->type == htonl(CBFS_TYPE_NULL)) || if ((src_entry->type == htobe32(CBFS_TYPE_NULL)) ||
(src_entry->type == htonl(CBFS_TYPE_CBFSHEADER)) || (src_entry->type == htobe32(CBFS_TYPE_CBFSHEADER)) ||
(src_entry->type == htonl(CBFS_TYPE_DELETED))) (src_entry->type == htobe32(CBFS_TYPE_DELETED)))
continue; 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); memcpy(dst_entry, src_entry, entry_size);
dst_entry = (struct cbfs_file *)( dst_entry = (struct cbfs_file *)(
(uintptr_t)dst_entry + align_up(entry_size, align)); (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. * maximum size.
*/ */
if ((strlen(trailer->filename) != 0) && if ((strlen(trailer->filename) != 0) &&
(trailer->type != htonl(CBFS_TYPE_NULL)) && (trailer->type != htobe32(CBFS_TYPE_NULL)) &&
(trailer->type != htonl(CBFS_TYPE_DELETED))) { (trailer->type != htobe32(CBFS_TYPE_DELETED))) {
/* nothing to truncate. Return de-facto CBFS size in case it /* nothing to truncate. Return de-facto CBFS size in case it
* was already truncated. */ * was already truncated. */
*size = (uint8_t *)entry - (uint8_t *)buffer_get(region); *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) 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) 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) 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 cur_size;
size_t empty_metadata_size; size_t empty_metadata_size;
size_t spill_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. */ /* Current entry is empty. Kepp track of it. */
if ((type == htonl(CBFS_TYPE_NULL)) || if ((type == htobe32(CBFS_TYPE_NULL)) ||
(type == htonl(CBFS_TYPE_DELETED))) { (type == htobe32(CBFS_TYPE_DELETED))) {
prev = cur; prev = cur;
continue; continue;
} }
@ -631,7 +631,7 @@ static int cbfs_add_entry_at(struct cbfs_image *image,
uint32_t len, header_offset; uint32_t len, header_offset;
uint32_t align = image->has_header ? image->header.align : uint32_t align = image->has_header ? image->header.align :
CBFS_ALIGNMENT; CBFS_ALIGNMENT;
uint32_t header_size = ntohl(header->offset); uint32_t header_size = be32toh(header->offset);
header_offset = content_offset - header_size; header_offset = content_offset - header_size;
if (header_offset % align) 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 * to file data. Move attributes forward so the end of the
* attribute list still matches the end of the metadata. * attribute list still matches the end of the metadata.
*/ */
uint32_t offset = ntohl(entry->offset); uint32_t offset = be32toh(entry->offset);
uint32_t attrs = ntohl(entry->attributes_offset); uint32_t attrs = be32toh(entry->attributes_offset);
DEBUG("|..|header|content|... <use offset to create entry>\n"); DEBUG("|..|header|content|... <use offset to create entry>\n");
DEBUG("before: attr_offset=0x%x, offset=0x%x\n", attrs, offset); DEBUG("before: attr_offset=0x%x, offset=0x%x\n", attrs, offset);
if (attrs == 0) { if (attrs == 0) {
@ -671,10 +671,10 @@ static int cbfs_add_entry_at(struct cbfs_image *image,
memmove(p + len, p, offset - attrs); memmove(p + len, p, offset - attrs);
memset(p, 0, len); memset(p, 0, len);
attrs += len; attrs += len;
entry->attributes_offset = htonl(attrs); entry->attributes_offset = htobe32(attrs);
} }
offset += len; offset += len;
entry->offset = htonl(offset); entry->offset = htobe32(offset);
DEBUG("after: attr_offset=0x%x, offset=0x%x\n", attrs, 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)); image->buffer.data));
assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data == assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
(ptrdiff_t)content_offset); (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); if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
// Align the length to a multiple of len_align // Align the length to a multiple of len_align
if (len_align && if (len_align &&
((ntohl(entry->offset) + ntohl(entry->len)) % len_align)) { ((be32toh(entry->offset) + be32toh(entry->len)) % len_align)) {
size_t off = (ntohl(entry->offset) + ntohl(entry->len)) % len_align; size_t off = (be32toh(entry->offset) + be32toh(entry->len)) % len_align;
entry->len = htonl(ntohl(entry->len) + len_align - off); entry->len = htobe32(be32toh(entry->len) + len_align - off);
} }
// Process buffer AFTER entry. // Process buffer AFTER entry.
@ -738,7 +738,7 @@ int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
uint32_t addr, addr_next; uint32_t addr, addr_next;
struct cbfs_file *entry, *next; struct cbfs_file *entry, *next;
uint32_t need_size; uint32_t need_size;
uint32_t header_size = ntohl(header->offset); uint32_t header_size = be32toh(header->offset);
need_size = header_size + buffer->size; need_size = header_size + buffer->size;
DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n", 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_is_valid_entry(image, entry);
entry = cbfs_find_next_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) if (entry_type != CBFS_TYPE_NULL)
continue; continue;
@ -975,7 +975,7 @@ static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch,
struct cbfs_file_attr_stageheader *stage = NULL; struct cbfs_file_attr_stageheader *stage = NULL;
for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
attr != NULL; attr = cbfs_file_next_attr(entry, attr)) { 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; stage = (struct cbfs_file_attr_stageheader *)attr;
break; 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. */ /* 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); ew = elf_writer_init(&ehdr);
if (ew == NULL) { if (ew == NULL) {
@ -1011,9 +1011,9 @@ static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch,
memset(&shdr, 0, sizeof(shdr)); memset(&shdr, 0, sizeof(shdr));
shdr.sh_type = SHT_PROGBITS; shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; 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); 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")) { if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
ERROR("Unable to add ELF section: .program\n"); 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)); memset(&shdr, 0, sizeof(shdr));
shdr.sh_type = SHT_NOBITS; shdr.sh_type = SHT_NOBITS;
shdr.sh_flags = SHF_WRITE | SHF_ALLOC; 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; shdr.sh_size = empty_sz;
if (elf_writer_add_section(ew, &shdr, &b, ".empty")) { if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
ERROR("Unable to add ELF section: .empty\n"); 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; return -1;
} }
unsigned int compressed_size = ntohl(entry->len); unsigned int compressed_size = be32toh(entry->len);
unsigned int decompressed_size = 0; unsigned int decompressed_size = 0;
unsigned int compression = cbfs_file_get_compression_info(entry, unsigned int compression = cbfs_file_get_compression_info(entry,
&decompressed_size); &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", LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n",
entry_name, cbfs_get_entry_addr(image, entry), 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); decompressed_size);
buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0); 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) { if (do_processing) {
int (*make_elf)(struct buffer *, uint32_t, int (*make_elf)(struct buffer *, uint32_t,
struct cbfs_file *) = NULL; struct cbfs_file *) = NULL;
switch (ntohl(entry->type)) { switch (be32toh(entry->type)) {
case CBFS_TYPE_STAGE: case CBFS_TYPE_STAGE:
make_elf = cbfs_stage_make_elf; make_elf = cbfs_stage_make_elf;
break; 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", DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
entry->filename, cbfs_get_entry_addr(image, entry)); 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); cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
return 0; 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; struct cbfs_file_attr_stageheader *stage = NULL;
for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
attr != NULL; attr = cbfs_file_next_attr(entry, attr)) { 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; stage = (struct cbfs_file_attr_stageheader *)attr;
break; break;
} }
@ -1354,9 +1354,9 @@ static int cbfs_print_stage_info(struct cbfs_file *entry, FILE* fp)
fprintf(fp, fprintf(fp,
" entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " " entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
"memlen: %d\n", "memlen: %d\n",
ntohll(stage->loadaddr) + ntohl(stage->entry_offset), be64toh(stage->loadaddr) + be32toh(stage->entry_offset),
ntohll(stage->loadaddr), be64toh(stage->loadaddr),
ntohl(stage->memlen)); be32toh(stage->memlen));
return 0; 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", fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n",
*name ? name : "(empty)", *name ? name : "(empty)",
cbfs_get_entry_addr(image, entry), cbfs_get_entry_addr(image, entry),
get_cbfs_entry_type_name(ntohl(entry->type)), get_cbfs_entry_type_name(be32toh(entry->type)),
ntohl(entry->len), be32toh(entry->len),
compression_name compression_name
); );
else else
fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n", fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n",
*name ? name : "(empty)", *name ? name : "(empty)",
cbfs_get_entry_addr(image, entry), cbfs_get_entry_addr(image, entry),
get_cbfs_entry_type_name(ntohl(entry->type)), get_cbfs_entry_type_name(be32toh(entry->type)),
ntohl(entry->len), be32toh(entry->len),
compression_name, compression_name,
decompressed_size 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); char *hash_str = bintohex(attr->hash.raw, hash_len);
int valid = vb2_hash_verify(CBFS_SUBHEADER(entry), 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"; const char *valid_str = valid ? "valid" : "invalid";
fprintf(fp, " hash %s:%s %s\n", 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", 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), be32toh(entry->offset),
cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), cbfs_get_entry_addr(image, entry) + be32toh(entry->offset),
ntohl(entry->len)); be32toh(entry->len));
/* note the components of the subheader may be in host order ... */ /* note the components of the subheader may be in host order ... */
switch (ntohl(entry->type)) { switch (be32toh(entry->type)) {
case CBFS_TYPE_STAGE: case CBFS_TYPE_STAGE:
cbfs_print_stage_info(entry, fp); cbfs_print_stage_info(entry, fp);
break; break;
@ -1521,9 +1521,9 @@ static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
name = entry->filename; name = entry->filename;
if (*name == '\0') if (*name == '\0')
name = "(empty)"; name = "(empty)";
type = get_cbfs_entry_type_name(ntohl(entry->type)), type = get_cbfs_entry_type_name(be32toh(entry->type)),
metadata_size = ntohl(entry->offset); metadata_size = be32toh(entry->offset);
data_size = ntohl(entry->len); data_size = be32toh(entry->len);
offset = cbfs_get_entry_addr(image, entry); offset = cbfs_get_entry_addr(image, entry);
fprintf(fp, "%s%s", name, sep); fprintf(fp, "%s%s", name, sep);
@ -1549,7 +1549,7 @@ static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
continue; continue;
char *hash_str = bintohex(attr->hash.raw, hash_len); char *hash_str = bintohex(attr->hash.raw, hash_len);
int valid = vb2_hash_verify(CBFS_SUBHEADER(entry), 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, fprintf(fp, "%shash:%s:%s:%s", sep,
vb2_get_hash_algorithm_name(attr->hash.algo), vb2_get_hash_algorithm_name(attr->hash.algo),
hash_str, valid ? "valid" : "invalid"); 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. /* Loop until non-empty entry is found, starting from the current entry.
After the loop, next_addr points to the next non-empty entry. */ After the loop, next_addr points to the next non-empty entry. */
next = entry; next = entry;
while (ntohl(next->type) == CBFS_TYPE_DELETED || while (be32toh(next->type) == CBFS_TYPE_DELETED ||
ntohl(next->type) == CBFS_TYPE_NULL) { be32toh(next->type) == CBFS_TYPE_NULL) {
next = cbfs_find_next_entry(image, next); next = cbfs_find_next_entry(image, next);
if (!next) if (!next)
break; 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) static int cbfs_header_valid(struct cbfs_header *header)
{ {
if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && if ((be32toh(header->magic) == CBFS_HEADER_MAGIC) &&
((ntohl(header->version) == CBFS_HEADER_VERSION1) || ((be32toh(header->version) == CBFS_HEADER_VERSION1) ||
(ntohl(header->version) == CBFS_HEADER_VERSION2)) && (be32toh(header->version) == CBFS_HEADER_VERSION2)) &&
(ntohl(header->offset) < ntohl(header->romsize))) (be32toh(header->offset) < be32toh(header->romsize)))
return 1; return 1;
return 0; 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); uint32_t addr = cbfs_get_entry_addr(image, entry);
int align = image->has_header ? image->header.align : CBFS_ALIGNMENT; int align = image->has_header ? image->header.align : CBFS_ALIGNMENT;
assert(entry && cbfs_is_valid_entry(image, entry)); 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); addr = align_up(addr, align);
return (struct cbfs_file *)(image->buffer.data + addr); 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); struct cbfs_file *entry = malloc(CBFS_METADATA_MAX_SIZE);
memset(entry, CBFS_CONTENT_DEFAULT_VALUE, CBFS_METADATA_MAX_SIZE); memset(entry, CBFS_CONTENT_DEFAULT_VALUE, CBFS_METADATA_MAX_SIZE);
memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
entry->type = htonl(type); entry->type = htobe32(type);
entry->len = htonl(len); entry->len = htobe32(len);
entry->attributes_offset = 0; entry->attributes_offset = 0;
entry->offset = htonl(cbfs_calculate_file_header_size(name)); entry->offset = htobe32(cbfs_calculate_file_header_size(name));
memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry)); memset(entry->filename, 0, be32toh(entry->offset) - sizeof(*entry));
strcpy(entry->filename, name); strcpy(entry->filename, name);
return entry; return entry;
} }
@ -1773,7 +1773,7 @@ int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
size_t len, const char *name) size_t len, const char *name)
{ {
struct cbfs_file *tmp = cbfs_create_file_header(type, len, 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); free(tmp);
memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
return 0; 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 /* attributes_offset should be 0 when there is no attribute, but all
* values that point into the cbfs_file header are invalid, too. */ * 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; return NULL;
/* There needs to be enough space for the file header and one /* There needs to be enough space for the file header and one
* attribute header for this to make sense. */ * attribute header for this to make sense. */
if (ntohl(file->offset) <= if (be32toh(file->offset) <=
sizeof(*file) + sizeof(struct cbfs_file_attribute)) sizeof(*file) + sizeof(struct cbfs_file_attribute))
return NULL; return NULL;
return (struct cbfs_file_attribute *) 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, 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; return NULL;
/* Is there enough space for another attribute? */ /* 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) > sizeof(struct cbfs_file_attribute) >
(uint8_t *)file + ntohl(file->offset)) (uint8_t *)file + be32toh(file->offset))
return NULL; return NULL;
struct cbfs_file_attribute *next = (struct cbfs_file_attribute *) 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 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; return NULL;
if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
return NULL; return NULL;
return next; return next;
@ -1831,7 +1831,7 @@ struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
attr = next; attr = next;
next = cbfs_file_next_attr(header, attr); next = cbfs_file_next_attr(header, attr);
} while (next != NULL); } 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) { if (header_size > CBFS_METADATA_MAX_SIZE) {
DEBUG("exceeding allocated space for cbfs_file headers"); DEBUG("exceeding allocated space for cbfs_file headers");
return NULL; return NULL;
@ -1847,20 +1847,20 @@ struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
header->attributes_offset = header->offset; header->attributes_offset = header->offset;
attr = (struct cbfs_file_attribute *) attr = (struct cbfs_file_attribute *)
(((uint8_t *)header) + (((uint8_t *)header) +
ntohl(header->attributes_offset)); be32toh(header->attributes_offset));
} else { } else {
attr = (struct cbfs_file_attribute *) attr = (struct cbfs_file_attribute *)
(((uint8_t *)attr) + (((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) /* Attributes are expected to be small (much smaller than a flash page)
and not really meant to be overwritten in-place. To avoid surprising and not really meant to be overwritten in-place. To avoid surprising
values in reserved fields of attribute structures, initialize them to values in reserved fields of attribute structures, initialize them to
0, not 0xff. */ 0, not 0xff. */
memset(attr, 0, size); memset(attr, 0, size);
attr->tag = htonl(tag); attr->tag = htobe32(tag);
attr->len = htonl(size); attr->len = htobe32(size);
return attr; 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_is_valid_entry(image, entry);
entry = cbfs_find_next_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) if (type != CBFS_TYPE_NULL)
continue; continue;

View File

@ -169,11 +169,11 @@ static struct mh_cache *get_mh_cache(void)
if (cbfs_image_from_buffer(&cbfs, &buffer, param.headeroffset)) if (cbfs_image_from_buffer(&cbfs, &buffer, param.headeroffset))
goto no_metadata_hash; goto no_metadata_hash;
bootblock = cbfs_get_entry(&cbfs, "bootblock"); 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; goto no_metadata_hash;
offset = (void *)bootblock + ntohl(bootblock->offset) - offset = (void *)bootblock + be32toh(bootblock->offset) -
buffer_get(&cbfs.buffer); buffer_get(&cbfs.buffer);
size = ntohl(bootblock->len); size = be32toh(bootblock->len);
} }
/* Find and validate the metadata hash anchor inside the bootblock and /* 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 * Check if the existing topswap boundary matches with
* the one provided. * 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"); ERROR("Top swap boundary does not match\n");
return 1; return 1;
} }
@ -701,16 +701,16 @@ static int cbfs_add_master_header(void)
return 1; return 1;
struct cbfs_header *h = (struct cbfs_header *)buffer.data; struct cbfs_header *h = (struct cbfs_header *)buffer.data;
h->magic = htonl(CBFS_HEADER_MAGIC); h->magic = htobe32(CBFS_HEADER_MAGIC);
h->version = htonl(CBFS_HEADER_VERSION); h->version = htobe32(CBFS_HEADER_VERSION);
/* The 4 bytes are left out for two reasons: /* The 4 bytes are left out for two reasons:
* 1. the cbfs master header pointer resides there * 1. the cbfs master header pointer resides there
* 2. some cbfs implementations assume that an image that resides * 2. some cbfs implementations assume that an image that resides
* below 4GB has a bootblock and get confused when the end of the * below 4GB has a bootblock and get confused when the end of the
* image is at 4GB == 0. * image is at 4GB == 0.
*/ */
h->bootblocksize = htonl(4); h->bootblocksize = htobe32(4);
h->align = htonl(CBFS_ALIGNMENT); h->align = htobe32(CBFS_ALIGNMENT);
/* The offset and romsize fields within the master header are absolute /* The offset and romsize fields within the master header are absolute
* values within the boot media. As such, romsize needs to relfect * values within the boot media. As such, romsize needs to relfect
* the end 'offset' for a CBFS. To achieve that the current buffer * 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) - offset = buffer_get(param.image_region) -
buffer_get_original_backing(param.image_region); buffer_get_original_backing(param.image_region);
size = buffer_size(param.image_region); size = buffer_size(param.image_region);
h->romsize = htonl(size + offset); h->romsize = htobe32(size + offset);
h->offset = htonl(offset); h->offset = htobe32(offset);
h->architecture = htonl(CBFS_ARCHITECTURE_UNKNOWN); h->architecture = htobe32(CBFS_ARCHITECTURE_UNKNOWN);
/* Never add a hash attribute to the master header. */ /* Never add a hash attribute to the master header. */
header = cbfs_create_file_header(CBFS_TYPE_CBFSHEADER, 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)); sizeof(struct cbfs_file_attr_position));
if (attrs == NULL) if (attrs == NULL)
goto error; goto error;
attrs->position = htonl(offset); attrs->position = htobe32(offset);
} }
/* Add alignment attribute if used */ /* Add alignment attribute if used */
if (param.alignment) { if (param.alignment) {
@ -936,7 +936,7 @@ static int cbfs_add_component(const char *filename,
sizeof(struct cbfs_file_attr_align)); sizeof(struct cbfs_file_attr_align));
if (attrs == NULL) if (attrs == NULL)
goto error; 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); free(compressed);
return -1; return -1;
} }
attrs->compression = htonl(param.compression); attrs->compression = htobe32(param.compression);
attrs->decompressed_size = htonl(decompressed_size); attrs->decompressed_size = htobe32(decompressed_size);
free(buffer->data); free(buffer->data);
buffer->data = compressed; buffer->data = compressed;
buffer->size = compressed_size; buffer->size = compressed_size;
out: out:
header->len = htonl(buffer->size); header->len = htobe32(buffer->size);
return 0; 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 /* Special care must be taken for LZ4-compressed stages that the BSS is
large enough to provide scratch space for in-place decompression. */ large enough to provide scratch space for in-place decompression. */
if (!param.precompression && param.compression == CBFS_COMPRESS_LZ4) { 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); size_t compressed_size = buffer_size(&output);
uint8_t *compare_buffer = malloc(memlen); uint8_t *compare_buffer = malloc(memlen);
uint8_t *start = compare_buffer + memlen - compressed_size; uint8_t *start = compare_buffer + memlen - compressed_size;
@ -1196,7 +1196,7 @@ static int cbfstool_convert_mkpayload(struct buffer *buffer,
if (ret != 0) { if (ret != 0) {
ret = parse_fit_to_payload(buffer, &output, param.compression); ret = parse_fit_to_payload(buffer, &output, param.compression);
if (ret == 0) 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 */ /* 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); buffer_delete(buffer);
// Direct assign, no dupe. // Direct assign, no dupe.
memcpy(buffer, &output, sizeof(*buffer)); memcpy(buffer, &output, sizeof(*buffer));
header->len = htonl(output.size); header->len = htobe32(output.size);
return 0; return 0;
} }
@ -1235,7 +1235,7 @@ static int cbfstool_convert_mkflatpayload(struct buffer *buffer,
buffer_delete(buffer); buffer_delete(buffer);
// Direct assign, no dupe. // Direct assign, no dupe.
memcpy(buffer, &output, sizeof(*buffer)); memcpy(buffer, &output, sizeof(*buffer));
header->len = htonl(output.size); header->len = htobe32(output.size);
return 0; return 0;
} }

View File

@ -15,17 +15,6 @@
/* Utilities */ /* Utilities */
int verbose = 0; 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) static off_t get_file_size(FILE *f)
{ {
off_t fsize; off_t fsize;

View File

@ -10,11 +10,10 @@
#include <assert.h> #include <assert.h>
#include <commonlib/bsd/cbfs_serialized.h> #include <commonlib/bsd/cbfs_serialized.h>
#include <commonlib/bsd/sysincludes.h>
#include <commonlib/helpers.h> #include <commonlib/helpers.h>
#include <console/console.h> #include <console/console.h>
#include "swab.h"
/* /*
* There are two address spaces that this tool deals with - SPI flash address space and host * 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 * address space. This macros checks if the address is greater than 2GiB under the assumption

View File

@ -356,11 +356,11 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
len = ntohl(cbfs_file->len); len = be32toh(cbfs_file->len);
offset = offset_to_ptr(convert_to_from_top_aligned, offset = offset_to_ptr(convert_to_from_top_aligned,
&image.buffer, &image.buffer,
cbfs_get_entry_addr(&image, cbfs_file) + cbfs_get_entry_addr(&image, cbfs_file) +
ntohl(cbfs_file->offset)); be32toh(cbfs_file->offset));
if (fit_add_entry(fit, offset, len, fit_type, 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, fit_address = offset_to_ptr(convert_to_from_top_aligned, &image.buffer,
cbfs_get_entry_addr(&image, cbfs_file) 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, if (set_fit_pointer(&bootblock, fit_address, convert_to_from_top_aligned,

View File

@ -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 */