~cjwatson/grub/cache-coherency-intrepid

« back to all changes in this revision

Viewing changes to debian/patches/gpt.diff

  • Committer: Steve Langasek
  • Date: 2008-01-28 18:41:45 UTC
  • Revision ID: steve.langasek@canonical.com-20080128184145-cf48vaw3vfoviy0r-svn4-upgrade
Tags: 0.97-29ubuntu4
importĀ grubĀ 0.97-29ubunt4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Hi,
 
2
 
 
3
Here is some patch I wrote because of my work for XenSource as part of
 
4
Summer of Code.  I understand that it will not be included in GRUB
 
5
Legacy CVS, but I hope this will be useful for at least some people or
 
6
distributions.
 
7
 
 
8
This patch adds support for GPT partitions.  When a protective MBR is
 
9
detected, it reads the GPT partitions instead of the MBR partitions.
 
10
This can be used to use GRUB Legacy using the Legacy Firmware of the
 
11
Intel Mac to boot into GNU/Linux without changing the protective MBR.
 
12
 
 
13
If someone is interested in this patch I will be happy to talk about
 
14
improvements.  For example, one thing I have in mind is the abuse of
 
15
the protective MBR to install windows.  My patch disables this ability
 
16
of abuse but also the possibility to chainload windows (I assume).
 
17
 
 
18
Please do not interpret this email as a personal interest to work on
 
19
GRUB Legacy, I will remain working on GRUB 2 like I used to.
 
20
 
 
21
--
 
22
Marco
 
23
 
 
24
 
 
25
 
 
26
2006-07-25  Marco Gerards  <mgerards@xs4all.nl>
 
27
 
 
28
        * stage2/disk_io.c: Include <gpt.h>.
 
29
        (next_partition):  Added the `gpt_offset', `gpt_count' and
 
30
        `gpt_size' arguments.  If the MBR is a Protective MBR, use the GPT
 
31
        partition table.  Added the local function `int next_gpt_slice'.
 
32
        Updated all callers.
 
33
 
 
34
        * stage2/shared.h (next_partition): Added the `gpt_offset',
 
35
        `gpt_count' and `gpt_size' arguments.
 
36
 
 
37
        * stage2/pc_slice.h (PC_SLICE_TYPE_GPT): New macro.
 
38
 
 
39
        * stage2/gpt.h: New file.
 
40
 
 
41
 
 
42
 
 
43
Index: stage2/builtins.c
 
44
===================================================================
 
45
RCS file: /sources/grub/grub/stage2/builtins.c,v
 
46
retrieving revision 1.152
 
47
diff -u -p -u -p -r1.152 builtins.c
 
48
--- grub-0.97/stage2/builtins.c 21 Mar 2006 20:51:58 -0000      1.152
 
49
+++ grub-0.97/stage2/builtins.c 25 Jul 2006 12:32:34 -0000
 
50
@@ -1233,14 +1233,15 @@ find_func (char *arg, int flags)
 
51
   for (drive = 0x80; drive < 0x88; drive++)
 
52
     {
 
53
       unsigned long part = 0xFFFFFF;
 
54
-      unsigned long start, len, offset, ext_offset;
 
55
-      int type, entry;
 
56
+      unsigned long start, len, offset, ext_offset, gpt_offset;
 
57
+      int type, entry, gpt_count, gpt_size;
 
58
       char buf[SECTOR_SIZE];
 
59
 
 
60
       current_drive = drive;
 
61
       while (next_partition (drive, 0xFFFFFF, &part, &type,
 
62
                             &start, &len, &offset, &entry,
 
63
-                            &ext_offset, buf))
 
64
+                            &ext_offset, &gpt_offset,
 
65
+                            &gpt_count, &gpt_size, buf))
 
