[SeaBIOS] [PATCH] Use cpu_to_be32() (and related) instead of htonl (and related).

Kevin O'Connor kevin at koconnor.net
Wed Aug 15 03:20:10 CEST 2012


Unify the syntax for byte swab calls.

This also fixes a bug in coreboot due to the lack of a be64_to_cpu()
call.

Signed-off-by: Kevin O'Connor <kevin at koconnor.net>
---
 src/ata.c       |  3 ++-
 src/blockcmd.c  | 17 +++++++-------
 src/byteorder.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/coreboot.c  | 31 +++++++++++++-------------
 src/paravirt.c  |  9 ++++----
 src/util.h      | 30 -------------------------
 vgasrc/vgafb.c  |  3 ++-
 7 files changed, 103 insertions(+), 59 deletions(-)
 create mode 100644 src/byteorder.h

diff --git a/src/ata.c b/src/ata.c
index 7ff5f86..f604b37 100644
--- a/src/ata.c
+++ b/src/ata.c
@@ -8,6 +8,7 @@
 #include "types.h" // u8
 #include "ioport.h" // inb
 #include "util.h" // dprintf
+#include "byteorder.h" // be16_to_cpu
 #include "cmos.h" // inb_cmos
 #include "pic.h" // enable_hwirq
 #include "biosvar.h" // GET_GLOBAL
@@ -696,7 +697,7 @@ ata_extract_model(char *model, u32 size, u16 *buffer)
     // Read model name
     int i;
     for (i=0; i<size/2; i++)
-        *(u16*)&model[i*2] = ntohs(buffer[27+i]);
+        *(u16*)&model[i*2] = be16_to_cpu(buffer[27+i]);
     model[size] = 0x00;
     nullTrailingSpace(model);
     return model;
diff --git a/src/blockcmd.c b/src/blockcmd.c
index 97c72a6..77c690f 100644
--- a/src/blockcmd.c
+++ b/src/blockcmd.c
@@ -6,7 +6,8 @@
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 
 #include "biosvar.h" // GET_GLOBAL
-#include "util.h" // htonl
+#include "util.h" // dprintf
+#include "byteorder.h" // be32_to_cpu
 #include "disk.h" // struct disk_op_s
 #include "blockcmd.h" // struct cdb_request_sense
 #include "ata.h" // atapi_cmd_data
@@ -144,12 +145,12 @@ scsi_init_drive(struct drive_s *drive, const char *s, int prio)
     // READ CAPACITY returns the address of the last block.
     // We do not bother with READ CAPACITY(16) because BIOS does not support
     // 64-bit LBA anyway.
-    drive->blksize = ntohl(capdata.blksize);
+    drive->blksize = be32_to_cpu(capdata.blksize);
     if (drive->blksize != DISK_SECTOR_SIZE) {
         dprintf(1, "%s: unsupported block size %d\n", s, drive->blksize);
         return -1;
     }
-    drive->sectors = (u64)ntohl(capdata.sectors) + 1;
+    drive->sectors = (u64)be32_to_cpu(capdata.sectors) + 1;
     dprintf(1, "%s blksize=%d sectors=%d\n"
             , s, drive->blksize, (unsigned)drive->sectors);
 
@@ -243,7 +244,7 @@ cdb_mode_sense_geom(struct disk_op_s *op, struct cdbres_mode_sense_geom *data)
     cmd.command = CDB_CMD_MODE_SENSE;
     cmd.flags = 8; /* DBD */
     cmd.page = MODE_PAGE_HD_GEOMETRY;
-    cmd.count = htons(sizeof(*data));
+    cmd.count = cpu_to_be16(sizeof(*data));
     op->count = 1;
     op->buf_fl = data;
     return cdb_cmd_data(op, &cmd, sizeof(*data));
@@ -256,8 +257,8 @@ cdb_read(struct disk_op_s *op)
     struct cdb_rwdata_10 cmd;
     memset(&cmd, 0, sizeof(cmd));
     cmd.command = CDB_CMD_READ_10;
