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