Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / partmap / dvh.c
blobc8f467eb5735a5ce86a7e26df215f4a6a0aab452
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2005,2006,2007,2011 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/partition.h>
20 #include <grub/disk.h>
21 #include <grub/mm.h>
22 #include <grub/misc.h>
23 #include <grub/dl.h>
24 #include <grub/symbol.h>
25 #include <grub/types.h>
26 #include <grub/err.h>
28 GRUB_MOD_LICENSE ("GPLv3+");
30 #define DVH_MAGIC 0x0be5a941
32 struct grub_dvh_partition_descriptor
34 grub_uint32_t length;
35 grub_uint32_t start;
36 grub_uint32_t type;
37 } __attribute__ ((packed));
39 struct grub_dvh_block
41 grub_uint32_t magic;
42 grub_uint8_t unused[308];
43 struct grub_dvh_partition_descriptor parts[16];
44 grub_uint32_t checksum;
45 grub_uint32_t unused2;
46 } __attribute__ ((packed));
48 static struct grub_partition_map grub_dvh_partition_map;
50 /* Verify checksum (true=ok). */
51 static int
52 grub_dvh_is_valid (grub_uint32_t *label)
54 grub_uint32_t *pos;
55 grub_uint32_t sum = 0;
57 for (pos = label;
58 pos < (label + sizeof (struct grub_dvh_block) / 4);
59 pos++)
60 sum += *pos;
62 return ! sum;
65 static grub_err_t
66 dvh_partition_map_iterate (grub_disk_t disk,
67 int (*hook) (grub_disk_t disk,
68 const grub_partition_t partition))
70 struct grub_partition p;
71 union
73 struct grub_dvh_block dvh;
74 grub_uint32_t raw[0];
75 } block;
76 unsigned partnum;
77 grub_err_t err;
79 p.partmap = &grub_dvh_partition_map;
80 err = grub_disk_read (disk, 0, 0, sizeof (struct grub_dvh_block),
81 &block);
82 if (err)
83 return err;
85 if (DVH_MAGIC != grub_be_to_cpu32 (block.dvh.magic))
86 return grub_error (GRUB_ERR_BAD_PART_TABLE, "not a dvh partition table");
88 if (! grub_dvh_is_valid (block.raw))
89 return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
91 /* Maybe another error value would be better, because partition
92 table _is_ recognized but invalid. */
93 for (partnum = 0; partnum < ARRAY_SIZE (block.dvh.parts); partnum++)
95 if (block.dvh.parts[partnum].length == 0)
96 continue;
98 if (partnum == 10)
99 continue;
101 p.start = grub_be_to_cpu32 (block.dvh.parts[partnum].start);
102 p.len = grub_be_to_cpu32 (block.dvh.parts[partnum].length);
103 p.number = p.index = partnum;
104 if (hook (disk, &p))
105 break;
108 return grub_errno;
112 /* Partition map type. */
113 static struct grub_partition_map grub_dvh_partition_map =
115 .name = "dvh",
116 .iterate = dvh_partition_map_iterate,
119 GRUB_MOD_INIT(part_dvh)
121 grub_partition_map_register (&grub_dvh_partition_map);
124 GRUB_MOD_FINI(part_dvh)
126 grub_partition_map_unregister (&grub_dvh_partition_map);