-    cmd.lba = htonl(op->lba);
-    cmd.count = htons(op->count);
+    cmd.lba = cpu_to_be32(op->lba);
+    cmd.count = cpu_to_be16(op->count);
     return cdb_cmd_data(op, &cmd, GET_GLOBAL(op->drive_g->blksize));
 }
 
@@ -268,7 +269,7 @@ cdb_write(struct disk_op_s *op)
     struct cdb_rwdata_10 cmd;
     memset(&cmd, 0, sizeof(cmd));
     cmd.command = CDB_CMD_WRITE_10;
-    cmd.lba = htonl(op->lba);
-    cmd.count = htons(op->count);
+    cmd.lba = cpu_to_be32(op->lba);
+    cmd.count = cpu_to_be16(op->count);
     return cdb_cmd_data(op, &cmd, GET_GLOBAL(op->drive_g->blksize));
 }
diff --git a/src/byteorder.h b/src/byteorder.h
new file mode 100644
index 0000000..94e3a3b
--- /dev/null
+++ b/src/byteorder.h
@@ -0,0 +1,69 @@
+#ifndef __BYTEORDER_H
+#define __BYTEORDER_H
+
+static inline u16 __swab16_constant(u16 val) {
+    return (val<<8) | (val>>8);
+}
+static inline u32 __swab32_constant(u32 val) {
+    return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
+}
+static inline u64 __swab64_constant(u64 val) {
+    return ((u64)__swab32_constant(val) << 32) | __swab32_constant(val>>32);
+}
+static inline u32 __swab32(u32 val) {
+    asm("bswapl %0" : "+r"(val));
+    return val;
+}
+static inline u64 __swab64(u64 val) {
+    union u64_u32_u i, o;
+    i.val = val;
+    o.hi = __swab32(i.lo);
+    o.lo = __swab32(i.hi);
+    return o.val;
+}
+
+#define swab16(x) __swab16_constant(x)
+#define swab32(x) (__builtin_constant_p((u32)(x)) \
+                   ? __swab32_constant(x) : __swab32(x))
+#define swab64(x) (__builtin_constant_p((u64)(x)) \
+                   ? __swab64_constant(x) : __swab64(x))
+
+static inline u16 cpu_to_le16(u16 x) {
+    return x;
+}
+static inline u32 cpu_to_le32(u32 x) {
+    return x;
+}
+static inline u64 cpu_to_le64(u64 x) {
+    return x;
+}
+static inline u16 le16_to_cpu(u16 x) {
+    return x;
+}
+static inline u32 le32_to_cpu(u32 x) {
+    return x;
+}
+static inline u32 le64_to_cpu(u64 x) {
+    return x;
+}
+
+static inline u16 cpu_to_be16(u16 x) {
+    return swab16(x);
+}
+static inline u32 cpu_to_be32(u32 x) {
+    return swab32(x);
+}
+static inline u64 cpu_to_be64(u64 x) {
+    return swab64(x);
+}
+static inline u16 be16_to_cpu(u16 x) {
+    return swab16(x);
+}
+static inline u32 be32_to_cpu(u32 x) {
+    return swab32(x);
+}
+static inline u32 be64_to_cpu(u64 x) {
+    return swab64(x);
+}
+
+#endif // byteorder.h
diff --git a/src/coreboot.c b/src/coreboot.c
index 55590ce..e4767f1 100644
--- a/src/coreboot.c
+++ b/src/coreboot.c
@@ -6,6 +6,7 @@
 
 #include "memmap.h" // add_e820
 #include "util.h" // dprintf
+#include "byteorder.h" // be32_to_cpu
 #include "lzmadecode.h" // LzmaDecode
 #include "smbios.h" // smbios_init
 #include "boot.h" // boot_add_cbfs
