Blame view

disk/part_dos.c 7.08 KB
fe8c2806c   wdenk   Initial revision
1
2
3
4
5
  /*
   * (C) Copyright 2001
   * Raymond Lo, lo@routefree.com
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
6
   * SPDX-License-Identifier:	GPL-2.0+
fe8c2806c   wdenk   Initial revision
7
8
9
10
11
12
13
14
15
16
17
18
19
   */
  
  /*
   * 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>
fe8c2806c   wdenk   Initial revision
20
  #include "part_dos.h"
2c1af9dcd   Stephen Warren   disk: define HAVE...
21
  #ifdef HAVE_BLOCK_DEVICE
fe8c2806c   wdenk   Initial revision
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  
  /* Convert char[4] in little endian format to the host format integer
   */
  static inline int le32_to_int(unsigned char *le32)
  {
      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 ...
40
41
42
43
  static inline int is_bootable(dos_partition_t *p)
  {
  	return p->boot_ind == 0x80;
  }
304b57113   Stephen Warren   disk: part_dos: c...
44
  static void print_one_part(dos_partition_t *p, int ext_part_sector,
e2e9b3789   Stephen Warren   disk: part_dos: p...
45
  			   int part_num, unsigned int disksig)
fe8c2806c   wdenk   Initial revision
46
47
48
  {
  	int lba_start = ext_part_sector + le32_to_int (p->start4);
  	int lba_size  = le32_to_int (p->size4);
e2e9b3789   Stephen Warren   disk: part_dos: p...
49
50
51
  	printf("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x%s%s
  ",
  		part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
40e0e5686   Rob Herring   disk/part: check ...
52
53
  		(is_extended(p->sys_ind) ? " Extd" : ""),
  		(is_bootable(p) ? " Boot" : ""));
fe8c2806c   wdenk   Initial revision
54
  }
7205e4075   wdenk   * Patches by Deni...
55
56
  static int test_block_type(unsigned char *buffer)
  {
9d956e0fe   Egbert Eich   disk/part_dos: ch...
57
58
  	int slot;
  	struct dos_partition *p;
7205e4075   wdenk   * Patches by Deni...
59
60
61
62
  	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...
63
64
65
66
67
68
69
70
71
72
73
74
75
  	p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
  	for (slot = 0; slot < 3; slot++) {
  		if (p->boot_ind != 0 && p->boot_ind != 0x80) {
  			if (!slot &&
  			    (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
  				     "FAT", 3) == 0 ||
  			     strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
  				     "FAT32", 5) == 0)) {
  				return DOS_PBR; /* is PBR */
  			} else {
  				return -1;
  			}
  		}
66c2d73cf   Wolfgang Denk   FAT32: fix suppor...
76
  	}
7205e4075   wdenk   * Patches by Deni...
77
78
  	return DOS_MBR;	    /* Is MBR */
  }
fe8c2806c   wdenk   Initial revision
79

fe8c2806c   wdenk   Initial revision
80
81
  int test_part_dos (block_dev_desc_t *dev_desc)
  {
dec049d92   Eric Nelson   part_dos: align d...
82
  	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
fe8c2806c   wdenk   Initial revision
83

d1efb6442   Stephen Warren   disk: part_dos: d...
84
85
86
87
88
89
90
  	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
  		return -1;
  
  	if (test_block_type(buffer) != DOS_MBR)
  		return -1;
  
  	return 0;
fe8c2806c   wdenk   Initial revision
91
92
93
94
  }
  
  /*  Print a partition that is relative to its Extended partition table
   */
304b57113   Stephen Warren   disk: part_dos: c...
95
96
  static void print_partition_extended(block_dev_desc_t *dev_desc,
  				     int ext_part_sector, int relative,
e2e9b3789   Stephen Warren   disk: part_dos: p...
97
  				     int part_num, unsigned int disksig)
fe8c2806c   wdenk   Initial revision
98
  {
dec049d92   Eric Nelson   part_dos: align d...
99
  	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
fe8c2806c   wdenk   Initial revision
100
101
102
103
104
105
106
107
108
  	dos_partition_t *pt;
  	int i;
  
  	if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
  		printf ("** Can't read partition table on %d:%d **
  ",
  			dev_desc->dev, ext_part_sector);
  		return;
  	}
7205e4075   wdenk   * Patches by Deni...
109
  	i=test_block_type(buffer);
d1efb6442   Stephen Warren   disk: part_dos: d...
110
  	if (i != DOS_MBR) {
fe8c2806c   wdenk   Initial revision
111
112
113
114
115
116
  		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...
117

e2e9b3789   Stephen Warren   disk: part_dos: p...
118
119
  	if (!ext_part_sector)
  		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
fe8c2806c   wdenk   Initial revision
120
121
122
123
  	/* 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:
124
  		 * fdisk does not show the extended partitions that
fe8c2806c   wdenk   Initial revision
125
126
127
128
129
  		 * 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...
130
  			print_one_part(pt, ext_part_sector, part_num, disksig);
fe8c2806c   wdenk   Initial revision
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  		}
  
  		/* 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)) {
  			int lba_start = le32_to_int (pt->start4) + relative;
304b57113   Stephen Warren   disk: part_dos: c...
145
146
  			print_partition_extended(dev_desc, lba_start,
  				ext_part_sector == 0  ? lba_start : relative,
e2e9b3789   Stephen Warren   disk: part_dos: p...
147
  				part_num, disksig);
fe8c2806c   wdenk   Initial revision
148
149
150
151
152
153
154
155
156
157
158
  		}
  	}
  
  	return;
  }
  
  
  /*  Print a partition that is relative to its Extended partition table
   */
  static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
  				 int relative, int part_num,
d27b5f939   Stephen Warren   disk: part_msdos:...
159
160
  				 int which_part, disk_partition_t *info,
  				 unsigned int disksig)
