From f3e309ad7dc2c07d2a6b5f4e0d86fc80cd46f755 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 6 Feb 2010 21:00:53 +0100 Subject: [PATCH] Use (hd0,msdos1) syntax. Eliminate partmap_iterate --- conf/any-emu.rmk | 2 +- conf/common.rmk | 2 +- conf/i386-pc.rmk | 14 ++-- conf/sparc64-ieee1275.rmk | 6 +- include/grub/partition.h | 26 ++++-- kern/partition.c | 170 ++++++++++++++------------------------ 6 files changed, 92 insertions(+), 128 deletions(-) diff --git a/conf/any-emu.rmk b/conf/any-emu.rmk index 53d4d1be7..9038aa45d 100644 --- a/conf/any-emu.rmk +++ b/conf/any-emu.rmk @@ -34,7 +34,7 @@ grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \ script/main.c script/execute.c script/function.c \ script/lexer.c script/script.c grub_script.tab.c \ partmap/amiga.c partmap/apple.c partmap/msdos.c partmap/sun.c \ - partmap/acorn.c partmap/gpt.c partmap/bsd.c \ + partmap/acorn.c partmap/gpt.c partmap/bsdlabel.c \ \ fs/affs.c fs/cpio.c fs/fat.c fs/ext2.c fs/hfs.c \ fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ diff --git a/conf/common.rmk b/conf/common.rmk index 2033d2478..009db2907 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -24,7 +24,7 @@ util/grub-probe.c_DEPENDENCIES = grub_probe_init.h grub_probe_SOURCES = gnulib/progname.c util/grub-probe.c \ util/hostdisk.c util/misc.c util/getroot.c \ kern/device.c kern/disk.c kern/err.c kern/misc.c \ - kern/parser.c kern/partition.c kern/file.c \ + kern/parser.c kern/partition.c kern/file.c kern/list.c \ \ fs/affs.c fs/cpio.c fs/fat.c fs/ext2.c fs/hfs.c \ fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index efbf5bfa2..02675be91 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -94,14 +94,14 @@ util/grub-mkrawimage.c_DEPENDENCIES = Makefile util/i386/pc/grub-setup.c_DEPENDENCIES = grub_setup_init.h grub_setup_SOURCES = gnulib/progname.c \ util/i386/pc/grub-setup.c util/hostdisk.c \ - util/misc.c util/getroot.c kern/device.c kern/disk.c \ - kern/err.c kern/misc.c kern/parser.c kern/partition.c \ - kern/file.c kern/fs.c kern/env.c fs/fshelp.c \ + util/misc.c util/getroot.c kern/device.c kern/disk.c \ + kern/err.c kern/misc.c kern/parser.c kern/partition.c \ + kern/file.c kern/list.c kern/fs.c kern/env.c fs/fshelp.c \ \ - fs/affs.c fs/cpio.c fs/ext2.c fs/fat.c fs/hfs.c \ - fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ - fs/ntfs.c fs/ntfscomp.c fs/reiserfs.c fs/sfs.c \ - fs/ufs.c fs/ufs2.c fs/xfs.c fs/afs.c fs/afs_be.c \ + fs/affs.c fs/cpio.c fs/ext2.c fs/fat.c fs/hfs.c \ + fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ + fs/ntfs.c fs/ntfscomp.c fs/reiserfs.c fs/sfs.c \ + fs/ufs.c fs/ufs2.c fs/xfs.c fs/afs.c fs/afs_be.c \ fs/befs.c fs/befs_be.c fs/tar.c \ \ partmap/msdos.c partmap/bsdlabel.c partmap/gpt.c \ diff --git a/conf/sparc64-ieee1275.rmk b/conf/sparc64-ieee1275.rmk index 9ec8dd22c..a428c14d1 100644 --- a/conf/sparc64-ieee1275.rmk +++ b/conf/sparc64-ieee1275.rmk @@ -68,9 +68,9 @@ grub_mkimage_SOURCES = util/sparc64/ieee1275/grub-mkimage.c util/misc.c \ # For grub-setup. util/sparc64/ieee1275/grub-setup.c_DEPENDENCIES = grub_setup_init.h grub_setup_SOURCES = util/sparc64/ieee1275/grub-setup.c util/hostdisk.c \ - util/misc.c util/getroot.c kern/device.c kern/disk.c \ - kern/err.c kern/misc.c kern/parser.c kern/partition.c \ - kern/file.c kern/fs.c kern/env.c fs/fshelp.c \ + util/misc.c util/getroot.c kern/device.c kern/disk.c \ + kern/err.c kern/misc.c kern/parser.c kern/partition.c \ + kern/file.c kern/fs.c kern/env.c kern/list.c fs/fshelp.c \ \ fs/affs.c fs/cpio.c fs/ext2.c fs/fat.c fs/hfs.c \ fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ diff --git a/include/grub/partition.h b/include/grub/partition.h index 5e8c476e0..bd97144c9 100644 --- a/include/grub/partition.h +++ b/include/grub/partition.h @@ -20,6 +20,7 @@ #define GRUB_PART_HEADER 1 #include +#include struct grub_disk; @@ -28,6 +29,9 @@ typedef struct grub_partition *grub_partition_t; /* Partition map type. */ struct grub_partition_map { + /* The next partition map type. */ + struct grub_partition_map *next; + /* The name of the partition map type. */ const char *name; @@ -35,9 +39,6 @@ struct grub_partition_map grub_err_t (*iterate) (struct grub_disk *disk, int (*hook) (struct grub_disk *disk, const grub_partition_t partition)); - - /* The next partition map type. */ - struct grub_partition_map *next; }; typedef struct grub_partition_map *grub_partition_map_t; @@ -76,11 +77,24 @@ int EXPORT_FUNC(grub_partition_iterate) (struct grub_disk *disk, const grub_partition_t partition)); char *EXPORT_FUNC(grub_partition_get_name) (const grub_partition_t partition); -int EXPORT_FUNC(grub_partition_map_iterate) (int (*hook) (const grub_partition_map_t partmap)); -void EXPORT_FUNC(grub_partition_map_register) (grub_partition_map_t partmap); +extern grub_partition_map_t EXPORT_VAR(grub_partition_map_list); -void EXPORT_FUNC(grub_partition_map_unregister) (grub_partition_map_t partmap); +static inline void +grub_partition_map_register (grub_partition_map_t partmap) +{ + grub_list_push (GRUB_AS_LIST_P (&grub_partition_map_list), + GRUB_AS_LIST (partmap)); +} + +static inline void +grub_partition_map_unregister (grub_partition_map_t partmap) +{ + grub_list_remove (GRUB_AS_LIST_P (&grub_partition_map_list), + GRUB_AS_LIST (partmap)); +} + +#define FOR_PARTITION_MAPS(var) for (var = grub_partition_map_list; var; var = var->next) #ifdef GRUB_UTIL void grub_msdos_partition_map_init (void); diff --git a/kern/partition.c b/kern/partition.c index 1f3f6556d..2a33ac329 100644 --- a/kern/partition.c +++ b/kern/partition.c @@ -21,39 +21,7 @@ #include #include -static grub_partition_map_t grub_partition_map_list; - -void -grub_partition_map_register (grub_partition_map_t partmap) -{ - partmap->next = grub_partition_map_list; - grub_partition_map_list = partmap; -} - -void -grub_partition_map_unregister (grub_partition_map_t partmap) -{ - grub_partition_map_t *p, q; - - for (p = &grub_partition_map_list, q = *p; q; p = &(q->next), q = q->next) - if (q == partmap) - { - *p = q->next; - break; - } -} - -int -grub_partition_map_iterate (int (*hook) (const grub_partition_map_t partmap)) -{ - grub_partition_map_t p; - - for (p = grub_partition_map_list; p; p = p->next) - if (hook (p)) - return 1; - - return 0; -} +grub_partition_map_t grub_partition_map_list; static grub_partition_t grub_partition_map_probe (const grub_partition_map_t partmap, @@ -97,41 +65,45 @@ grub_partition_probe (struct grub_disk *disk, const char *str) grub_partition_t curpart = 0; grub_partition_t tail; const char *ptr; - int num; - - auto int part_map_probe (const grub_partition_map_t partmap); - - int part_map_probe (const grub_partition_map_t partmap) - { - disk->partition = part; - curpart = grub_partition_map_probe (partmap, disk, num); - disk->partition = tail; - if (curpart) - return 1; - - if (grub_errno == GRUB_ERR_BAD_PART_TABLE) - { - /* Continue to next partition map type. */ - grub_errno = GRUB_ERR_NONE; - return 0; - } - - return 1; - } part = tail = disk->partition; for (ptr = str; *ptr;) { - /* BSD-like partition specification. */ - if (*ptr >= 'a' && *ptr <= 'z') - num = *(ptr++) - 'a'; - else - num = grub_strtoul (ptr, (char **) &ptr, 0) - 1; + grub_partition_map_t partmap; + int num; + const char *partname, *partname_end; + + partname = ptr; + while (*ptr && grub_isalpha (*ptr)) + ptr++; + partname_end = ptr; + num = grub_strtoul (ptr, (char **) &ptr, 0) - 1; curpart = 0; /* Use the first partition map type found. */ - grub_partition_map_iterate (part_map_probe); + FOR_PARTITION_MAPS(partmap) + { + if (partname_end != partname && + (grub_strncmp (partmap->name, partname, partname_end - partname) + != 0 || partmap->name[partname_end - partname] != 0)) + continue; + + disk->partition = part; + curpart = grub_partition_map_probe (partmap, disk, num); + disk->partition = tail; + if (curpart) + break; + + if (grub_errno == GRUB_ERR_BAD_PART_TABLE) + { + /* Continue to next partition map type. */ + grub_errno = GRUB_ERR_NONE; + continue; + } + + break; + } if (! curpart) { @@ -153,45 +125,6 @@ grub_partition_probe (struct grub_disk *disk, const char *str) return part; } -static grub_partition_map_t -get_partmap (struct grub_disk *disk) -{ - grub_partition_map_t partmap = 0; - struct grub_partition part; - int found = 0; - auto int part_map_iterate (const grub_partition_map_t p); - auto int part_map_iterate_hook (grub_disk_t d, - const grub_partition_t partition); - int part_map_iterate_hook (grub_disk_t d __attribute__ ((unused)), - const grub_partition_t partition) - { - found = 1; - part = *partition; - return 1; - } - - int part_map_iterate (const grub_partition_map_t p) - { - grub_dprintf ("partition", "Detecting %s...\n", p->name); - p->iterate (disk, part_map_iterate_hook); - - if (grub_errno != GRUB_ERR_NONE || ! found) - { - /* Continue to next partition map type. */ - grub_dprintf ("partition", "%s detection failed.\n", p->name); - grub_errno = GRUB_ERR_NONE; - return 0; - } - grub_free (part.data); - - grub_dprintf ("partition", "%s detection succeeded.\n", p->name); - partmap = p; - return 1; - } - grub_partition_map_iterate (part_map_iterate); - return partmap; -} - int grub_partition_iterate (struct grub_disk *disk, int (*hook) (grub_disk_t disk, @@ -205,27 +138,42 @@ grub_partition_iterate (struct grub_disk *disk, const grub_partition_t partition) { struct grub_partition p = *partition; - grub_partition_map_t partmap = 0; p.parent = dsk->partition; dsk->partition = 0; if (hook (dsk, &p)) + { + ret = 1; return 1; + } if (p.start != 0) { + const struct grub_partition_map *partmap; dsk->partition = &p; - partmap = get_partmap (dsk); - if (partmap) - ret = partmap->iterate (dsk, part_iterate); + FOR_PARTITION_MAPS(partmap) + { + grub_err_t err; + err = partmap->iterate (dsk, part_iterate); + if (err) + grub_errno = GRUB_ERR_NONE; + if (ret) + break; + } } dsk->partition = p.parent; return ret; } { - grub_partition_map_t partmap = 0; - partmap = get_partmap (disk); - if (partmap) - ret = partmap->iterate (disk, part_iterate); + const struct grub_partition_map *partmap; + FOR_PARTITION_MAPS(partmap) + { + grub_err_t err; + err = partmap->iterate (disk, part_iterate); + if (err) + grub_errno = GRUB_ERR_NONE; + if (ret) + break; + } } return ret; @@ -235,14 +183,16 @@ char * grub_partition_get_name (const grub_partition_t partition) { char *out = 0; - /* Even on 64-bit machines this buffer is enough to hold longest number. */ - char buf[25]; int curlen = 0; grub_partition_t part; for (part = partition; part; part = part->parent) { + /* Even on 64-bit machines this buffer is enough to hold + longest number. */ + char buf[grub_strlen (part->partmap->name) + 25]; int strl; - grub_snprintf (buf, sizeof (buf), "%d", part->number + 1); + grub_snprintf (buf, sizeof (buf), "%s%d", part->partmap->name, + part->number + 1); strl = grub_strlen (buf); if (curlen) {