@@ -329,17 +330,17 @@ coreboot_cbfs_setup(void)
         return;
 
     struct cbfs_header *hdr = *(void **)CBFS_HEADPTR_ADDR;
-    if (hdr->magic != htonl(CBFS_HEADER_MAGIC)) {
+    if (hdr->magic != cpu_to_be32(CBFS_HEADER_MAGIC)) {
         dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
-                , hdr, hdr->magic, htonl(CBFS_HEADER_MAGIC));
+                , hdr, hdr->magic, cpu_to_be32(CBFS_HEADER_MAGIC));
         return;
     }
     dprintf(1, "Found CBFS header at %p\n", hdr);
 
-    struct cbfs_file *cfile = (void *)(0 - ntohl(hdr->romsize)
-                                       + ntohl(hdr->offset));
+    struct cbfs_file *cfile = (void *)(0 - be32_to_cpu(hdr->romsize)
+                                       + be32_to_cpu(hdr->offset));
     for (;;) {
-        if (cfile < (struct cbfs_file *)(0xFFFFFFFF - ntohl(hdr->romsize)))
+        if (cfile < (struct cbfs_file *)(0xFFFFFFFF - be32_to_cpu(hdr->romsize)))
             break;
         u64 magic = cfile->magic;
         if (magic != CBFS_FILE_MAGIC)
@@ -352,10 +353,10 @@ coreboot_cbfs_setup(void)
         memset(file, 0, sizeof(*file));
         strtcpy(file->name, cfile->filename, sizeof(file->name));
         dprintf(3, "Found CBFS file: %s\n", file->name);
-        file->size = file->rawsize = ntohl(cfile->len);
+        file->size = file->rawsize = be32_to_cpu(cfile->len);
         file->id = (u32)cfile;
         file->copy = cbfs_copyfile;
-        file->data = (void*)cfile + ntohl(cfile->offset);
+        file->data = (void*)cfile + be32_to_cpu(cfile->offset);
         int len = strlen(file->name);
         if (len > 5 && strcmp(&file->name[len-5], ".lzma") == 0) {
             // Using compression.
@@ -365,7 +366,7 @@ coreboot_cbfs_setup(void)
         }
         romfile_add(file);
 
-        cfile = (void*)ALIGN((u32)file->data + file->size, ntohl(hdr->align));
+        cfile = (void*)ALIGN((u32)file->data + file->size, be32_to_cpu(hdr->align));
     }
 }
 
@@ -394,13 +395,13 @@ cbfs_run_payload(struct cbfs_file *file)
     if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
         return;
     dprintf(1, "Run %s\n", file->filename);
