Byteswap the constant MBR_{BS_}MAGIC throughout, should genetate better code.

Except that non all architectures have that optimisation :-(
This commit is contained in:
dsl 2006-01-29 12:55:16 +00:00
parent f1d35de4d5
commit c28e0ae4af

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdisk.c,v 1.95 2005/10/07 23:57:10 uwe Exp $ */ /* $NetBSD: fdisk.c,v 1.96 2006/01/29 12:55:16 dsl Exp $ */
/* /*
* Mach Operating System * Mach Operating System
@ -39,7 +39,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID("$NetBSD: fdisk.c,v 1.95 2005/10/07 23:57:10 uwe Exp $"); __RCSID("$NetBSD: fdisk.c,v 1.96 2006/01/29 12:55:16 dsl Exp $");
#endif /* not lint */ #endif /* not lint */
#define MBRPTYPENAMES #define MBRPTYPENAMES
@ -78,6 +78,9 @@ __RCSID("$NetBSD: fdisk.c,v 1.95 2005/10/07 23:57:10 uwe Exp $");
#define DEFAULT_BOOTDIR "/usr/mdec" #define DEFAULT_BOOTDIR "/usr/mdec"
#define LE_MBR_MAGIC htole16(MBR_MAGIC)
#define LE_MBR_BS_MAGIC htole16(MBR_BS_MAGIC)
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
#if !HAVE_NBTOOL_CONFIG_H #if !HAVE_NBTOOL_CONFIG_H
#include <machine/cpu.h> #include <machine/cpu.h>
@ -559,8 +562,7 @@ print_s0(int which)
} }
} }
#ifdef BOOTSEL #ifdef BOOTSEL
if (!sh_flag && if (!sh_flag && mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
le16toh(mboot.mbr_bootsel_magic) == MBR_BS_MAGIC) {
int tmo; int tmo;
printf("Bootselector "); printf("Bootselector ");
@ -672,7 +674,7 @@ print_mbr_partition(struct mbr_sector *boot, int part,
printf("%s (sysid %d)\n", get_type(partp->mbrp_type), partp->mbrp_type); printf("%s (sysid %d)\n", get_type(partp->mbrp_type), partp->mbrp_type);
#ifdef BOOTSEL #ifdef BOOTSEL
if (le16toh(boot->mbr_bootsel_magic) == MBR_BS_MAGIC && if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC &&
boot->mbr_bootsel.mbrbs_nametab[part][0]) boot->mbr_bootsel.mbrbs_nametab[part][0])
printf("%*s bootmenu: %s\n", indent, "", printf("%*s bootmenu: %s\n", indent, "",
boot->mbr_bootsel.mbrbs_nametab[part]); boot->mbr_bootsel.mbrbs_nametab[part]);
@ -777,7 +779,7 @@ read_boot(const char *name, void *buf, size_t len, int err_exit)
/* /*
* Do some sanity checking here * Do some sanity checking here
*/ */
if (le16toh(((struct mbr_sector *)buf)->mbr_magic) != MBR_MAGIC) { if (((struct mbr_sector *)buf)->mbr_magic != LE_MBR_MAGIC) {
warnx("%s: invalid magic", boot_path); warnx("%s: invalid magic", boot_path);
goto fail; goto fail;
} }
@ -805,8 +807,8 @@ init_sector0(int zappart)
sizeof bootcode, 1); sizeof bootcode, 1);
#endif #endif
#ifdef BOOTSEL #ifdef BOOTSEL
if (le16toh(mboot.mbr_bootsel_magic) == MBR_BS_MAGIC if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC
&& le16toh(bootcode[0].mbr_bootsel_magic) == MBR_BS_MAGIC) && bootcode[0].mbr_bootsel_magic == LE_MBR_BS_MAGIC)
copy_size = MBR_BS_OFFSET; copy_size = MBR_BS_OFFSET;
#endif #endif
@ -814,7 +816,7 @@ init_sector0(int zappart)
boot_installed = 1; boot_installed = 1;
memcpy(&mboot, bootcode, copy_size); memcpy(&mboot, bootcode, copy_size);
} }
mboot.mbr_magic = htole16(MBR_MAGIC); mboot.mbr_magic = LE_MBR_MAGIC;
if (!zappart) if (!zappart)
return; return;
@ -983,7 +985,7 @@ get_default_boot(void)
unsigned int id; unsigned int id;
int p; int p;
if (le16toh(mboot.mbr_bootsel_magic) != MBR_BS_MAGIC) if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
/* default to first active partition */ /* default to first active partition */
return DEFAULT_ACTIVE; return DEFAULT_ACTIVE;
@ -1025,7 +1027,7 @@ set_default_boot(daddr_t default_ptn)
int p; int p;
int key = SCAN_1; int key = SCAN_1;
if (le16toh(mboot.mbr_bootsel_magic) != MBR_BS_MAGIC) if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
/* sanity */ /* sanity */
return; return;
@ -1085,7 +1087,7 @@ install_bootsel(int needed)
for (p = 0; p < MBR_PART_COUNT; p++) { for (p = 0; p < MBR_PART_COUNT; p++) {
if (mboot.mbr_parts[p].mbrp_type == 0) if (mboot.mbr_parts[p].mbrp_type == 0)
continue; continue;
if (le16toh(mboot.mbr_bootsel_magic) != MBR_BS_MAGIC) if (mboot.mbr_bootsel_magic != LE_MBR_BS_MAGIC)
break; break;
if (mbs->mbrbs_nametab[p][0] == 0) if (mbs->mbrbs_nametab[p][0] == 0)
continue; continue;
@ -1095,7 +1097,7 @@ install_bootsel(int needed)
} }
for (p = 0; p < ext.num_ptn; p++) { for (p = 0; p < ext.num_ptn; p++) {
if (le16toh(ext.ptn[p].mbr_bootsel_magic) != MBR_BS_MAGIC) if (ext.ptn[p].mbr_bootsel_magic != LE_MBR_BS_MAGIC)
continue; continue;
if (ext.ptn[p].mbr_parts[0].mbrp_type == 0) if (ext.ptn[p].mbr_parts[0].mbrp_type == 0)
continue; continue;
@ -1109,7 +1111,7 @@ install_bootsel(int needed)
/* Is the installed code good enough ? */ /* Is the installed code good enough ? */
if (!i_flag && (needed == 0 || if (!i_flag && (needed == 0 ||
(le16toh(mboot.mbr_bootsel_magic) == MBR_BS_MAGIC (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC
&& (mbs->mbrbs_flags & needed) == needed))) { && (mbs->mbrbs_flags & needed) == needed))) {
/* yes - just set flags */ /* yes - just set flags */
mbs->mbrbs_flags |= ext13; mbs->mbrbs_flags |= ext13;
@ -1131,7 +1133,7 @@ install_bootsel(int needed)
/* Were we told a specific file ? (which we have already read) */ /* Were we told a specific file ? (which we have already read) */
/* If so check that it supports what we need. */ /* If so check that it supports what we need. */
if (bootsize != 0 && needed != 0 if (bootsize != 0 && needed != 0
&& (le16toh(bootcode[0].mbr_bootsel_magic) != MBR_BS_MAGIC && (bootcode[0].mbr_bootsel_magic != LE_MBR_BS_MAGIC
|| ((bootcode[0].mbr_bootsel.mbrbs_flags & needed) != needed))) { || ((bootcode[0].mbr_bootsel.mbrbs_flags & needed) != needed))) {
/* No it doesn't... */ /* No it doesn't... */
if (f_flag) if (f_flag)
@ -1171,7 +1173,7 @@ install_bootsel(int needed)
init_sector0(0); init_sector0(0);
if (le16toh(mboot.mbr_bootsel_magic) == MBR_BS_MAGIC) if (mboot.mbr_bootsel_magic == LE_MBR_BS_MAGIC)
mbs->mbrbs_flags = bootcode[0].mbr_bootsel.mbrbs_flags | ext13; mbs->mbrbs_flags = bootcode[0].mbr_bootsel.mbrbs_flags | ext13;
} }
@ -1439,7 +1441,7 @@ add_ext_ptn(daddr_t start, daddr_t size)
memmove(&ext.ptn[part + 1], &ext.ptn[part], memmove(&ext.ptn[part + 1], &ext.ptn[part],
(ext.num_ptn - part) * sizeof ext.ptn[0]); (ext.num_ptn - part) * sizeof ext.ptn[0]);
memset(&ext.ptn[part], 0, sizeof ext.ptn[0]); memset(&ext.ptn[part], 0, sizeof ext.ptn[0]);
ext.ptn[part].mbr_magic = htole16(MBR_MAGIC); ext.ptn[part].mbr_magic = LE_MBR_MAGIC;
/* we will be 'part' */ /* we will be 'part' */
if (part == 0) { if (part == 0) {
/* link us to 'next' */ /* link us to 'next' */
@ -1512,7 +1514,7 @@ check_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
ext.ptn = calloc(1, sizeof ext.ptn[0]); ext.ptn = calloc(1, sizeof ext.ptn[0]);
if (ext.ptn == NULL) if (ext.ptn == NULL)
err(1, "Malloc failed"); err(1, "Malloc failed");
ext.ptn[0].mbr_magic = htole16(MBR_MAGIC); ext.ptn[0].mbr_magic = LE_MBR_MAGIC;
ext.ptn_id = part; ext.ptn_id = part;
ext.base = start; ext.base = start;
ext.limit = start + size; ext.limit = start + size;
@ -1679,8 +1681,7 @@ change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
if (bootmenu != NULL) if (bootmenu != NULL)
strlcpy(tmp_bootmenu, bootmenu, bootmenu_len); strlcpy(tmp_bootmenu, bootmenu, bootmenu_len);
else else
if (boot != NULL && if (boot != NULL && boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC)
le16toh(boot->mbr_bootsel_magic) == MBR_BS_MAGIC)
strlcpy(tmp_bootmenu, strlcpy(tmp_bootmenu,
boot->mbr_bootsel.mbrbs_nametab[upart], boot->mbr_bootsel.mbrbs_nametab[upart],
bootmenu_len); bootmenu_len);
@ -1868,7 +1869,7 @@ change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
if (start == 0 && size == 0) if (start == 0 && size == 0)
memset(partp, 0, sizeof *partp); memset(partp, 0, sizeof *partp);
#ifdef BOOTSEL #ifdef BOOTSEL
if (le16toh(boot->mbr_bootsel_magic) == MBR_BS_MAGIC) if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC)
memset(boot->mbr_bootsel.mbrbs_nametab[upart], 0, memset(boot->mbr_bootsel.mbrbs_nametab[upart], 0,
sizeof boot->mbr_bootsel.mbrbs_nametab[0]); sizeof boot->mbr_bootsel.mbrbs_nametab[0]);
#endif #endif
@ -1903,19 +1904,19 @@ change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
&partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect); &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
#ifdef BOOTSEL #ifdef BOOTSEL
if (extended) { if (extended) {
boot->mbr_bootsel_magic = htole16(MBR_BS_MAGIC); boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC;
strncpy(boot->mbr_bootsel.mbrbs_nametab[upart], tmp_bootmenu, strncpy(boot->mbr_bootsel.mbrbs_nametab[upart], tmp_bootmenu,
bootmenu_len); bootmenu_len);
} else { } else {
/* We need to bootselect code installed in order to have /* We need to bootselect code installed in order to have
* somewhere to safely write the menu tag. * somewhere to safely write the menu tag.
*/ */
if (le16toh(boot->mbr_bootsel_magic) != MBR_BS_MAGIC) { if (boot->mbr_bootsel_magic != LE_MBR_BS_MAGIC) {
if (yesno("The bootselect code is not installed, " if (yesno("The bootselect code is not installed, "
"do you want to install it now?")) "do you want to install it now?"))
install_bootsel(MBR_BS_ACTIVE); install_bootsel(MBR_BS_ACTIVE);
} }
if (le16toh(boot->mbr_bootsel_magic) == MBR_BS_MAGIC) { if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
strncpy(boot->mbr_bootsel.mbrbs_nametab[upart], strncpy(boot->mbr_bootsel.mbrbs_nametab[upart],
tmp_bootmenu, bootmenu_len); tmp_bootmenu, bootmenu_len);
} }
@ -2293,14 +2294,14 @@ read_s0(daddr_t offset, struct mbr_sector *boot)
warn("Can't read %s partition table", tabletype); warn("Can't read %s partition table", tabletype);
return -1; return -1;
} }
if (le16toh(boot->mbr_magic) != MBR_MAGIC) { if (boot->mbr_magic != LE_MBR_MAGIC) {
warnx("%s partition table invalid, " warnx("%s partition table invalid, "
"no magic in sector %"PRIdaddr, tabletype, offset); "no magic in sector %"PRIdaddr, tabletype, offset);
return -1; return -1;
} }
#ifdef BOOTSEL #ifdef BOOTSEL
if (le16toh(boot->mbr_bootsel_magic) == MBR_BS_MAGIC) { if (boot->mbr_bootsel_magic == LE_MBR_BS_MAGIC) {
/* mbr_bootsel in new location */ /* mbr_bootsel in new location */
if (validate_bootsel(&boot->mbr_bootsel)) { if (validate_bootsel(&boot->mbr_bootsel)) {
warnx("removing corrupt bootsel information"); warnx("removing corrupt bootsel information");
@ -2308,7 +2309,7 @@ read_s0(daddr_t offset, struct mbr_sector *boot)
} }
return 0; return 0;
} }
if (le16toh(boot->mbr_bootsel_magic) != MBR_MAGIC) if (boot->mbr_bootsel_magic != LE_MBR_MAGIC)
return 0; return 0;
/* mbr_bootsel in old location */ /* mbr_bootsel in old location */
@ -2337,7 +2338,7 @@ read_s0(daddr_t offset, struct mbr_sector *boot)
id = DEFAULT_ACTIVE; id = DEFAULT_ACTIVE;
boot->mbr_bootsel.mbrbs_defkey = id; boot->mbr_bootsel.mbrbs_defkey = id;
} }
boot->mbr_bootsel_magic = htole16(MBR_BS_MAGIC); boot->mbr_bootsel_magic = LE_MBR_BS_MAGIC;
/* highlight that new bootsel code is necessary */ /* highlight that new bootsel code is necessary */
boot->mbr_bootsel.mbrbs_flags &= ~MBR_BS_NEWMBR; boot->mbr_bootsel.mbrbs_flags &= ~MBR_BS_NEWMBR;
#endif /* BOOTSEL */ #endif /* BOOTSEL */