diff --git a/ChangeLog b/ChangeLog index b59e31134..e2e38f307 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2013-05-15 Radosław Szymczyszyn + + * grub-core/partmap/dfly.c: New partition map. + 2013-05-15 Vladimir Serbinenko * grub-core/kern/corecmd.c (grub_core_cmd_ls): Fix empty path diff --git a/Makefile.util.def b/Makefile.util.def index 6cc15cbf2..17fdfa569 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -130,6 +130,7 @@ library = { common = grub-core/partmap/dvh.c; common = grub-core/partmap/sunpc.c; common = grub-core/partmap/bsdlabel.c; + common = grub-core/partmap/dfly.c; common = grub-core/script/function.c; common = grub-core/script/lexer.c; common = grub-core/script/main.c; diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 602b49765..b63d33d0f 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1652,6 +1652,11 @@ module = { common = partmap/sunpc.c; }; +module = { + name = part_dfly; + common = partmap/dfly.c; +}; + module = { name = msdospart; common = parttool/msdospart.c; diff --git a/grub-core/partmap/dfly.c b/grub-core/partmap/dfly.c new file mode 100644 index 000000000..e72b799e4 --- /dev/null +++ b/grub-core/partmap/dfly.c @@ -0,0 +1,133 @@ +/* dfly.c - Read DragonFly BSD disklabel64. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2013 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +static struct grub_partition_map grub_dfly_partition_map; + +#define GRUB_PARTITION_DISKLABEL64_HEADER_SIZE 200 + +/* Full entry is 64 bytes however we really care only + about offset and size which are in first 16 bytes. + Avoid using too much stack. */ +#define GRUB_PARTITION_DISKLABEL64_ENTRY_SIZE 64 +struct grub_partition_disklabel64_entry +{ + grub_uint64_t boffset; + grub_uint64_t bsize; +}; + +/* Full entry is 200 bytes however we really care only + about magic and number of partitions which are in first 16 bytes. + Avoid using too much stack. */ +struct grub_partition_disklabel64 +{ + grub_uint32_t magic; +#define GRUB_DISKLABEL64_MAGIC ((grub_uint32_t)0xc4464c59) + grub_uint32_t crc; + grub_uint32_t unused; + grub_uint32_t npartitions; +}; + +static grub_err_t +dfly_partition_map_iterate (grub_disk_t disk, + grub_partition_iterate_hook_t hook, + void *hook_data) +{ + struct grub_partition part; + unsigned partno, pos; + struct grub_partition_disklabel64 label; + + part.partmap = &grub_dfly_partition_map; + + if (grub_disk_read (disk, 1, 0, sizeof (label), &label)) + return grub_errno; + + if (label.magic != grub_cpu_to_le32_compile_time (GRUB_DISKLABEL64_MAGIC)) + { + grub_dprintf ("partition", + "bad magic (found 0x%" PRIxGRUB_UINT32_T "; " + "wanted 0x%" PRIxGRUB_UINT32_T ")\n", + grub_le_to_cpu32 (label.magic), + GRUB_DISKLABEL64_MAGIC); + return grub_error (GRUB_ERR_BAD_PART_TABLE, "disklabel64 not found"); + } + + pos = GRUB_PARTITION_DISKLABEL64_HEADER_SIZE + GRUB_DISK_SECTOR_SIZE; + + for (partno = 0; + partno < grub_le_to_cpu32 (label.npartitions); ++partno) + { + grub_disk_addr_t sector = pos >> GRUB_DISK_SECTOR_BITS; + grub_off_t offset = pos & (GRUB_DISK_SECTOR_SIZE - 1); + struct grub_partition_disklabel64_entry dpart; + + pos += GRUB_PARTITION_DISKLABEL64_ENTRY_SIZE; + if (grub_disk_read (disk, sector, offset, sizeof (dpart), &dpart)) + return grub_errno; + + grub_dprintf ("partition", + "partition %2d: offset 0x%" PRIxGRUB_UINT64_T ", " + "size 0x%" PRIxGRUB_UINT64_T "\n", + partno, + grub_le_to_cpu64 (dpart.boffset), + grub_le_to_cpu64 (dpart.bsize)); + + /* Is partition initialized? */ + if (dpart.bsize == 0) + continue; + + part.number = partno; + part.start = grub_le_to_cpu64 (dpart.boffset) >> GRUB_DISK_SECTOR_BITS; + part.len = grub_le_to_cpu64 (dpart.bsize) >> GRUB_DISK_SECTOR_BITS; + + /* This is counter-intuitive, but part.offset and sector have + * the same type, and offset (NOT part.offset) is guaranteed + * to fit into part.index. */ + part.offset = sector; + part.index = offset; + + if (hook (disk, &part, hook_data)) + return grub_errno; + } + + return GRUB_ERR_NONE; +} + +/* Partition map type. */ +static struct grub_partition_map grub_dfly_partition_map = +{ + .name = "dfly", + .iterate = dfly_partition_map_iterate, +}; + +GRUB_MOD_INIT(part_dfly) +{ + grub_partition_map_register (&grub_dfly_partition_map); +} + +GRUB_MOD_FINI(part_dfly) +{ + grub_partition_map_unregister (&grub_dfly_partition_map); +} diff --git a/tests/dfly-mbr-mbexample.dfly.img.gz b/tests/dfly-mbr-mbexample.dfly.img.gz new file mode 100644 index 000000000..b63595d4e Binary files /dev/null and b/tests/dfly-mbr-mbexample.dfly.img.gz differ diff --git a/tests/dfly-mbr-mbexample.mbr.img.gz b/tests/dfly-mbr-mbexample.mbr.img.gz new file mode 100644 index 000000000..add2b1337 Binary files /dev/null and b/tests/dfly-mbr-mbexample.mbr.img.gz differ diff --git a/tests/partmap_test.in b/tests/partmap_test.in index 5be7b9dfb..7786ccfbc 100644 --- a/tests/partmap_test.in +++ b/tests/partmap_test.in @@ -28,6 +28,14 @@ create_disk_image () { dd if=/dev/zero of="${name}" bs=512 count=1 seek=$((size * 2048 - 1)) status=noxfer > /dev/null } +create_dfly_image () { + name="$1" + rm -f ${name} + + gunzip < "@srcdir@/tests/dfly-mbr-mbexample.mbr.img.gz" | dd of=${name} bs=1 seek=440 count=72 conv=notrunc > /dev/null + gunzip < "@srcdir@/tests/dfly-mbr-mbexample.dfly.img.gz" | dd of=${name} bs=512 seek=33 count=1 conv=notrunc > /dev/null +} + check_output () { outfile=$1 shift @@ -461,3 +469,12 @@ ${parted} -a none -s "${imgfile}" mklabel amiga mkpart x 0 10M mkpart x 10M 20M list_parts part_amiga "${imgfile}" "${outfile}" check_output "${outfile}" $disk $disk,amiga1 $disk,amiga2 $disk,amiga3 $disk,amiga4 $disk,amiga5 $disk,amiga6 $disk,amiga7 +# +# DragonFly BSD disklabel64 +# + +echo "Checking DragonFly BSD disklabel64..." + +create_dfly_image "${imgfile}" +list_parts part_dfly "${imgfile}" "${outfile}" +check_output "${outfile}" $disk $disk,msdos1 $disk,msdos1,dfly1 $disk,msdos1,dfly2 $disk,msdos1,dfly3