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