66
        {
 
67
          if (type != PC_SLICE_TYPE_NONE
 
68
              && ! IS_PC_SLICE_TYPE_BSD (type)
 
69
@@ -2815,8 +2816,8 @@ parttype_func (char *arg, int flags)
 
70
 {
 
71
   int new_type;
 
72
   unsigned long part = 0xFFFFFF;
 
73
-  unsigned long start, len, offset, ext_offset;
 
74
-  int entry, type;
 
75
+  unsigned long start, len, offset, ext_offset, gpt_offset;
 
76
+  int entry, type, gpt_count, gpt_size;
 
77
   char mbr[512];
 
78
 
 
79
   /* Get the drive and the partition.  */
 
80
@@ -2853,8 +2854,15 @@ parttype_func (char *arg, int flags)
 
81
   /* Look for the partition.  */
 
82
   while (next_partition (current_drive, 0xFFFFFF, &part, &type,
 
83
                         &start, &len, &offset, &entry,
 
84
-                        &ext_offset, mbr))
 
85
+                        &ext_offset, &gpt_offset, &gpt_count, &gpt_size, mbr))
 
86
     {
 
87
+      /* The partition may not be a GPT partition.  */
 
88
+      if (gpt_offset != 0)
 
89
+       {
 
90
+         errnum = ERR_BAD_ARGUMENT;
 
91
+         return 1;
 
92
+       }
 
93
+
 
94
       if (part == current_partition)
 
95
        {
 
96
          /* Found.  */
 
97
Index: stage2/disk_io.c
 
98
===================================================================
 
99
RCS file: /sources/grub/grub/stage2/disk_io.c,v
 
100
retrieving revision 1.58
 
101
diff -u -p -u -p -r1.58 disk_io.c
 
102
--- grub-0.97/stage2/disk_io.c  23 May 2004 16:45:45 -0000      1.58
 
103
+++ grub-0.97/stage2/disk_io.c  25 Jul 2006 12:32:35 -0000
 
104
@@ -21,6 +21,7 @@
 
105
 
 
106
 #include <shared.h>
 
107
 #include <filesys.h>
 
108
+#include <gpt.h>
 
109
 
 
110
 #ifdef SUPPORT_NETBOOT
 
111
 # define GRUB  1
 
112
@@ -502,8 +503,8 @@ int
 
113
 set_partition_hidden_flag (int hidden)
 
114
 {
 
115
   unsigned long part = 0xFFFFFF;
 
116
-  unsigned long start, len, offset, ext_offset;
 
117
-  int entry, type;
 
118
+  unsigned long start, len, offset, ext_offset, gpt_offset;
 
119
+  int entry, type, gpt_count, gpt_size;
 
120
   char mbr[512];
 
121
   
 
122
   /* The drive must be a hard disk.  */
 
123
@@ -524,8 +525,16 @@ set_partition_hidden_flag (int hidden)
 
124
   /* Look for the partition.  */
 
125
   while (next_partition (current_drive, 0xFFFFFF, &part, &type,           
 
126
                         &start, &len, &offset, &entry,
 
127
-                        &ext_offset, mbr))
 
128
+                        &ext_offset, &gpt_offset, &gpt_count, &gpt_size, mbr))
 
129
     {                                                                       
 
130
+      /* The partition may not be a GPT partition.  */
 
131
+      if (gpt_offset != 0)
 
132
+       {
 
133
+         errnum = ERR_BAD_ARGUMENT;
 
134
+         return 1;
 
135
+       }
 
136
+
 
137
+
 
138
       if (part == current_partition)
 
139
        {
 
140
          /* Found.  */
 
141
@@ -577,11 +586,14 @@ next_partition (unsigned long drive, uns
 
142
                unsigned long *partition, int *type,
 
143
                unsigned long *start, unsigned long *len,
 
144
                unsigned long *offset, int *entry,
 
145
-               unsigned long *ext_offset, char *buf)
 
146
+               unsigned long *ext_offset,
 
147
+               unsigned long *gpt_offset, int *gpt_count,
 
148
+               int *gpt_size, char *buf)
 
149
 {
 
150
   /* Forward declarations.  */
 
151
   auto int next_bsd_partition (void);
 
152
   auto int next_pc_slice (void);
 
153
+  auto int next_gpt_slice (void);
 
154
 
 
155
   /* Get next BSD partition in current PC slice.  */
 
156
   int next_bsd_partition (void)
 
157
@@ -666,6 +678,40 @@ next_partition (unsigned long drive, uns
 
158
          return 0;
 
159
        }
 
160
 
 
161
+      /* If this is a GPT partition table, read it as such.  */
 
162
+      if (*entry == -1 && *offset == 0 && PC_SLICE_TYPE (buf, 0) == PC_SLICE_TYPE_GPT)
 
163
+       {
 
164
+         struct grub_gpt_header *hdr = (struct grub_gpt_header *) buf;
 
165
+
 
166
+         /* Read in the GPT Partition table header.  */
 
167
+         if (! rawread (drive, 1, 0, SECTOR_SIZE, buf))
 
168
+           return 0;
 
169
+
 
170
+         if (hdr->magic == GPT_HEADER_MAGIC && hdr->version == 0x10000)
 
171
+           {
 
172
+             /* Let gpt_offset point to the first entry in the GPT
 
173
+                partition table.  This can also be used by callers of
 
174
+                next_partition to determine if a entry comes from a
 
175
+                GPT partition table or not.  */
 
176
+             *gpt_offset = hdr->partitions;
 
177
+             *gpt_count = hdr->maxpart;
 
178
+             *gpt_size =  hdr->partentry_size;
 
179
+             
 
180
+             return next_gpt_slice();
 
181
+           }
 
182
+         else
 
183
+           {
 
184
+             /* This is not a valid header for a GPT partition table.
 
185
+                Re-read the MBR or the boot sector of the extended
 
186
+                partition.  */
 
187
+             if (! rawread (drive, *offset, 0, SECTOR_SIZE, buf))
 
188
+               return 0;
 
189
+           }
 
190
+       }
 
191
+
 
192
+      /* Not a GPT partition.  */
 
193
+      *gpt_offset = 0;
 
194
+
 
195
       /* Increase the entry number.  */
 
196
       (*entry)++;
 
197
 
 
198
@@ -710,6 +756,43 @@ next_partition (unsigned long drive, uns
 
199
       return 1;
 
200
     }
 
201
 
 
202
+  /* Get the next GPT slice.  */
 
203
+  int next_gpt_slice (void)
 
204
+    {
 
205
+      struct grub_gpt_partentry *gptentry = (struct grub_gpt_partentry *) buf;
 
206
+      /* Make GPT partitions show up as PC slices.  */
 
207
+      int pc_slice_no = (*partition & 0xFF0000) >> 16;
 
208
+
 
209
+      /* If this is the first time...  */
 
210
+      if (pc_slice_no == 0xFF)
 
211
+       {
 
212
+         pc_slice_no = -1;
 
213
+         *entry = -1;
 
214
+       }
 
215
+
 
216
+      do {
 
217
+       (*entry)++;
 
218
+
 
219
+       if (*entry >= *gpt_count)
 
220
+         {
 
221
+           errnum = ERR_NO_PART;
 
222
+           return 0;
 
223
+         }
 
224
+       /* Read in the GPT Partition table entry.  */
 
225
+       if (! rawread (drive, (*gpt_offset) + GPT_ENTRY_SECTOR (*gpt_size, *entry), GPT_ENTRY_INDEX (*gpt_size, *entry), *gpt_size, buf))
 
226
+         return 0;
 
227
+      } while (! (gptentry->type1 && gptentry->type2));
 
228
+
 
229
+      pc_slice_no++;
 
230
+      *start = gptentry->start;
 
231
+      *len = gptentry->end - gptentry->start + 1;
 
232
+      *type = PC_SLICE_TYPE_EXT2FS;
 
233
+      *entry = pc_slice_no;
 
234
+      *partition = (*entry << 16) | 0xFFFF;
 
235
+
 
236
+      return 1;
 
237
+    }
 
238
+
 
239
   /* Start the body of this function.  */
 
240
   
 
241
 #ifndef STAGE1_5
 
242
@@ -717,6 +800,9 @@ next_partition (unsigned long drive, uns
 
243
     return 0;
 
244
 #endif
 
245
 
 
246
+  if (*partition != 0xFFFFFF && *gpt_offset != 0)
 
247
+    return next_gpt_slice ();
 
248
+
 
249
   /* If previous partition is a BSD partition or a PC slice which
 
250
      contains BSD partitions...  */
 
251
   if ((*partition != 0xFFFFFF && IS_PC_SLICE_TYPE_BSD (*type & 0xff))
 
252
@@ -755,6 +841,9 @@ real_open_partition (int flags)
 
253
   unsigned long dest_partition = current_partition;
 
254
   unsigned long part_offset;
 
255
   unsigned long ext_offset;
 
256
+  unsigned long gpt_offset;
 
257
+  int gpt_count;
 
258
+  int gpt_size;
 
259
   int entry;
 
260
   char buf[SECTOR_SIZE];
 
261
   int bsd_part, pc_slice;
 
262
@@ -766,7 +855,8 @@ real_open_partition (int flags)
 
263
       int ret = next_partition (current_drive, dest_partition,
 
264
                                &current_partition, &current_slice,
 
265
                                &part_start, &part_length,
 
266
-                               &part_offset, &entry, &ext_offset, buf);
 
267
+                               &part_offset, &entry, &ext_offset,
 
268
+                               &gpt_offset, &gpt_count, &gpt_size, buf);
 
269
       bsd_part = (current_partition >> 8) & 0xFF;
 
270
       pc_slice = current_partition >> 16;
 
271
       return ret;
 
272
Index: stage2/gpt.h
 
273
===================================================================
 
274
RCS file: stage2/gpt.h
 
275
diff -N stage2/gpt.h
 
276
--- /dev/null   1 Jan 1970 00:00:00 -0000
 
277
+++ grub-0.97/stage2/gpt.h      25 Jul 2006 12:32:36 -0000
 
278
@@ -0,0 +1,68 @@
 
279
+/*
 
280
+ *  GRUB  --  GRand Unified Bootloader
 
281
+ *  Copyright (C) 2002,2005,2006   Free Software Foundation, Inc.
 
282
+ *
 
283
+ *  This program is free software; you can redistribute it and/or modify
 
284
+ *  it under the terms of the GNU General Public License as published by
 
285
+ *  the Free Software Foundation; either version 2 of the License, or
 
286
+ *  (at your option) any later version.
 
287
+ *
 
288
+ *  This program is distributed in the hope that it will be useful,
 
289
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
290
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
291
+ *  GNU General Public License for more details.
 
292
+ *
 
293
+ *  You should have received a copy of the GNU General Public License
 
294
+ *  along with this program; if not, write to the Free Software
 
295
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
296
+ */
 
297
+
 
298
+#ifndef _GPT_H
 
299
+#define _GPT_H
 
300
+
 
301
+typedef signed char grub_int8_t;
 
302
+typedef signed short grub_int16_t;
 
303
+typedef signed int grub_int32_t;
 
304
+typedef signed long long int grub_int64_t;
 
305
+typedef unsigned char grub_uint8_t;
 
306
+typedef unsigned short grub_uint16_t;
 
307
+typedef unsigned int grub_uint32_t;
 
308
+typedef unsigned long long int grub_uint64_t;
 
309
+
 
310
+struct grub_gpt_header
 
311
+{
 
312
+  grub_uint64_t magic;
 
313
+  grub_uint32_t version;
 
314
+  grub_uint32_t headersize;
 
315
+  grub_uint32_t crc32;
 
316
+  grub_uint32_t unused1;
 
317
+  grub_uint64_t primary;
 
318
+  grub_uint64_t backup;
 
319
+  grub_uint64_t start;
 
320
+  grub_uint64_t end;
 
321
+  grub_uint8_t guid[16];
 
322
+  grub_uint64_t partitions;
 
323
+  grub_uint32_t maxpart;
 
324
+  grub_uint32_t partentry_size;
 
325
+  grub_uint32_t partentry_crc32;
 
326
+} __attribute__ ((packed));
 
327
+
 
328
+struct grub_gpt_partentry
 
329
+{
 
330
+  grub_uint64_t type1;
 
331
+  grub_uint64_t type2;
 
332
+  grub_uint8_t guid[16];
 
333
+  grub_uint64_t start;
 
334
+  grub_uint64_t end;
 
335
+  grub_uint8_t attrib;
 
336
+  char name[72];
 
337
+} __attribute__ ((packed));
 
338
+
 
339
+#define GPT_HEADER_MAGIC       0x5452415020494645UL
 
340
+
 
341
+#define        GPT_ENTRY_SECTOR(size,entry)                                    \
 
342
+       ((((entry) * (size) + 1) & ~(SECTOR_SIZE - 1)) >> SECTOR_BITS)
 
343
+#define        GPT_ENTRY_INDEX(size,entry)                                     \
 
344
+       ((((entry) * (size) + 1) & (SECTOR_SIZE - 1)) - 1)
 
345
+
 
346
+#endif /* _GPT_H */
 
347
Index: stage2/pc_slice.h
 
348
===================================================================
 
349
RCS file: /sources/grub/grub/stage2/pc_slice.h,v
 
350
retrieving revision 1.16
 
351
diff -u -p -u -p -r1.16 pc_slice.h
 
352
--- grub-0.97/stage2/pc_slice.h 24 Jan 2003 16:28:41 -0000      1.16
 
353
+++ grub-0.97/stage2/pc_slice.h 25 Jul 2006 12:32:36 -0000
 
354
@@ -115,6 +115,7 @@
 
355
 #define PC_SLICE_TYPE_LINUX_EXTENDED   0x85
 
356
 #define PC_SLICE_TYPE_VSTAFS           0x9e
 
357
 #define PC_SLICE_TYPE_DELL_UTIL                0xde
 
358
+#define PC_SLICE_TYPE_GPT              0xee
 
359
 #define PC_SLICE_TYPE_LINUX_RAID       0xfd
 
360
 
 
361
 
 
362
Index: stage2/shared.h
 
363
===================================================================
 
364
RCS file: /sources/grub/grub/stage2/shared.h,v
 
365
retrieving revision 1.100
 
366
diff -u -p -u -p -r1.100 shared.h
 
367
--- grub-0.97/stage2/shared.h   2 May 2006 20:46:24 -0000       1.100
 
368
+++ grub-0.97/stage2/shared.h   25 Jul 2006 12:32:36 -0000
 
369
@@ -934,7 +934,9 @@ int next_partition (unsigned long drive,
 
370
                    unsigned long *partition, int *type,
 
371
                    unsigned long *start, unsigned long *len,
 
372
                    unsigned long *offset, int *entry,
 
373
-                   unsigned long *ext_offset, char *buf);
 
374
+                   unsigned long *ext_offset,
 
375
+                   unsigned long *gpt_offset, int *gpt_count,
 
376
+                   int *gpt_size, char *buf);
 
377
 
 
378
 /* Sets device to the one represented by the SAVED_* parameters. */
 
379
 int make_saved_active (void);