wok diff grub/stuff/gpt.diff @ rev 24987

grub: add gpt support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu May 12 07:08:39 2022 +0000 (2022-05-12)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/grub/stuff/gpt.diff	Thu May 12 07:08:39 2022 +0000
     1.3 @@ -0,0 +1,310 @@
     1.4 +--- grub-0.97/stage2/builtins.c
     1.5 ++++ grub-0.97/stage2/builtins.c
     1.6 +@@ -1233,14 +1233,15 @@ find_func (char *arg, int flags)
     1.7 +   for (drive = 0x80; drive < 0x88; drive++)
     1.8 +     {
     1.9 +       unsigned long part = 0xFFFFFF;
    1.10 +-      unsigned long start, len, offset, ext_offset;
    1.11 +-      int type, entry;
    1.12 ++      unsigned long start, len, offset, ext_offset, gpt_offset;
    1.13 ++      int type, entry, gpt_count, gpt_size;
    1.14 +       char buf[SECTOR_SIZE];
    1.15 + 
    1.16 +       current_drive = drive;
    1.17 +       while (next_partition (drive, 0xFFFFFF, &part, &type,
    1.18 + 			     &start, &len, &offset, &entry,
    1.19 +-			     &ext_offset, buf))
    1.20 ++                            &ext_offset, &gpt_offset,
    1.21 ++                            &gpt_count, &gpt_size, buf))
    1.22 + 	{
    1.23 + 	  if (type != PC_SLICE_TYPE_NONE
    1.24 + 	      && ! IS_PC_SLICE_TYPE_BSD (type)
    1.25 +@@ -2815,8 +2816,8 @@ parttype_func (char *arg, int flags)
    1.26 + {
    1.27 +   int new_type;
    1.28 +   unsigned long part = 0xFFFFFF;
    1.29 +-  unsigned long start, len, offset, ext_offset;
    1.30 +-  int entry, type;
    1.31 ++  unsigned long start, len, offset, ext_offset, gpt_offset;
    1.32 ++  int entry, type, gpt_count, gpt_size;
    1.33 +   char mbr[512];
    1.34 + 
    1.35 +   /* Get the drive and the partition.  */
    1.36 +@@ -2853,7 +2854,14 @@ parttype_func (char *arg, int flags)
    1.37 +   /* Look for the partition.  */
    1.38 +   while (next_partition (current_drive, 0xFFFFFF, &part, &type,
    1.39 + 			 &start, &len, &offset, &entry,
    1.40 +-			 &ext_offset, mbr))
    1.41 ++			 &ext_offset, &gpt_offset, &gpt_count, &gpt_size, mbr))
    1.42 ++	  /* The partition may not be a GPT partition.  */
    1.43 ++	  if (gpt_offset != 0)
    1.44 ++	    {
    1.45 ++		errnum = ERR_BAD_ARGUMENT;
    1.46 ++		return 1;
    1.47 ++	    }
    1.48 ++
    1.49 +     {
    1.50 +       if (part == current_partition)
    1.51 + 	{
    1.52 +--- grub-0.97/stage2/disk_io.c
    1.53 ++++ grub-0.97/stage2/disk_io.c
    1.54 +@@ -21,6 +21,7 @@
    1.55 + 
    1.56 + #include <shared.h>
    1.57 + #include <filesys.h>
    1.58 ++#include <gpt.h>
    1.59 + 
    1.60 + #ifdef SUPPORT_NETBOOT
    1.61 + # define GRUB	1
    1.62 +@@ -502,8 +503,8 @@ int
    1.63 + set_partition_hidden_flag (int hidden)
    1.64 + {
    1.65 +   unsigned long part = 0xFFFFFF;
    1.66 +-  unsigned long start, len, offset, ext_offset;
    1.67 +-  int entry, type;
    1.68 ++  unsigned long start, len, offset, ext_offset, gpt_offset;
    1.69 ++  int entry, type, gpt_count, gpt_size;
    1.70 +   char mbr[512];
    1.71 +   
    1.72 +   /* The drive must be a hard disk.  */
    1.73 +@@ -524,7 +525,14 @@ set_partition_hidden_flag (int hidden)
    1.74 +   /* Look for the partition.  */
    1.75 +   while (next_partition (current_drive, 0xFFFFFF, &part, &type,           
    1.76 + 			 &start, &len, &offset, &entry,
    1.77 +-			 &ext_offset, mbr))
    1.78 ++			 &ext_offset, &gpt_offset, &gpt_count, &gpt_size, mbr))
    1.79 ++	  /* The partition may not be a GPT partition.  */
    1.80 ++	  if (gpt_offset != 0)
    1.81 ++	    {
    1.82 ++		errnum = ERR_BAD_ARGUMENT;
    1.83 ++		return 1;
    1.84 ++	    }
    1.85 ++
    1.86 +     {                                                                       
    1.87 +       if (part == current_partition)
    1.88 + 	{
    1.89 +@@ -577,11 +585,14 @@ next_partition (unsigned long drive, uns
    1.90 + 		unsigned long *partition, int *type,
    1.91 + 		unsigned long *start, unsigned long *len,
    1.92 + 		unsigned long *offset, int *entry,
    1.93 +-		unsigned long *ext_offset, char *buf)
    1.94 ++               unsigned long *ext_offset,
    1.95 ++               unsigned long *gpt_offset, int *gpt_count,
    1.96 ++               int *gpt_size, char *buf)
    1.97 + {
    1.98 +   /* Forward declarations.  */
    1.99 +   auto int next_bsd_partition (void);
   1.100 +   auto int next_pc_slice (void);
   1.101 ++  auto int next_gpt_slice(void);
   1.102 + 
   1.103 +   /* Get next BSD partition in current PC slice.  */
   1.104 +   int next_bsd_partition (void)
   1.105 +@@ -666,6 +677,40 @@ next_partition (unsigned long drive, uns
   1.106 + 	  return 0;
   1.107 + 	}
   1.108 + 
   1.109 ++      /* If this is a GPT partition table, read it as such.  */
   1.110 ++      if (*entry == -1 && *offset == 0 && PC_SLICE_TYPE (buf, 0) == PC_SLICE_TYPE_GPT)
   1.111 ++       {
   1.112 ++         struct grub_gpt_header *hdr = (struct grub_gpt_header *) buf;
   1.113 ++
   1.114 ++         /* Read in the GPT Partition table header.  */
   1.115 ++         if (! rawread (drive, 1, 0, SECTOR_SIZE, buf))
   1.116 ++           return 0;
   1.117 ++
   1.118 ++         if (hdr->magic == GPT_HEADER_MAGIC && hdr->version == 0x10000)
   1.119 ++           {
   1.120 ++             /* Let gpt_offset point to the first entry in the GPT
   1.121 ++                partition table.  This can also be used by callers of
   1.122 ++                next_partition to determine if a entry comes from a
   1.123 ++                GPT partition table or not.  */
   1.124 ++             *gpt_offset = hdr->partitions;
   1.125 ++             *gpt_count = hdr->maxpart;
   1.126 ++             *gpt_size =  hdr->partentry_size;
   1.127 ++             
   1.128 ++             return next_gpt_slice();
   1.129 ++           }
   1.130 ++         else
   1.131 ++           {
   1.132 ++             /* This is not a valid header for a GPT partition table.
   1.133 ++                Re-read the MBR or the boot sector of the extended
   1.134 ++                partition.  */
   1.135 ++             if (! rawread (drive, *offset, 0, SECTOR_SIZE, buf))
   1.136 ++               return 0;
   1.137 ++           }
   1.138 ++       }
   1.139 ++
   1.140 ++      /* Not a GPT partition.  */
   1.141 ++      *gpt_offset = 0;
   1.142 ++
   1.143 +       /* Increase the entry number.  */
   1.144 +       (*entry)++;
   1.145 + 
   1.146 +@@ -710,6 +755,43 @@ next_partition (unsigned long drive, uns
   1.147 +       return 1;
   1.148 +     }
   1.149 + 
   1.150 ++  /* Get the next GPT slice.  */
   1.151 ++  int next_gpt_slice (void)
   1.152 ++    {
   1.153 ++      struct grub_gpt_partentry *gptentry = (struct grub_gpt_partentry *) buf;
   1.154 ++      /* Make GPT partitions show up as PC slices.  */
   1.155 ++      int pc_slice_no = (*partition & 0xFF0000) >> 16;
   1.156 ++
   1.157 ++      /* If this is the first time...  */
   1.158 ++      if (pc_slice_no == 0xFF)
   1.159 ++       {
   1.160 ++         pc_slice_no = -1;
   1.161 ++         *entry = -1;
   1.162 ++       }
   1.163 ++
   1.164 ++      do {
   1.165 ++       (*entry)++;
   1.166 ++
   1.167 ++       if (*entry >= *gpt_count)
   1.168 ++         {
   1.169 ++           errnum = ERR_NO_PART;
   1.170 ++           return 0;
   1.171 ++         }
   1.172 ++       /* Read in the GPT Partition table entry.  */
   1.173 ++       if (! rawread (drive, (*gpt_offset) + GPT_ENTRY_SECTOR (*gpt_size, *entry), GPT_ENTRY_INDEX (*gpt_size, *entry), *gpt_size, buf))
   1.174 ++         return 0;
   1.175 ++      } while (! (gptentry->type1 && gptentry->type2));
   1.176 ++
   1.177 ++      pc_slice_no++;
   1.178 ++      *start = gptentry->start;
   1.179 ++      *len = gptentry->end - gptentry->start + 1;
   1.180 ++      *type = PC_SLICE_TYPE_EXT2FS;
   1.181 ++      *entry = pc_slice_no;
   1.182 ++      *partition = (*entry << 16) | 0xFFFF;
   1.183 ++
   1.184 ++      return 1;
   1.185 ++    }
   1.186 ++
   1.187 +   /* Start the body of this function.  */
   1.188 +   
   1.189 + #ifndef STAGE1_5
   1.190 +@@ -717,6 +799,9 @@ next_partition (unsigned long drive, uns
   1.191 +     return 0;
   1.192 + #endif
   1.193 + 
   1.194 ++  if (*partition != 0xFFFFFF && *gpt_offset != 0)
   1.195 ++    return next_gpt_slice ();
   1.196 ++
   1.197 +   /* If previous partition is a BSD partition or a PC slice which
   1.198 +      contains BSD partitions...  */
   1.199 +   if ((*partition != 0xFFFFFF && IS_PC_SLICE_TYPE_BSD (*type & 0xff))
   1.200 +@@ -755,6 +840,9 @@ real_open_partition (int flags)
   1.201 +   unsigned long dest_partition = current_partition;
   1.202 +   unsigned long part_offset;
   1.203 +   unsigned long ext_offset;
   1.204 ++  unsigned long gpt_offset;
   1.205 ++  int gpt_count;
   1.206 ++  int gpt_size;
   1.207 +   int entry;
   1.208 +   char buf[SECTOR_SIZE];
   1.209 +   int bsd_part, pc_slice;
   1.210 +@@ -766,7 +854,8 @@ real_open_partition (int flags)
   1.211 +       int ret = next_partition (current_drive, dest_partition,
   1.212 + 				&current_partition, &current_slice,
   1.213 + 				&part_start, &part_length,
   1.214 +-				&part_offset, &entry, &ext_offset, buf);
   1.215 ++                               &part_offset, &entry, &ext_offset,
   1.216 ++                               &gpt_offset, &gpt_count, &gpt_size, buf);
   1.217 +       bsd_part = (current_partition >> 8) & 0xFF;
   1.218 +       pc_slice = current_partition >> 16;
   1.219 +       return ret;
   1.220 +--- grub-0.97/stage2/gpt.h
   1.221 ++++ grub-0.97/stage2/gpt.h
   1.222 +@@ -0,0 +1,68 @@
   1.223 ++/*
   1.224 ++ *  GRUB  --  GRand Unified Bootloader
   1.225 ++ *  Copyright (C) 2002,2005,2006   Free Software Foundation, Inc.
   1.226 ++ *
   1.227 ++ *  This program is free software; you can redistribute it and/or modify
   1.228 ++ *  it under the terms of the GNU General Public License as published by
   1.229 ++ *  the Free Software Foundation; either version 2 of the License, or
   1.230 ++ *  (at your option) any later version.
   1.231 ++ *
   1.232 ++ *  This program is distributed in the hope that it will be useful,
   1.233 ++ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   1.234 ++ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   1.235 ++ *  GNU General Public License for more details.
   1.236 ++ *
   1.237 ++ *  You should have received a copy of the GNU General Public License
   1.238 ++ *  along with this program; if not, write to the Free Software
   1.239 ++ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   1.240 ++ */
   1.241 ++
   1.242 ++#ifndef _GPT_H
   1.243 ++#define _GPT_H
   1.244 ++
   1.245 ++typedef signed char grub_int8_t;
   1.246 ++typedef signed short grub_int16_t;
   1.247 ++typedef signed int grub_int32_t;
   1.248 ++typedef signed long long int grub_int64_t;
   1.249 ++typedef unsigned char grub_uint8_t;
   1.250 ++typedef unsigned short grub_uint16_t;
   1.251 ++typedef unsigned int grub_uint32_t;
   1.252 ++typedef unsigned long long int grub_uint64_t;
   1.253 ++
   1.254 ++struct grub_gpt_header
   1.255 ++{
   1.256 ++  grub_uint64_t magic;
   1.257 ++  grub_uint32_t version;
   1.258 ++  grub_uint32_t headersize;
   1.259 ++  grub_uint32_t crc32;
   1.260 ++  grub_uint32_t unused1;
   1.261 ++  grub_uint64_t primary;
   1.262 ++  grub_uint64_t backup;
   1.263 ++  grub_uint64_t start;
   1.264 ++  grub_uint64_t end;
   1.265 ++  grub_uint8_t guid[16];
   1.266 ++  grub_uint64_t partitions;
   1.267 ++  grub_uint32_t maxpart;
   1.268 ++  grub_uint32_t partentry_size;
   1.269 ++  grub_uint32_t partentry_crc32;
   1.270 ++} __attribute__ ((packed));
   1.271 ++
   1.272 ++struct grub_gpt_partentry
   1.273 ++{
   1.274 ++  grub_uint64_t type1;
   1.275 ++  grub_uint64_t type2;
   1.276 ++  grub_uint8_t guid[16];
   1.277 ++  grub_uint64_t start;
   1.278 ++  grub_uint64_t end;
   1.279 ++  grub_uint8_t attrib;
   1.280 ++  char name[72];
   1.281 ++} __attribute__ ((packed));
   1.282 ++
   1.283 ++#define GPT_HEADER_MAGIC       0x5452415020494645UL
   1.284 ++
   1.285 ++#define        GPT_ENTRY_SECTOR(size,entry)                                    \
   1.286 ++       ((((entry) * (size) + 1) & ~(SECTOR_SIZE - 1)) >> SECTOR_BITS)
   1.287 ++#define        GPT_ENTRY_INDEX(size,entry)                                     \
   1.288 ++       ((((entry) * (size) + 1) & (SECTOR_SIZE - 1)) - 1)
   1.289 ++
   1.290 ++#endif /* _GPT_H */
   1.291 +--- grub-0.97/stage2/pc_slice.h
   1.292 ++++ grub-0.97/stage2/pc_slice.h
   1.293 +@@ -115,6 +115,7 @@
   1.294 + #define PC_SLICE_TYPE_LINUX_EXTENDED	0x85
   1.295 + #define PC_SLICE_TYPE_VSTAFS		0x9e
   1.296 + #define PC_SLICE_TYPE_DELL_UTIL		0xde
   1.297 ++#define PC_SLICE_TYPE_GPT              0xee
   1.298 + #define PC_SLICE_TYPE_LINUX_RAID	0xfd
   1.299 + 
   1.300 + 
   1.301 +--- grub-0.97/stage2/shared.h
   1.302 ++++ grub-0.97/stage2/shared.h
   1.303 +@@ -934,7 +934,9 @@ int next_partition (unsigned long drive,
   1.304 + 		    unsigned long *partition, int *type,
   1.305 + 		    unsigned long *start, unsigned long *len,
   1.306 + 		    unsigned long *offset, int *entry,
   1.307 +-		    unsigned long *ext_offset, char *buf);
   1.308 ++                   unsigned long *ext_offset,
   1.309 ++                   unsigned long *gpt_offset, int *gpt_count,
   1.310 ++                   int *gpt_size, char *buf);
   1.311 + 
   1.312 + /* Sets device to the one represented by the SAVED_* parameters. */
   1.313 + int make_saved_active (void);