-    struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
+    struct cbfs_payload *pay = (void*)file + be32_to_cpu(file->offset);
     struct cbfs_payload_segment *seg = pay->segments;
     for (;;) {
-        void *src = (void*)pay + ntohl(seg->offset);
-        void *dest = (void*)ntohl((u32)seg->load_addr);
-        u32 src_len = ntohl(seg->len);
-        u32 dest_len = ntohl(seg->mem_len);
+        void *src = (void*)pay + be32_to_cpu(seg->offset);
+        void *dest = (void*)(u32)be64_to_cpu(seg->load_addr);
+        u32 src_len = be32_to_cpu(seg->len);
+        u32 dest_len = be32_to_cpu(seg->mem_len);
         switch (seg->type) {
         case PAYLOAD_SEGMENT_BSS:
             dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
@@ -415,12 +416,12 @@ cbfs_run_payload(struct cbfs_file *file)
         default:
             dprintf(3, "Segment %x %d@%p -> %d@%p\n"
                     , seg->type, src_len, src, dest_len, dest);
-            if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
+            if (seg->compression == cpu_to_be32(CBFS_COMPRESS_NONE)) {
                 if (src_len > dest_len)
                     src_len = dest_len;
                 memcpy(dest, src, src_len);
             } else if (CONFIG_LZMA
-                       && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
+                       && seg->compression == cpu_to_be32(CBFS_COMPRESS_LZMA)) {
                 int ret = ulzma(dest, dest_len, src, src_len);
                 if (ret < 0)
                     return;
diff --git a/src/paravirt.c b/src/paravirt.c
index 2a98d53..4b5c441 100644
--- a/src/paravirt.c
+++ b/src/paravirt.c
@@ -8,7 +8,8 @@
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 
 #include "config.h" // CONFIG_COREBOOT
-#include "util.h" // ntoh[ls]
+#include "util.h" // dprintf
+#include "byteorder.h" // be32_to_cpu
 #include "ioport.h" // outw
 #include "paravirt.h" // qemu_cfg_port_probe
 #include "smbios.h" // struct smbios_structure_header
@@ -327,7 +328,7 @@ void qemu_cfg_romfile_setup(void)
 
     u32 count;
     qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
-    count = ntohl(count);
+    count = be32_to_cpu(count);
     u32 e;
     for (e = 0; e < count; e++) {
         struct QemuCfgFile qfile;
@@ -339,8 +340,8 @@ void qemu_cfg_romfile_setup(void)
         }
         memset(file, 0, sizeof(*file));
         strtcpy(file->name, qfile.name, sizeof(file->name));
-        file->size = ntohl(qfile.size);
-        file->id = ntohs(qfile.select);
+        file->size = be32_to_cpu(qfile.size);
+        file->id = be16_to_cpu(qfile.select);
         file->copy = qemu_cfg_read_file;
         romfile_add(file);
         dprintf(3, "Found fw_cfg file: %s (size=%d)\n", file->name, file->size);
diff --git a/src/util.h b/src/util.h
index 89e928c..062eea3 100644
--- a/src/util.h
+++ b/src/util.h
@@ -104,36 +104,6 @@ static inline u32 __fls(u32 word)
     return word;
 }
 
-static inline u16 __htons_constant(u16 val) {
-    return (val<<8) | (val>>8);
-}
-static inline u32 __htonl_constant(u32 val) {
-    return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
-}
-static inline u32 __htonl(u32 val) {
-    asm("bswapl %0" : "+r"(val));
-    return val;
-}
-#define htonl(x) (__builtin_constant_p((u32)(x)) ? __htonl_constant(x) : __htonl(x))
-#define ntohl(x) htonl(x)
-#define htons(x) __htons_constant(x)
-#define ntohs(x) htons(x)
-
-static inline u16 cpu_to_le16(u16 x)
-{
-    return x;
-}
-
-static inline u32 cpu_to_le32(u32 x)
-{
-    return x;
-}
-
-static inline u32 le32_to_cpu(u32 x)
-{
-    return x;
-}
-
 static inline u32 getesp(void) {
     u32 esp;
     asm("movl %%esp, %0" : "=rm"(esp));
diff --git a/vgasrc/vgafb.c b/vgasrc/vgafb.c
index 233f3d5..3b2f367 100644
--- a/vgasrc/vgafb.c
+++ b/vgasrc/vgafb.c
@@ -8,6 +8,7 @@
 #include "vgabios.h" // vgafb_scroll
 #include "biosvar.h" // GET_BDA
 #include "util.h" // memset_far
+#include "byteorder.h" // cpu_to_be16
 #include "stdvga.h" // stdvga_planar4_plane
 
 
@@ -272,7 +273,7 @@ write_gfx_char_cga(struct vgamode_s *vmode_g
             for (j = 0; j < 8; j++)
                 if (fontline & (1<<j))
                     pixels |= (ca.attr & 0x03) << (j*2);
-            pixels = htons(pixels);
+            pixels = cpu_to_be16(pixels);
             if (ca.attr & 0x80)
                 pixels ^= GET_FARVAR(SEG_GRAPH, *(u16*)dest_far);
             SET_FARVAR(SEG_CTEXT, *(u16*)dest_far, pixels);
-- 
1.7.11.2




More information about the SeaBIOS mailing list