efi_loader: type of efi_secure_mode
[oweals/u-boot.git] / disk / part_dos.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001
4  * Raymond Lo, lo@routefree.com
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  */
7
8 /*
9  * Support for harddisk partitions.
10  *
11  * To be compatible with LinuxPPC and Apple we use the standard Apple
12  * SCSI disk partitioning scheme. For more information see:
13  * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
14  */
15
16 #include <common.h>
17 #include <blk.h>
18 #include <command.h>
19 #include <ide.h>
20 #include <memalign.h>
21 #include "part_dos.h"
22 #include <part.h>
23
24 #ifdef CONFIG_HAVE_BLOCK_DEVICE
25
26 #define DOS_PART_DEFAULT_SECTOR 512
27
28 /* should this be configurable? It looks like it's not very common at all
29  * to use large numbers of partitions */
30 #define MAX_EXT_PARTS 256
31
32 /* Convert char[4] in little endian format to the host format integer
33  */
34 static inline unsigned int le32_to_int(unsigned char *le32)
35 {
36     return ((le32[3] << 24) +
37             (le32[2] << 16) +
38             (le32[1] << 8) +
39              le32[0]
40            );
41 }
42
43 static inline int is_extended(int part_type)
44 {
45     return (part_type == 0x5 ||
46             part_type == 0xf ||
47             part_type == 0x85);
48 }
49
50 static int get_bootable(dos_partition_t *p)
51 {
52         int ret = 0;
53
54         if (p->sys_ind == 0xef)
55                 ret |= PART_EFI_SYSTEM_PARTITION;
56         if (p->boot_ind == 0x80)
57                 ret |= PART_BOOTABLE;
58         return ret;
59 }
60
61 static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
62                            int part_num, unsigned int disksig)
63 {
64         lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4);
65         lbaint_t lba_size  = le32_to_int (p->size4);
66
67         printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
68                 "u\t%08x-%02x\t%02x%s%s\n",
69                 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
70                 (is_extended(p->sys_ind) ? " Extd" : ""),
71                 (get_bootable(p) ? " Boot" : ""));
72 }
73
74 static int test_block_type(unsigned char *buffer)
75 {
76         int slot;
77         struct dos_partition *p;
78         int part_count = 0;
79
80         if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
81             (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
82                 return (-1);
83         } /* no DOS Signature at all */
84         p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
85
86         /* Check that the boot indicators are valid and count the partitions. */
87         for (slot = 0; slot < 4; ++slot, ++p) {
88                 if (p->boot_ind != 0 && p->boot_ind != 0x80)
89                         break;
90                 if (p->sys_ind)
91                         ++part_count;
92         }
93
94         /*
95          * If the partition table is invalid or empty,
96          * check if this is a DOS PBR
97          */
98         if (slot != 4 || !part_count) {
99                 if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
100                              "FAT", 3) ||
101                     !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
102                              "FAT32", 5))
103                         return DOS_PBR; /* This is a DOS PBR and not an MBR */
104         }
105         if (slot == 4)
106                 return DOS_MBR; /* This is an DOS MBR */
107
108         /* This is neither a DOS MBR nor a DOS PBR */
109         return -1;
110 }
111
112 static int part_test_dos(struct blk_desc *dev_desc)
113 {
114 #ifndef CONFIG_SPL_BUILD
115         ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
116                         DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
117
118         if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
119                 return -1;
120
121         if (test_block_type((unsigned char *)mbr) != DOS_MBR)
122                 return -1;
123
124         if (dev_desc->sig_type == SIG_TYPE_NONE &&
125             mbr->unique_mbr_signature != 0) {
126                 dev_desc->sig_type = SIG_TYPE_MBR;
127                 dev_desc->mbr_sig = mbr->unique_mbr_signature;
128         }
129 #else
130         ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
131
132         if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
133                 return -1;
134
135         if (test_block_type(buffer) != DOS_MBR)
136                 return -1;
137 #endif
138
139         return 0;
140 }
141
142 /*  Print a partition that is relative to its Extended partition table
143  */
144 static void print_partition_extended(struct blk_desc *dev_desc,
145                                      lbaint_t ext_part_sector,
146                                      lbaint_t relative,
147                                      int part_num, unsigned int disksig)
148 {
149         ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
150         dos_partition_t *pt;
151         int i;
152
153         /* set a maximum recursion level */
154         if (part_num > MAX_EXT_PARTS)
155         {
156                 printf("** Nested DOS partitions detected, stopping **\n");
157                 return;
158     }
159
160         if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
161                 printf ("** Can't read partition table on %d:" LBAFU " **\n",
162                         dev_desc->devnum, ext_part_sector);
163                 return;
164         }
165         i=test_block_type(buffer);
166         if (i != DOS_MBR) {
167                 printf ("bad MBR sector signature 0x%02x%02x\n",
168                         buffer[DOS_PART_MAGIC_OFFSET],
169                         buffer[DOS_PART_MAGIC_OFFSET + 1]);
170                 return;
171         }
172
173         if (!ext_part_sector)
174                 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
175
176         /* Print all primary/logical partitions */
177         pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
178         for (i = 0; i < 4; i++, pt++) {
179                 /*
180                  * fdisk does not show the extended partitions that
181                  * are not in the MBR
182                  */
183
184                 if ((pt->sys_ind != 0) &&
185                     (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
186                         print_one_part(pt, ext_part_sector, part_num, disksig);
187                 }
188
189                 /* Reverse engr the fdisk part# assignment rule! */
190                 if ((ext_part_sector == 0) ||
191                     (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
192                         part_num++;
193                 }
194         }
195
196         /* Follows the extended partitions */
197         pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
198         for (i = 0; i < 4; i++, pt++) {
199                 if (is_extended (pt->sys_ind)) {
200                         lbaint_t lba_start
201                                 = le32_to_int (pt->start4) + relative;
202
203                         print_partition_extended(dev_desc, lba_start,
204                                 ext_part_sector == 0  ? lba_start : relative,
205                                 part_num, disksig);
206                 }
207         }
208
209         return;
210 }
211
212
213 /*  Print a partition that is relative to its Extended partition table
214  */
215 static int part_get_info_extended(struct blk_desc *dev_desc,
216                                   lbaint_t ext_part_sector, lbaint_t relative,
217                                   int part_num, int which_part,
218                                   struct disk_partition *info, uint disksig)
219 {
220         ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
221         dos_partition_t *pt;
222         int i;
223         int dos_type;
224
225         /* set a maximum recursion level */
226         if (part_num > MAX_EXT_PARTS)
227         {
228                 printf("** Nested DOS partitions detected, stopping **\n");
229                 return -1;
230     }
231
232         if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
233                 printf ("** Can't read partition table on %d:" LBAFU " **\n",
234                         dev_desc->devnum, ext_part_sector);
235                 return -1;
236         }
237         if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
238                 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
239                 printf ("bad MBR sector signature 0x%02x%02x\n",
240                         buffer[DOS_PART_MAGIC_OFFSET],
241                         buffer[DOS_PART_MAGIC_OFFSET + 1]);
242                 return -1;
243         }
244
245 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
246         if (!ext_part_sector)
247                 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
248 #endif
249
250         /* Print all primary/logical partitions */
251         pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
252         for (i = 0; i < 4; i++, pt++) {
253                 /*
254                  * fdisk does not show the extended partitions that
255                  * are not in the MBR
256                  */
257                 if (((pt->boot_ind & ~0x80) == 0) &&
258                     (pt->sys_ind != 0) &&
259                     (part_num == which_part) &&
260                     (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
261                         info->blksz = DOS_PART_DEFAULT_SECTOR;
262                         info->start = (lbaint_t)(ext_part_sector +
263                                         le32_to_int(pt->start4));
264                         info->size  = (lbaint_t)le32_to_int(pt->size4);
265                         part_set_generic_name(dev_desc, part_num,
266                                               (char *)info->name);
267                         /* sprintf(info->type, "%d, pt->sys_ind); */
268                         strcpy((char *)info->type, "U-Boot");
269                         info->bootable = get_bootable(pt);
270 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
271                         sprintf(info->uuid, "%08x-%02x", disksig, part_num);
272 #endif
273                         info->sys_ind = pt->sys_ind;
274                         return 0;
275                 }
276
277                 /* Reverse engr the fdisk part# assignment rule! */
278                 if ((ext_part_sector == 0) ||
279                     (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
280                         part_num++;
281                 }
282         }
283
284         /* Follows the extended partitions */
285         pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
286         for (i = 0; i < 4; i++, pt++) {
287                 if (is_extended (pt->sys_ind)) {
288                         lbaint_t lba_start
289                                 = le32_to_int (pt->start4) + relative;
290
291                         return part_get_info_extended(dev_desc, lba_start,
292                                  ext_part_sector == 0 ? lba_start : relative,
293                                  part_num, which_part, info, disksig);
294                 }
295         }
296
297         /* Check for DOS PBR if no partition is found */
298         dos_type = test_block_type(buffer);
299
300         if (dos_type == DOS_PBR) {
301                 info->start = 0;
302                 info->size = dev_desc->lba;
303                 info->blksz = DOS_PART_DEFAULT_SECTOR;
304                 info->bootable = 0;
305                 strcpy((char *)info->type, "U-Boot");
306 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
307                 info->uuid[0] = 0;
308 #endif
309                 return 0;
310         }
311
312         return -1;
313 }
314
315 void part_print_dos(struct blk_desc *dev_desc)
316 {
317         printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
318         print_partition_extended(dev_desc, 0, 0, 1, 0);
319 }
320
321 int part_get_info_dos(struct blk_desc *dev_desc, int part,
322                       struct disk_partition *info)
323 {
324         return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
325 }
326
327 int is_valid_dos_buf(void *buf)
328 {
329         return test_block_type(buf) == DOS_MBR ? 0 : -1;
330 }
331
332 int write_mbr_partition(struct blk_desc *dev_desc, void *buf)
333 {
334         if (is_valid_dos_buf(buf))
335                 return -1;
336
337         /* write MBR */
338         if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
339                 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
340                        __func__, "MBR");
341                 return 1;
342         }
343
344         return 0;
345 }
346
347 U_BOOT_PART_TYPE(dos) = {
348         .name           = "DOS",
349         .part_type      = PART_TYPE_DOS,
350         .max_entries    = DOS_ENTRY_NUMBERS,
351         .get_info       = part_get_info_ptr(part_get_info_dos),
352         .print          = part_print_ptr(part_print_dos),
353         .test           = part_test_dos,
354 };
355
356 #endif