Blame view

disk/part_dos.c 8.88 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
fe8c2806c   wdenk   Initial revision
2
3
4
5
  /*
   * (C) Copyright 2001
   * Raymond Lo, lo@routefree.com
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
fe8c2806c   wdenk   Initial revision
6
7
8
9
10
11
12
13
14
15
16
17
18
   */
  
  /*
   * Support for harddisk partitions.
   *
   * To be compatible with LinuxPPC and Apple we use the standard Apple
   * SCSI disk partitioning scheme. For more information see:
   * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
   */
  
  #include <common.h>
  #include <command.h>
  #include <ide.h>
cf92e05c0   Simon Glass   Move ALLOC_CACHE_...
19
  #include <memalign.h>
fe8c2806c   wdenk   Initial revision
20
  #include "part_dos.h"
1811a928c   Adam Ford   Move most CONFIG_...
21
  #ifdef CONFIG_HAVE_BLOCK_DEVICE
fe8c2806c   wdenk   Initial revision
22

4a36be9bd   Darwin Dingel   disk: part_dos.c:...
23
  #define DOS_PART_DEFAULT_SECTOR 512
232e2f4fd   Paul Emge   CVE-2019-13103: d...
24
25
26
  /* should this be configurable? It looks like it's not very common at all
   * to use large numbers of partitions */
  #define MAX_EXT_PARTS 256
fe8c2806c   wdenk   Initial revision
27
28
  /* Convert char[4] in little endian format to the host format integer
   */
d29892ba8   Stefan Monnier   part_dos.c: Don't...
29
  static inline unsigned int le32_to_int(unsigned char *le32)
fe8c2806c   wdenk   Initial revision
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  {
      return ((le32[3] << 24) +
  	    (le32[2] << 16) +
  	    (le32[1] << 8) +
  	     le32[0]
  	   );
  }
  
  static inline int is_extended(int part_type)
  {
      return (part_type == 0x5 ||
  	    part_type == 0xf ||
  	    part_type == 0x85);
  }
40e0e5686   Rob Herring   disk/part: check ...
44
45
  static inline int is_bootable(dos_partition_t *p)
  {
eefa0a647   Andre Przywara   EFI: find EFI sys...
46
  	return (p->sys_ind == 0xef) || (p->boot_ind == 0x80);
40e0e5686   Rob Herring   disk/part: check ...
47
  }
d29892ba8   Stefan Monnier   part_dos.c: Don't...
48
  static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
e2e9b3789   Stephen Warren   disk: part_dos: p...
49
  			   int part_num, unsigned int disksig)
fe8c2806c   wdenk   Initial revision
50
  {
d29892ba8   Stefan Monnier   part_dos.c: Don't...
51
52
  	lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4);
  	lbaint_t lba_size  = le32_to_int (p->size4);
fe8c2806c   wdenk   Initial revision
53

d29892ba8   Stefan Monnier   part_dos.c: Don't...
54
55
56
  	printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
  		"u\t%08x-%02x\t%02x%s%s
  ",
e2e9b3789   Stephen Warren   disk: part_dos: p...
57
  		part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
40e0e5686   Rob Herring   disk/part: check ...
58
59
  		(is_extended(p->sys_ind) ? " Extd" : ""),
  		(is_bootable(p) ? " Boot" : ""));
fe8c2806c   wdenk   Initial revision
60
  }
7205e4075   wdenk   * Patches by Deni...
61
62
  static int test_block_type(unsigned char *buffer)
  {
9d956e0fe   Egbert Eich   disk/part_dos: ch...
63
64
  	int slot;
  	struct dos_partition *p;
34856b0f1   Heinrich Schuchardt   disk: part_dos: c...
65
  	int part_count = 0;
9d956e0fe   Egbert Eich   disk/part_dos: ch...
66

7205e4075   wdenk   * Patches by Deni...
67
68
69
70
  	if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
  	    (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
  		return (-1);
  	} /* no DOS Signature at all */
9d956e0fe   Egbert Eich   disk/part_dos: ch...
71
  	p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
34856b0f1   Heinrich Schuchardt   disk: part_dos: c...
72
73
74
75
76
77
78
  
  	/* Check that the boot indicators are valid and count the partitions. */
  	for (slot = 0; slot < 4; ++slot, ++p) {
  		if (p->boot_ind != 0 && p->boot_ind != 0x80)
  			break;
  		if (p->sys_ind)
  			++part_count;
66c2d73cf   Wolfgang Denk   FAT32: fix suppor...
79
  	}
7205e4075   wdenk   * Patches by Deni...
80

34856b0f1   Heinrich Schuchardt   disk: part_dos: c...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  	/*
  	 * If the partition table is invalid or empty,
  	 * check if this is a DOS PBR
  	 */
  	if (slot != 4 || !part_count) {
  		if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
  			     "FAT", 3) ||
  		    !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
  			     "FAT32", 5))
  			return DOS_PBR; /* This is a DOS PBR and not an MBR */
  	}
  	if (slot == 4)
  		return DOS_MBR;	/* This is an DOS MBR */
  
  	/* This is neither a DOS MBR nor a DOS PBR */
  	return -1;
  }
