Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / partmap / bsdlabel.c
blobc806f19a788d79dd2b348feaac919d86b5ddec27
1 /* bsdlabel.c - Read BSD style partition tables. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/partition.h>
21 #include <grub/bsdlabel.h>
22 #include <grub/disk.h>
23 #include <grub/mm.h>
24 #include <grub/misc.h>
25 #include <grub/dl.h>
26 #include <grub/msdos_partition.h>
27 #include <grub/i18n.h>
29 GRUB_MOD_LICENSE ("GPLv3+");
31 #ifdef GRUB_UTIL
32 #include <grub/emu/misc.h>
33 #endif
35 static struct grub_partition_map grub_bsdlabel_partition_map;
36 static struct grub_partition_map grub_netbsdlabel_partition_map;
37 static struct grub_partition_map grub_openbsdlabel_partition_map;
41 static grub_err_t
42 iterate_real (grub_disk_t disk, grub_disk_addr_t sector, int freebsd,
43 struct grub_partition_map *pmap,
44 int (*hook) (grub_disk_t disk,
45 const grub_partition_t partition))
47 struct grub_partition_bsd_disk_label label;
48 struct grub_partition p;
49 grub_disk_addr_t delta = 0;
50 grub_disk_addr_t pos;
52 /* Read the BSD label. */
53 if (grub_disk_read (disk, sector, 0, sizeof (label), &label))
54 return grub_errno;
56 /* Check if it is valid. */
57 if (label.magic != grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC))
58 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
60 /* A kludge to determine a base of be.offset. */
61 if (GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION
62 < grub_cpu_to_le16 (label.num_partitions) && freebsd)
64 struct grub_partition_bsd_entry whole_disk_be;
66 pos = sizeof (label) + sector * GRUB_DISK_SECTOR_SIZE
67 + sizeof (struct grub_partition_bsd_entry)
68 * GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION;
70 if (grub_disk_read (disk, pos / GRUB_DISK_SECTOR_SIZE,
71 pos % GRUB_DISK_SECTOR_SIZE, sizeof (whole_disk_be),
72 &whole_disk_be))
73 return grub_errno;
75 delta = grub_le_to_cpu32 (whole_disk_be.offset);
78 pos = sizeof (label) + sector * GRUB_DISK_SECTOR_SIZE;
80 for (p.number = 0;
81 p.number < grub_cpu_to_le16 (label.num_partitions);
82 p.number++, pos += sizeof (struct grub_partition_bsd_entry))
84 struct grub_partition_bsd_entry be;
86 if (p.number == GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION)
87 continue;
89 p.offset = pos / GRUB_DISK_SECTOR_SIZE;
90 p.index = pos % GRUB_DISK_SECTOR_SIZE;
92 if (grub_disk_read (disk, p.offset, p.index, sizeof (be), &be))
93 return grub_errno;
95 p.start = grub_le_to_cpu32 (be.offset);
96 p.len = grub_le_to_cpu32 (be.size);
97 p.partmap = pmap;
99 if (p.len == 0)
100 continue;
102 if (p.start < delta)
104 #ifdef GRUB_UTIL
105 char *partname;
106 /* disk->partition != NULL as 0 < delta */
107 partname = disk->partition ? grub_partition_get_name (disk->partition)
108 : 0;
109 grub_util_warn (_("Discarding improperly nested partition (%s,%s,%s%d)"),
110 disk->name, partname ? : "", p.partmap->name,
111 p.number + 1);
112 grub_free (partname);
113 #endif
114 continue;
117 p.start -= delta;
119 if (hook (disk, &p))
120 return grub_errno;
122 return GRUB_ERR_NONE;
125 static grub_err_t
126 bsdlabel_partition_map_iterate (grub_disk_t disk,
127 int (*hook) (grub_disk_t disk,
128 const grub_partition_t partition))
131 if (disk->partition && grub_strcmp (disk->partition->partmap->name, "msdos")
132 == 0 && disk->partition->msdostype == GRUB_PC_PARTITION_TYPE_FREEBSD)
133 return iterate_real (disk, GRUB_PC_PARTITION_BSD_LABEL_SECTOR, 1,
134 &grub_bsdlabel_partition_map, hook);
136 if (disk->partition
137 && (grub_strcmp (disk->partition->partmap->name, "msdos") == 0
138 || disk->partition->partmap == &grub_bsdlabel_partition_map
139 || disk->partition->partmap == &grub_netbsdlabel_partition_map
140 || disk->partition->partmap == &grub_openbsdlabel_partition_map))
141 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no embedding supported");
143 return iterate_real (disk, GRUB_PC_PARTITION_BSD_LABEL_SECTOR, 0,
144 &grub_bsdlabel_partition_map, hook);
147 /* This is a total breakage. Even when net-/openbsd label is inside partition
148 it actually describes the whole disk.
150 static grub_err_t
151 netopenbsdlabel_partition_map_iterate (grub_disk_t disk, grub_uint8_t type,
152 struct grub_partition_map *pmap,
153 int (*hook) (grub_disk_t disk,
154 const grub_partition_t partition))
156 int count = 0;
158 auto int check_msdos (grub_disk_t dsk,
159 const grub_partition_t partition);
161 int check_msdos (grub_disk_t dsk,
162 const grub_partition_t partition)
164 grub_err_t err;
166 if (partition->msdostype != type)
167 return 0;
169 err = iterate_real (dsk, partition->start
170 + GRUB_PC_PARTITION_BSD_LABEL_SECTOR, 0, pmap, hook);
171 if (err == GRUB_ERR_NONE)
173 count++;
174 return 1;
176 if (err == GRUB_ERR_BAD_PART_TABLE)
178 grub_errno = GRUB_ERR_NONE;
179 return 0;
181 grub_print_error ();
182 return 0;
185 if (disk->partition && grub_strcmp (disk->partition->partmap->name, "msdos")
186 == 0)
187 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no embedding supported");
190 grub_err_t err;
191 err = grub_partition_msdos_iterate (disk, check_msdos);
193 if (err)
194 return err;
195 if (!count)
196 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no bsdlabel found");
198 return GRUB_ERR_NONE;
201 static grub_err_t
202 netbsdlabel_partition_map_iterate (grub_disk_t disk,
203 int (*hook) (grub_disk_t disk,
204 const grub_partition_t partition))
206 return netopenbsdlabel_partition_map_iterate (disk,
207 GRUB_PC_PARTITION_TYPE_NETBSD,
208 &grub_netbsdlabel_partition_map,
209 hook);
212 static grub_err_t
213 openbsdlabel_partition_map_iterate (grub_disk_t disk,
214 int (*hook) (grub_disk_t disk,
215 const grub_partition_t partition))
217 return netopenbsdlabel_partition_map_iterate (disk,
218 GRUB_PC_PARTITION_TYPE_OPENBSD,
219 &grub_openbsdlabel_partition_map,
220 hook);
224 static struct grub_partition_map grub_bsdlabel_partition_map =
226 .name = "bsd",
227 .iterate = bsdlabel_partition_map_iterate,
230 static struct grub_partition_map grub_openbsdlabel_partition_map =
232 .name = "openbsd",
233 .iterate = openbsdlabel_partition_map_iterate,
236 static struct grub_partition_map grub_netbsdlabel_partition_map =
238 .name = "netbsd",
239 .iterate = netbsdlabel_partition_map_iterate,
244 GRUB_MOD_INIT(part_bsd)
246 grub_partition_map_register (&grub_bsdlabel_partition_map);
247 grub_partition_map_register (&grub_netbsdlabel_partition_map);
248 grub_partition_map_register (&grub_openbsdlabel_partition_map);
251 GRUB_MOD_FINI(part_bsd)
253 grub_partition_map_unregister (&grub_bsdlabel_partition_map);
254 grub_partition_map_unregister (&grub_netbsdlabel_partition_map);
255 grub_partition_map_unregister (&grub_openbsdlabel_partition_map);