fe8c2806c   wdenk   Initial revision
161
  {
dec049d92   Eric Nelson   part_dos: align d...
162
  	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
fe8c2806c   wdenk   Initial revision
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  	dos_partition_t *pt;
  	int i;
  
  	if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
  		printf ("** Can't read partition table on %d:%d **
  ",
  			dev_desc->dev, ext_part_sector);
  		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;
  	}
d27b5f939   Stephen Warren   disk: part_msdos:...
180
181
182
183
  #ifdef CONFIG_PARTITION_UUIDS
  	if (!ext_part_sector)
  		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
  #endif
fe8c2806c   wdenk   Initial revision
184
185
186
187
  	/* 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:
188
189
  		 * fdisk does not show the extended partitions that
  		 * are not in the MBR
fe8c2806c   wdenk   Initial revision
190
  		 */
78f4ca797   Daniel Mack   part_dos: check s...
191
192
  		if (((pt->boot_ind & ~0x80) == 0) &&
  		    (pt->sys_ind != 0) &&
7f70e8530   wdenk   * Patch by David ...
193
194
  		    (part_num == which_part) &&
  		    (is_extended(pt->sys_ind) == 0)) {
fe8c2806c   wdenk   Initial revision
195
196
197
198
199
  			info->blksz = 512;
  			info->start = ext_part_sector + le32_to_int (pt->start4);
  			info->size  = le32_to_int (pt->size4);
  			switch(dev_desc->if_type) {
  				case IF_TYPE_IDE:
c7057b529   Dave Liu   ata: add the supp...
200
  				case IF_TYPE_SATA:
fe8c2806c   wdenk   Initial revision
201
  				case IF_TYPE_ATAPI:
716655288   Wolfgang Denk   Partition support...
202
203
  					sprintf ((char *)info->name, "hd%c%d",
  						'a' + dev_desc->dev, part_num);
fe8c2806c   wdenk   Initial revision
204
205
  					break;
  				case IF_TYPE_SCSI:
716655288   Wolfgang Denk   Partition support...
206
207
  					sprintf ((char *)info->name, "sd%c%d",
  						'a' + dev_desc->dev, part_num);
fe8c2806c   wdenk   Initial revision
208
209
  					break;
  				case IF_TYPE_USB:
716655288   Wolfgang Denk   Partition support...
210
211
  					sprintf ((char *)info->name, "usbd%c%d",
  						'a' + dev_desc->dev, part_num);
fe8c2806c   wdenk   Initial revision
212
213
  					break;
  				case IF_TYPE_DOC:
716655288   Wolfgang Denk   Partition support...
214
215
  					sprintf ((char *)info->name, "docd%c%d",
  						'a' + dev_desc->dev, part_num);
fe8c2806c   wdenk   Initial revision
216
217
  					break;
  				default:
716655288   Wolfgang Denk   Partition support...
218
219
  					sprintf ((char *)info->name, "xx%c%d",
  						'a' + dev_desc->dev, part_num);
fe8c2806c   wdenk   Initial revision
220
221
222
  					break;
  			}
  			/* sprintf(info->type, "%d, pt->sys_ind); */
77ddac948   Wolfgang Denk   Cleanup for GCC-4.x
223
  			sprintf ((char *)info->type, "U-Boot");
40e0e5686   Rob Herring   disk/part: check ...
224
  			info->bootable = is_bootable(pt);
d27b5f939   Stephen Warren   disk: part_msdos:...
225
226
227
  #ifdef CONFIG_PARTITION_UUIDS
  			sprintf(info->uuid, "%08x-%02x", disksig, part_num);
  #endif
fe8c2806c   wdenk   Initial revision
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  			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)) {
  			int lba_start = le32_to_int (pt->start4) + relative;
  
  			return get_partition_info_extended (dev_desc, lba_start,
  				 ext_part_sector == 0 ? lba_start : relative,
d27b5f939   Stephen Warren   disk: part_msdos:...
246
  				 part_num, which_part, info, disksig);
fe8c2806c   wdenk   Initial revision
247
248
249
250
251
252
253
  		}
  	}
  	return -1;
  }
  
  void print_part_dos (block_dev_desc_t *dev_desc)
  {
e2e9b3789   Stephen Warren   disk: part_dos: p...
254
255
256
  	printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType
  ");
  	print_partition_extended(dev_desc, 0, 0, 1, 0);
fe8c2806c   wdenk   Initial revision
257
258
259
260
  }
  
  int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
  {
d27b5f939   Stephen Warren   disk: part_msdos:...
261
  	return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);
fe8c2806c   wdenk   Initial revision
262
  }
cde5c64d1   Jon Loeliger   disk/: Remove obs...
263
  #endif