fe8c2806c   wdenk   Initial revision
98

084bf4c24   Simon Glass   part: Rename test...
99
  static int part_test_dos(struct blk_desc *dev_desc)
fe8c2806c   wdenk   Initial revision
100
  {
3ea052051   Fabio Estevam   disk: part_dos: U...
101
  #ifndef CONFIG_SPL_BUILD
7aed3d380   Faiz Abbas   disk: part_dos: A...
102
103
  	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
  			DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
fe8c2806c   wdenk   Initial revision
104

ff98cb905   Peter Jones   part: extract MBR...
105
  	if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
d1efb6442   Stephen Warren   disk: part_dos: d...
106
  		return -1;
ff98cb905   Peter Jones   part: extract MBR...
107
  	if (test_block_type((unsigned char *)mbr) != DOS_MBR)
d1efb6442   Stephen Warren   disk: part_dos: d...
108
  		return -1;
ff98cb905   Peter Jones   part: extract MBR...
109
110
111
112
113
  	if (dev_desc->sig_type == SIG_TYPE_NONE &&
  	    mbr->unique_mbr_signature != 0) {
  		dev_desc->sig_type = SIG_TYPE_MBR;
  		dev_desc->mbr_sig = mbr->unique_mbr_signature;
  	}
3ea052051   Fabio Estevam   disk: part_dos: U...
114
115
116
117
118
119
120
121
122
  #else
  	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  
  	if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
  		return -1;
  
  	if (test_block_type(buffer) != DOS_MBR)
  		return -1;
  #endif
ff98cb905   Peter Jones   part: extract MBR...
123

d1efb6442   Stephen Warren   disk: part_dos: d...
124
  	return 0;
fe8c2806c   wdenk   Initial revision
125
126
127
128
  }
  
  /*  Print a partition that is relative to its Extended partition table
   */
4101f6879   Simon Glass   dm: Drop the bloc...
129
  static void print_partition_extended(struct blk_desc *dev_desc,
d29892ba8   Stefan Monnier   part_dos.c: Don't...
130
131
  				     lbaint_t ext_part_sector,
  				     lbaint_t relative,
e2e9b3789   Stephen Warren   disk: part_dos: p...
132
  				     int part_num, unsigned int disksig)
