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@koconnor.net>
#include "acpi.h" // struct rsdp_descriptor
#include "util.h" // memcpy
+#include "byteorder.h" // cpu_to_le16
#include "pci.h" // pci_find_init_device
#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
#include "pci_regs.h" // PCI_INTERRUPT_LINE
#include "ioport.h" // inl
-#include "paravirt.h"
+#include "paravirt.h" // qemu_cfg_irq0_override
/****************************************************/
/* ACPI tables init */
#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
// 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;
// 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
// 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);
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));
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));
}
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));
}
--- /dev/null
+#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
#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
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)
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.
}
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));
}
}
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);
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;
// 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
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;
}
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);
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));
#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
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);