2009-06-08 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder.git] / commands / gptsync.c
blob73dafdeb30e738f236aad0b35cf52004d9c3291e
1 /* gptsync.c - fill the mbr based on gpt entries */
2 /* XXX: I don't know what to do if sector size isn't 512 bytes */
3 /*
4 * GRUB -- GRand Unified Bootloader
5 * Copyright (C) 2009 Free Software Foundation, Inc.
7 * GRUB is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GRUB is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/command.h>
22 #include <grub/dl.h>
23 #include <grub/device.h>
24 #include <grub/disk.h>
25 #include <grub/pc_partition.h>
26 #include <grub/partition.h>
27 #include <grub/misc.h>
28 #include <grub/mm.h>
29 #include <grub/fs.h>
31 /* Convert a LBA address to a CHS address in the INT 13 format. */
32 /* Taken from grub1. */
33 /* XXX: use hardcoded geometry of C = 1024, H = 255, S = 63.
34 Is it a problem?
36 static void
37 lba_to_chs (int lba, grub_uint8_t *cl, grub_uint8_t *ch,
38 grub_uint8_t *dh)
40 int cylinder, head, sector;
41 int sectors = 63, heads = 255, cylinders = 1024;
43 sector = lba % sectors + 1;
44 head = (lba / sectors) % heads;
45 cylinder = lba / (sectors * heads);
47 if (cylinder >= cylinders)
49 *cl = *ch = *dh = 0xff;
50 return;
53 *cl = sector | ((cylinder & 0x300) >> 2);
54 *ch = cylinder & 0xFF;
55 *dh = head;
58 static grub_err_t
59 grub_cmd_gptsync (grub_command_t cmd __attribute__ ((unused)),
60 int argc, char **args)
62 grub_device_t dev;
63 struct grub_pc_partition_mbr mbr;
64 struct grub_partition *partition;
65 grub_disk_addr_t first_sector;
66 int numactive = 0;
68 if (argc < 1)
69 return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
70 if (argc > 4)
71 return grub_error (GRUB_ERR_BAD_ARGUMENT, "only 3 partitions can be "
72 "in hybrid MBR");
74 if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
76 args[0][grub_strlen (args[0]) - 1] = 0;
77 dev = grub_device_open (args[0] + 1);
78 args[0][grub_strlen (args[0])] = ')';
80 else
81 dev = grub_device_open (args[0]);
83 if (! dev)
84 return grub_errno;
86 if (! dev->disk)
88 grub_device_close (dev);
89 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
92 /* Read the protective MBR. */
93 if (grub_disk_read (dev->disk, 0, 0, sizeof (mbr), &mbr))
95 grub_device_close (dev);
96 return grub_errno;
99 /* Check if it is valid. */
100 if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
102 grub_device_close (dev);
103 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
106 /* Make sure the MBR is a protective MBR and not a normal MBR. */
107 if (mbr.entries[0].type != GRUB_PC_PARTITION_TYPE_GPT_DISK)
109 grub_device_close (dev);
110 return grub_error (GRUB_ERR_BAD_PART_TABLE, "no GPT partition map found");
113 int i;
114 first_sector = dev->disk->total_sectors;
115 for (i = 1; i < argc; i++)
117 char *separator, csep = 0;
118 grub_uint8_t type;
119 separator = grub_strchr (args[i], '+');
120 if (! separator)
121 separator = grub_strchr (args[i], '-');
122 if (separator)
124 csep = *separator;
125 *separator = 0;
127 partition = grub_partition_probe (dev->disk, args[i]);
128 if (separator)
129 *separator = csep;
130 if (! partition)
132 grub_device_close (dev);
133 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such partition");
136 if (partition->start + partition->len > 0xffffffff)
138 grub_device_close (dev);
139 return grub_error (GRUB_ERR_OUT_OF_RANGE,
140 "only partitions resding in the first 2TB "
141 "can be presen in hybrid MBR");
145 if (first_sector > partition->start)
146 first_sector = partition->start;
148 if (separator && *(separator + 1))
149 type = grub_strtoul (separator + 1, 0, 0);
150 else
152 grub_fs_t fs = 0;
153 dev->disk->partition = partition;
154 fs = grub_fs_probe (dev);
156 /* Unknown filesystem isn't fatal. */
157 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
159 fs = 0;
160 grub_errno = GRUB_ERR_NONE;
163 if (fs && grub_strcmp (fs->name, "ntfs") == 0)
164 type = GRUB_PC_PARTITION_TYPE_NTFS;
165 else if (fs && grub_strcmp (fs->name, "fat") == 0)
166 /* FIXME: detect FAT16. */
167 type = GRUB_PC_PARTITION_TYPE_FAT32_LBA;
168 else if (fs && (grub_strcmp (fs->name, "hfsplus") == 0
169 || grub_strcmp (fs->name, "hfs") == 0))
170 type = GRUB_PC_PARTITION_TYPE_HFS;
171 else
172 /* FIXME: detect more types. */
173 type = GRUB_PC_PARTITION_TYPE_EXT2FS;
175 dev->disk->partition = 0;
178 mbr.entries[i].flag = (csep == '+') ? 0x80 : 0;
179 if (csep == '+')
181 numactive++;
182 if (numactive == 2)
184 grub_device_close (dev);
185 return grub_error (GRUB_ERR_BAD_ARGUMENT,
186 "only one partition can be active");
189 mbr.entries[i].type = type;
190 mbr.entries[i].start = grub_cpu_to_le32 (partition->start);
191 lba_to_chs (partition->start,
192 &(mbr.entries[i].start_sector),
193 &(mbr.entries[i].start_cylinder),
194 &(mbr.entries[i].start_head));
195 lba_to_chs (partition->start + partition->len - 1,
196 &(mbr.entries[i].end_sector),
197 &(mbr.entries[i].end_cylinder),
198 &(mbr.entries[i].end_head));
199 mbr.entries[i].length = grub_cpu_to_le32 (partition->len);
200 grub_free (partition);
202 for (; i < 4; i++)
203 grub_memset (&(mbr.entries[i]), 0, sizeof (mbr.entries[i]));
205 /* The protective partition. */
206 if (first_sector > 0xffffffff)
207 first_sector = 0xffffffff;
208 else
209 first_sector--;
210 mbr.entries[0].flag = 0;
211 mbr.entries[0].type = GRUB_PC_PARTITION_TYPE_GPT_DISK;
212 mbr.entries[0].start = grub_cpu_to_le32 (1);
213 lba_to_chs (1,
214 &(mbr.entries[0].start_sector),
215 &(mbr.entries[0].start_cylinder),
216 &(mbr.entries[0].start_head));
217 lba_to_chs (first_sector,
218 &(mbr.entries[0].end_sector),
219 &(mbr.entries[0].end_cylinder),
220 &(mbr.entries[0].end_head));
221 mbr.entries[0].length = grub_cpu_to_le32 (first_sector);
223 mbr.signature = grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE);
225 if (grub_disk_write (dev->disk, 0, 0, sizeof (mbr), &mbr))
227 grub_device_close (dev);
228 return grub_errno;
231 grub_printf ("New MBR is written to '%s'\n", args[0]);
233 return GRUB_ERR_NONE;
237 static grub_command_t cmd;
239 GRUB_MOD_INIT(gptsync)
241 (void) mod; /* To stop warning. */
242 cmd = grub_register_command ("gptsync", grub_cmd_gptsync,
243 "gptsync DEVICE [PARTITION[+/-[TYPE]]] ...",
244 "Fill hybrid MBR of GPT drive DEVICE. "
245 "specified partitions will be a part "
246 "of hybrid mbr. Up to 3 partitions are "
247 "allowed. TYPE is an MBR type. "
248 "+ means that partition is active. "
249 "Only one partition can be active");
252 GRUB_MOD_FINI(gptsync)
254 grub_unregister_command (cmd);