fe8c2806c   wdenk   Initial revision
133
  {
dec049d92   Eric Nelson   part_dos: align d...
134
  	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
fe8c2806c   wdenk   Initial revision
135
136
  	dos_partition_t *pt;
  	int i;
232e2f4fd   Paul Emge   CVE-2019-13103: d...
137
138
139
140
141
142
143
  	/* set a maximum recursion level */
  	if (part_num > MAX_EXT_PARTS)
  	{
  		printf("** Nested DOS partitions detected, stopping **
  ");
  		return;
      }
2a981dc2c   Simon Glass   dm: block: Adjust...
144
  	if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
d29892ba8   Stefan Monnier   part_dos.c: Don't...
145
146
  		printf ("** Can't read partition table on %d:" LBAFU " **
  ",
bcce53d04   Simon Glass   dm: block: Rename...
147
  			dev_desc->devnum, ext_part_sector);
fe8c2806c   wdenk   Initial revision
148
149
  		return;
  	}
7205e4075   wdenk   * Patches by Deni...
150
  	i=test_block_type(buffer);
d1efb6442   Stephen Warren   disk: part_dos: d...
151
  	if (i != DOS_MBR) {
fe8c2806c   wdenk   Initial revision
152
153
154
155
156
157
  		printf ("bad MBR sector signature 0x%02x%02x
  ",
  			buffer[DOS_PART_MAGIC_OFFSET],
  			buffer[DOS_PART_MAGIC_OFFSET + 1]);
  		return;
  	}
d1efb6442   Stephen Warren   disk: part_dos: d...
158

e2e9b3789   Stephen Warren   disk: part_dos: p...
159
160
  	if (!ext_part_sector)
  		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
fe8c2806c   wdenk   Initial revision
161
162
163
164
  	/* Print all primary/logical partitions */
  	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  	for (i = 0; i < 4; i++, pt++) {
  		/*
8bde7f776   wdenk   * Code cleanup:
165
  		 * fdisk does not show the extended partitions that
fe8c2806c   wdenk   Initial revision
166
167
168
169
170
  		 * are not in the MBR
  		 */
  
  		if ((pt->sys_ind != 0) &&
  		    (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
e2e9b3789   Stephen Warren   disk: part_dos: p...
171
  			print_one_part(pt, ext_part_sector, part_num, disksig);
fe8c2806c   wdenk   Initial revision
172
173
174
175
176
177
178
179
180
181
182
183
184
  		}
  
  		/* Reverse engr the fdisk part# assignment rule! */
  		if ((ext_part_sector == 0) ||
  		    (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  			part_num++;
  		}
  	}
  
  	/* Follows the extended partitions */
  	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  	for (i = 0; i < 4; i++, pt++) {
  		if (is_extended (pt->sys_ind)) {
d29892ba8   Stefan Monnier   part_dos.c: Don't...
185
186
  			lbaint_t lba_start
  				= le32_to_int (pt->start4) + relative;
fe8c2806c   wdenk   Initial revision
187

304b57113   Stephen Warren   disk: part_dos: c...
188
189
  			print_partition_extended(dev_desc, lba_start,
  				ext_part_sector == 0  ? lba_start : relative,
e2e9b3789   Stephen Warren   disk: part_dos: p...
190
  				part_num, disksig);
fe8c2806c   wdenk   Initial revision
191
192
193
194
195
196
197
198
199
  		}
  	}
  
  	return;
  }
  
  
  /*  Print a partition that is relative to its Extended partition table
   */
3e8bd4695   Simon Glass   dm: part: Rename ...
200
201
202
203
  static int part_get_info_extended(struct blk_desc *dev_desc,
  				  lbaint_t ext_part_sector, lbaint_t relative,
  				  int part_num, int which_part,
  				  disk_partition_t *info, unsigned int disksig)
fe8c2806c   wdenk   Initial revision
204
  {
dec049d92   Eric Nelson   part_dos: align d...
205
  	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
fe8c2806c   wdenk   Initial revision
206
207
  	dos_partition_t *pt;
  	int i;
4a36be9bd   Darwin Dingel   disk: part_dos.c:...
208
  	int dos_type;
fe8c2806c   wdenk   Initial revision
209

232e2f4fd   Paul Emge   CVE-2019-13103: d...
210
211
212
213
214
215
216
  	/* set a maximum recursion level */
  	if (part_num > MAX_EXT_PARTS)
  	{
  		printf("** Nested DOS partitions detected, stopping **
  ");
  		return -1;
      }
2a981dc2c   Simon Glass   dm: block: Adjust...
217
  	if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
d29892ba8   Stefan Monnier   part_dos.c: Don't...
218
219
  		printf ("** Can't read partition table on %d:" LBAFU " **
  ",
bcce53d04   Simon Glass   dm: block: Rename...
220
  			dev_desc->devnum, ext_part_sector);
fe8c2806c   wdenk   Initial revision
221
222
223
224
225
226
227
228
229
230
  		return -1;
  	}
  	if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  		buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  		printf ("bad MBR sector signature 0x%02x%02x
  ",
  			buffer[DOS_PART_MAGIC_OFFSET],
  			buffer[DOS_PART_MAGIC_OFFSET + 1]);
  		return -1;
  	}
b331cd620   Patrick Delaunay   cmd, disk: conver...
231
  #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
d27b5f939   Stephen Warren   disk: part_msdos:...
232
233
234
  	if (!ext_part_sector)
  		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  #endif
fe8c2806c   wdenk   Initial revision
235
236
237
238
  	/* Print all primary/logical partitions */
  	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  	for (i = 0; i < 4; i++, pt++) {
  		/*
8bde7f776   wdenk   * Code cleanup:
239
240
  		 * fdisk does not show the extended partitions that
  		 * are not in the MBR
fe8c2806c   wdenk   Initial revision
241
  		 */
78f4ca797   Daniel Mack   part_dos: check s...
242
243
  		if (((pt->boot_ind & ~0x80) == 0) &&
  		    (pt->sys_ind != 0) &&
7f70e8530   wdenk   * Patch by David ...
244
  		    (part_num == which_part) &&
8f7102cf6   Shawn Guo   disk: part_dos: f...
245
  		    (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
4a36be9bd   Darwin Dingel   disk: part_dos.c:...
246
  			info->blksz = DOS_PART_DEFAULT_SECTOR;
e04350d29   Steve Rae   disk: part_efi: c...
247
248
249
  			info->start = (lbaint_t)(ext_part_sector +
  					le32_to_int(pt->start4));
  			info->size  = (lbaint_t)le32_to_int(pt->size4);
da2ee24d9   Petr Kulhavy   disk: part: refac...
250
251
  			part_set_generic_name(dev_desc, part_num,
  					      (char *)info->name);
fe8c2806c   wdenk   Initial revision
252
  			/* sprintf(info->type, "%d, pt->sys_ind); */
192bc6948   Ben Whitten   Fix GCC format-se...
253
  			strcpy((char *)info->type, "U-Boot");
40e0e5686   Rob Herring   disk/part: check ...
254
  			info->bootable = is_bootable(pt);
b331cd620   Patrick Delaunay   cmd, disk: conver...
255
  #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
d27b5f939   Stephen Warren   disk: part_msdos:...
256
257
  			sprintf(info->uuid, "%08x-%02x", disksig, part_num);
  #endif
f0fb4fa7d   Dalon Westergreen   SPL: add support ...
258
  			info->sys_ind = pt->sys_ind;
fe8c2806c   wdenk   Initial revision
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  			return 0;
  		}
  
  		/* Reverse engr the fdisk part# assignment rule! */
  		if ((ext_part_sector == 0) ||
  		    (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
  			part_num++;
  		}
  	}
  
  	/* Follows the extended partitions */
  	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
  	for (i = 0; i < 4; i++, pt++) {
  		if (is_extended (pt->sys_ind)) {
d29892ba8   Stefan Monnier   part_dos.c: Don't...
273
274
  			lbaint_t lba_start
  				= le32_to_int (pt->start4) + relative;
fe8c2806c   wdenk   Initial revision
275

3e8bd4695   Simon Glass   dm: part: Rename ...
276
  			return part_get_info_extended(dev_desc, lba_start,
fe8c2806c   wdenk   Initial revision
277
  				 ext_part_sector == 0 ? lba_start : relative,
d27b5f939   Stephen Warren   disk: part_msdos:...
278
  				 part_num, which_part, info, disksig);
fe8c2806c   wdenk   Initial revision
279
280
  		}
  	}
4a36be9bd   Darwin Dingel   disk: part_dos.c:...
281
282
283
284
285
286
287
288
289
  
  	/* Check for DOS PBR if no partition is found */
  	dos_type = test_block_type(buffer);
  
  	if (dos_type == DOS_PBR) {
  		info->start = 0;
  		info->size = dev_desc->lba;
  		info->blksz = DOS_PART_DEFAULT_SECTOR;
  		info->bootable = 0;
192bc6948   Ben Whitten   Fix GCC format-se...
290
  		strcpy((char *)info->type, "U-Boot");
b331cd620   Patrick Delaunay   cmd, disk: conver...
291
  #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
4a36be9bd   Darwin Dingel   disk: part_dos.c:...
292
293
294
295
  		info->uuid[0] = 0;
  #endif
  		return 0;
  	}
fe8c2806c   wdenk   Initial revision
296
297
  	return -1;
  }
084bf4c24   Simon Glass   part: Rename test...
298
  void part_print_dos(struct blk_desc *dev_desc)
fe8c2806c   wdenk   Initial revision
299
  {
e2e9b3789   Stephen Warren   disk: part_dos: p...
300
301
302
  	printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType
  ");
  	print_partition_extended(dev_desc, 0, 0, 1, 0);
fe8c2806c   wdenk   Initial revision
303
  }
3e8bd4695   Simon Glass   dm: part: Rename ...
304
305
  int part_get_info_dos(struct blk_desc *dev_desc, int part,
  		      disk_partition_t *info)
fe8c2806c   wdenk   Initial revision
306
  {
3e8bd4695   Simon Glass   dm: part: Rename ...
307
  	return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
fe8c2806c   wdenk   Initial revision
308
  }
b6dd69a4d   Petr Kulhavy   fastboot: add sup...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  int is_valid_dos_buf(void *buf)
  {
  	return test_block_type(buf) == DOS_MBR ? 0 : -1;
  }
  
  int write_mbr_partition(struct blk_desc *dev_desc, void *buf)
  {
  	if (is_valid_dos_buf(buf))
  		return -1;
  
  	/* write MBR */
  	if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
  		printf("%s: failed writing '%s' (1 blks at 0x0)
  ",
  		       __func__, "MBR");
  		return 1;
  	}
  
  	return 0;
  }
96e5b03c8   Simon Glass   dm: part: Convert...
329
330
331
  U_BOOT_PART_TYPE(dos) = {
  	.name		= "DOS",
  	.part_type	= PART_TYPE_DOS,
87b8530fe   Petr Kulhavy   disk: part: imple...
332
  	.max_entries	= DOS_ENTRY_NUMBERS,
3e8bd4695   Simon Glass   dm: part: Rename ...
333
  	.get_info	= part_get_info_ptr(part_get_info_dos),
084bf4c24   Simon Glass   part: Rename test...
334
335
  	.print		= part_print_ptr(part_print_dos),
  	.test		= part_test_dos,
96e5b03c8   Simon Glass   dm: part: Convert...
336
  };
fe8c2806c   wdenk   Initial revision
337

cde5c64d1   Jon Loeliger   disk/: Remove obs...
338
  #endif