Skip to content

Commit

Permalink
Unify checksum functions.
Browse files Browse the repository at this point in the history
Several checksum functions were scattered through source - use just one.
  • Loading branch information
KevinOConnor committed Mar 29, 2008
1 parent 46df61c commit 2e7ab8b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 59 deletions.
18 changes: 4 additions & 14 deletions src/disk.c
Expand Up @@ -26,16 +26,6 @@
disk_ret(__regs, DISK_RET_SUCCESS); \
} while (0)

static u8
checksum_seg(u16 seg, u16 offset, u32 len)
{
u32 i;
u8 sum = 0;
for (i=0; i<len; i++)
sum += GET_FARVAR(seg, *(u8*)(offset+i));
return sum;
}

static void
basic_access(struct bregs *regs, u8 device, u16 command)
{
Expand Down Expand Up @@ -452,9 +442,9 @@ disk_1348(struct bregs *regs, u8 device)
else
SET_EBDA(ata.dpte.revision, 0x10);

u8 sum = checksum_seg(EBDA_SEG
, offsetof(struct extended_bios_data_area_s, ata.dpte)
, 15);
u8 *p = MAKE_FARPTR(EBDA_SEG
, offsetof(struct extended_bios_data_area_s, ata.dpte));
u8 sum = checksum(p, 15);
SET_EBDA(ata.dpte.checksum, ~sum);

if (size < 0x42) {
Expand Down Expand Up @@ -498,7 +488,7 @@ disk_1348(struct bregs *regs, u8 device)
SET_INT13DPT(regs, device_path[2], 0);
SET_INT13DPT(regs, device_path[4], 0L);

sum = checksum_seg(regs->ds, 30, 34);
sum = checksum(MAKE_FARPTR(regs->ds, 30), 34);
SET_INT13DPT(regs, checksum, ~sum);
}

Expand Down
12 changes: 1 addition & 11 deletions src/post.c
Expand Up @@ -17,16 +17,6 @@
#define bda ((struct bios_data_area_s *)0)
#define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4))

static u8
checksum(u8 *p, u32 len)
{
u32 i;
u8 sum = 0;
for (i=0; i<len; i++)
sum += p[i];
return sum;
}

static void
init_bda()
{
Expand Down Expand Up @@ -258,7 +248,7 @@ fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
}
info->cylinders = cyl;
info->heads = heads;
info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
info->checksum = -checksum((u8*)info, sizeof(*info)-1);
}

static void
Expand Down
42 changes: 8 additions & 34 deletions src/rombios32.c
Expand Up @@ -634,15 +634,6 @@ static void putle32(u8 **pp, int val)
*pp = q;
}

static int mpf_checksum(const u8 *data, int len)
{
int sum, i;
sum = 0;
for(i = 0; i < len; i++)
sum += data[i];
return sum & 0xff;
}

static unsigned long align(unsigned long addr, unsigned long v)
{
return (addr + v - 1) & ~(v - 1);
Expand Down Expand Up @@ -734,7 +725,7 @@ static void mptable_init(void)
mp_config_table[4] = len;
mp_config_table[5] = len >> 8;

mp_config_table[7] = -mpf_checksum(mp_config_table, q - mp_config_table);
mp_config_table[7] = -checksum(mp_config_table, q - mp_config_table);

mp_config_table_size = q - mp_config_table;

Expand Down Expand Up @@ -764,8 +755,8 @@ static void mptable_init(void)
putb(&q, 0);
putb(&q, 0);
putb(&q, 0);
float_pointer_struct[10] =
-mpf_checksum(float_pointer_struct, q - float_pointer_struct);
float_pointer_struct[10] = -checksum(float_pointer_struct
, q - float_pointer_struct);
#ifdef BX_USE_EBDA_TABLES
ebda_cur_addr += (q - float_pointer_struct);
#else
Expand Down Expand Up @@ -981,15 +972,6 @@ static inline u32 cpu_to_le32(u32 x)
return x;
}

static int acpi_checksum(const u8 *data, int len)
{
int sum, i;
sum = 0;
for(i = 0; i < len; i++)
sum += data[i];
return (-sum) & 0xff;
}

static void acpi_build_table_header(struct acpi_table_header *h,
char *sig, int len, u8 rev)
{
Expand All @@ -1011,7 +993,7 @@ static void acpi_build_table_header(struct acpi_table_header *h,
memcpy(h->asl_compiler_id, "BXPC", 4);
#endif
h->asl_compiler_revision = cpu_to_le32(1);
h->checksum = acpi_checksum((void *)h, len);
h->checksum = -checksum((void *)h, len);
}

int acpi_build_processor_ssdt(u8 *ssdt)
Expand Down Expand Up @@ -1135,7 +1117,7 @@ void acpi_bios_init(void)
memcpy(rsdp->oem_id, "BOCHS ", 6);
#endif
rsdp->rsdt_physical_address = cpu_to_le32(rsdt_addr);
rsdp->checksum = acpi_checksum((void *)rsdp, 20);
rsdp->checksum = -checksum((void *)rsdp, 20);

/* RSDT */
memset(rsdt, 0, sizeof(*rsdt));
Expand Down Expand Up @@ -1369,8 +1351,6 @@ smbios_entry_point_init(void *start,
u32 structure_table_address,
u16 number_of_structures)
{
u8 sum;
int i;
struct smbios_entry_point *ep = (struct smbios_entry_point *)start;

memcpy(ep->anchor_string, "_SM_", 4);
Expand All @@ -1390,16 +1370,10 @@ smbios_entry_point_init(void *start,
ep->checksum = 0;
ep->intermediate_checksum = 0;

sum = 0;
for (i = 0; i < 0x10; i++)
sum += ((s8 *)start)[i];
ep->checksum = -sum;
ep->checksum = -checksum(start, 0x10);

sum = 0;
for (i = 0x10; i < ep->length; i++)
sum += ((s8 *)start)[i];
ep->intermediate_checksum = -sum;
}
ep->intermediate_checksum = -checksum(start + 0x10, ep->length - 0x10);
}

/* Type 0 -- BIOS Information */
#define RELEASE_DATE_STR "01/01/2007"
Expand Down
11 changes: 11 additions & 0 deletions src/util.c
@@ -1,5 +1,16 @@
#include "util.h" // usleep

// Sum the bytes in the specified area.
u8
checksum(u8 *far_data, u32 len)
{
u32 i;
u8 sum = 0;
for (i=0; i<len; i++)
sum += GET_FARPTR(far_data[i]);
return sum;
}

// Sleep for n microseconds. currently using the
// refresh request port 0x61 bit4, toggling every 15usec
void
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Expand Up @@ -173,6 +173,7 @@ void VISIBLE16 handle_1553(struct bregs *regs);
void handle_1ab1(struct bregs *regs);

// util.c
u8 checksum(u8 *far_data, u32 len);
void usleep(u32 count);

// rombios32.c
Expand Down

0 comments on commit 2e7ab8b

Please sign in to comment.