]>
Commit | Line | Data |
---|---|---|
fe8c2806 WD |
1 | /* |
2 | * (C) Copyright 2001 | |
3 | * Raymond Lo, lo@routefree.com | |
4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
5 | * | |
1a459660 | 6 | * SPDX-License-Identifier: GPL-2.0+ |
fe8c2806 WD |
7 | */ |
8 | ||
9 | /* | |
10 | * Support for harddisk partitions. | |
11 | * | |
12 | * To be compatible with LinuxPPC and Apple we use the standard Apple | |
13 | * SCSI disk partitioning scheme. For more information see: | |
14 | * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92 | |
15 | */ | |
16 | ||
17 | #include <common.h> | |
18 | #include <command.h> | |
19 | #include <ide.h> | |
fe8c2806 WD |
20 | #include "part_dos.h" |
21 | ||
2c1af9dc | 22 | #ifdef HAVE_BLOCK_DEVICE |
fe8c2806 | 23 | |
4a36be9b DD |
24 | #define DOS_PART_DEFAULT_SECTOR 512 |
25 | ||
fe8c2806 WD |
26 | /* Convert char[4] in little endian format to the host format integer |
27 | */ | |
28 | static inline int le32_to_int(unsigned char *le32) | |
29 | { | |
30 | return ((le32[3] << 24) + | |
31 | (le32[2] << 16) + | |
32 | (le32[1] << 8) + | |
33 | le32[0] | |
34 | ); | |
35 | } | |
36 | ||
37 | static inline int is_extended(int part_type) | |
38 | { | |
39 | return (part_type == 0x5 || | |
40 | part_type == 0xf || | |
41 | part_type == 0x85); | |
42 | } | |
43 | ||
40e0e568 RH |
44 | static inline int is_bootable(dos_partition_t *p) |
45 | { | |
46 | return p->boot_ind == 0x80; | |
47 | } | |
48 | ||
304b5711 | 49 | static void print_one_part(dos_partition_t *p, int ext_part_sector, |
e2e9b378 | 50 | int part_num, unsigned int disksig) |
fe8c2806 WD |
51 | { |
52 | int lba_start = ext_part_sector + le32_to_int (p->start4); | |
53 | int lba_size = le32_to_int (p->size4); | |
54 | ||
e2e9b378 SW |
55 | printf("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x%s%s\n", |
56 | part_num, lba_start, lba_size, disksig, part_num, p->sys_ind, | |
40e0e568 RH |
57 | (is_extended(p->sys_ind) ? " Extd" : ""), |
58 | (is_bootable(p) ? " Boot" : "")); | |
fe8c2806 WD |
59 | } |
60 | ||
7205e407 WD |
61 | static int test_block_type(unsigned char *buffer) |
62 | { | |
9d956e0f EE |
63 | int slot; |
64 | struct dos_partition *p; | |
65 | ||
7205e407 WD |
66 | if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) || |
67 | (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) { | |
68 | return (-1); | |
69 | } /* no DOS Signature at all */ | |
9d956e0f EE |
70 | p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET]; |
71 | for (slot = 0; slot < 3; slot++) { | |
72 | if (p->boot_ind != 0 && p->boot_ind != 0x80) { | |
73 | if (!slot && | |
74 | (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET], | |
75 | "FAT", 3) == 0 || | |
76 | strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET], | |
77 | "FAT32", 5) == 0)) { | |
78 | return DOS_PBR; /* is PBR */ | |
79 | } else { | |
80 | return -1; | |
81 | } | |
82 | } | |
66c2d73c | 83 | } |
7205e407 WD |
84 | return DOS_MBR; /* Is MBR */ |
85 | } | |
86 | ||
fe8c2806 | 87 | |
fe8c2806 WD |
88 | int test_part_dos (block_dev_desc_t *dev_desc) |
89 | { | |
dec049d9 | 90 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
fe8c2806 | 91 | |
d1efb644 SW |
92 | if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) |
93 | return -1; | |
94 | ||
95 | if (test_block_type(buffer) != DOS_MBR) | |
96 | return -1; | |
97 | ||
98 | return 0; | |
fe8c2806 WD |
99 | } |
100 | ||
101 | /* Print a partition that is relative to its Extended partition table | |
102 | */ | |
304b5711 SW |
103 | static void print_partition_extended(block_dev_desc_t *dev_desc, |
104 | int ext_part_sector, int relative, | |
e2e9b378 | 105 | int part_num, unsigned int disksig) |
fe8c2806 | 106 | { |
dec049d9 | 107 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
fe8c2806 WD |
108 | dos_partition_t *pt; |
109 | int i; | |
110 | ||
111 | if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { | |
112 | printf ("** Can't read partition table on %d:%d **\n", | |
113 | dev_desc->dev, ext_part_sector); | |
114 | return; | |
115 | } | |
7205e407 | 116 | i=test_block_type(buffer); |
d1efb644 | 117 | if (i != DOS_MBR) { |
fe8c2806 WD |
118 | printf ("bad MBR sector signature 0x%02x%02x\n", |
119 | buffer[DOS_PART_MAGIC_OFFSET], | |
120 | buffer[DOS_PART_MAGIC_OFFSET + 1]); | |
121 | return; | |
122 | } | |
d1efb644 | 123 | |
e2e9b378 SW |
124 | if (!ext_part_sector) |
125 | disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]); | |
126 | ||
fe8c2806 WD |
127 | /* Print all primary/logical partitions */ |
128 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); | |
129 | for (i = 0; i < 4; i++, pt++) { | |
130 | /* | |
8bde7f77 | 131 | * fdisk does not show the extended partitions that |
fe8c2806 WD |
132 | * are not in the MBR |
133 | */ | |
134 | ||
135 | if ((pt->sys_ind != 0) && | |
136 | (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { | |
e2e9b378 | 137 | print_one_part(pt, ext_part_sector, part_num, disksig); |
fe8c2806 WD |
138 | } |
139 | ||
140 | /* Reverse engr the fdisk part# assignment rule! */ | |
141 | if ((ext_part_sector == 0) || | |
142 | (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { | |
143 | part_num++; | |
144 | } | |
145 | } | |
146 | ||
147 | /* Follows the extended partitions */ | |
148 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); | |
149 | for (i = 0; i < 4; i++, pt++) { | |
150 | if (is_extended (pt->sys_ind)) { | |
151 | int lba_start = le32_to_int (pt->start4) + relative; | |
152 | ||
304b5711 SW |
153 | print_partition_extended(dev_desc, lba_start, |
154 | ext_part_sector == 0 ? lba_start : relative, | |
e2e9b378 | 155 | part_num, disksig); |
fe8c2806 WD |
156 | } |
157 | } | |
158 | ||
159 | return; | |
160 | } | |
161 | ||
162 | ||
163 | /* Print a partition that is relative to its Extended partition table | |
164 | */ | |
165 | static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector, | |
166 | int relative, int part_num, | |
d27b5f93 SW |
167 | int which_part, disk_partition_t *info, |
168 | unsigned int disksig) | |
fe8c2806 | 169 | { |
dec049d9 | 170 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
fe8c2806 WD |
171 | dos_partition_t *pt; |
172 | int i; | |
4a36be9b | 173 | int dos_type; |
fe8c2806 WD |
174 | |
175 | if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { | |
176 | printf ("** Can't read partition table on %d:%d **\n", | |
177 | dev_desc->dev, ext_part_sector); | |
178 | return -1; | |
179 | } | |
180 | if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || | |
181 | buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { | |
182 | printf ("bad MBR sector signature 0x%02x%02x\n", | |
183 | buffer[DOS_PART_MAGIC_OFFSET], | |
184 | buffer[DOS_PART_MAGIC_OFFSET + 1]); | |
185 | return -1; | |
186 | } | |
187 | ||
d27b5f93 SW |
188 | #ifdef CONFIG_PARTITION_UUIDS |
189 | if (!ext_part_sector) | |
190 | disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]); | |
191 | #endif | |
192 | ||
fe8c2806 WD |
193 | /* Print all primary/logical partitions */ |
194 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); | |
195 | for (i = 0; i < 4; i++, pt++) { | |
196 | /* | |
8bde7f77 WD |
197 | * fdisk does not show the extended partitions that |
198 | * are not in the MBR | |
fe8c2806 | 199 | */ |
78f4ca79 DM |
200 | if (((pt->boot_ind & ~0x80) == 0) && |
201 | (pt->sys_ind != 0) && | |
7f70e853 WD |
202 | (part_num == which_part) && |
203 | (is_extended(pt->sys_ind) == 0)) { | |
4a36be9b | 204 | info->blksz = DOS_PART_DEFAULT_SECTOR; |
e04350d2 SR |
205 | info->start = (lbaint_t)(ext_part_sector + |
206 | le32_to_int(pt->start4)); | |
207 | info->size = (lbaint_t)le32_to_int(pt->size4); | |
fe8c2806 WD |
208 | switch(dev_desc->if_type) { |
209 | case IF_TYPE_IDE: | |
c7057b52 | 210 | case IF_TYPE_SATA: |
fe8c2806 | 211 | case IF_TYPE_ATAPI: |
71665528 WD |
212 | sprintf ((char *)info->name, "hd%c%d", |
213 | 'a' + dev_desc->dev, part_num); | |
fe8c2806 WD |
214 | break; |
215 | case IF_TYPE_SCSI: | |
71665528 WD |
216 | sprintf ((char *)info->name, "sd%c%d", |
217 | 'a' + dev_desc->dev, part_num); | |
fe8c2806 WD |
218 | break; |
219 | case IF_TYPE_USB: | |
71665528 WD |
220 | sprintf ((char *)info->name, "usbd%c%d", |
221 | 'a' + dev_desc->dev, part_num); | |
fe8c2806 WD |
222 | break; |
223 | case IF_TYPE_DOC: | |
71665528 WD |
224 | sprintf ((char *)info->name, "docd%c%d", |
225 | 'a' + dev_desc->dev, part_num); | |
fe8c2806 WD |
226 | break; |
227 | default: | |
71665528 WD |
228 | sprintf ((char *)info->name, "xx%c%d", |
229 | 'a' + dev_desc->dev, part_num); | |
fe8c2806 WD |
230 | break; |
231 | } | |
232 | /* sprintf(info->type, "%d, pt->sys_ind); */ | |
77ddac94 | 233 | sprintf ((char *)info->type, "U-Boot"); |
40e0e568 | 234 | info->bootable = is_bootable(pt); |
d27b5f93 SW |
235 | #ifdef CONFIG_PARTITION_UUIDS |
236 | sprintf(info->uuid, "%08x-%02x", disksig, part_num); | |
237 | #endif | |
fe8c2806 WD |
238 | return 0; |
239 | } | |
240 | ||
241 | /* Reverse engr the fdisk part# assignment rule! */ | |
242 | if ((ext_part_sector == 0) || | |
243 | (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { | |
244 | part_num++; | |
245 | } | |
246 | } | |
247 | ||
248 | /* Follows the extended partitions */ | |
249 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); | |
250 | for (i = 0; i < 4; i++, pt++) { | |
251 | if (is_extended (pt->sys_ind)) { | |
252 | int lba_start = le32_to_int (pt->start4) + relative; | |
253 | ||
254 | return get_partition_info_extended (dev_desc, lba_start, | |
255 | ext_part_sector == 0 ? lba_start : relative, | |
d27b5f93 | 256 | part_num, which_part, info, disksig); |
fe8c2806 WD |
257 | } |
258 | } | |
4a36be9b DD |
259 | |
260 | /* Check for DOS PBR if no partition is found */ | |
261 | dos_type = test_block_type(buffer); | |
262 | ||
263 | if (dos_type == DOS_PBR) { | |
264 | info->start = 0; | |
265 | info->size = dev_desc->lba; | |
266 | info->blksz = DOS_PART_DEFAULT_SECTOR; | |
267 | info->bootable = 0; | |
268 | sprintf ((char *)info->type, "U-Boot"); | |
269 | #ifdef CONFIG_PARTITION_UUIDS | |
270 | info->uuid[0] = 0; | |
271 | #endif | |
272 | return 0; | |
273 | } | |
274 | ||
fe8c2806 WD |
275 | return -1; |
276 | } | |
277 | ||
278 | void print_part_dos (block_dev_desc_t *dev_desc) | |
279 | { | |
e2e9b378 SW |
280 | printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n"); |
281 | print_partition_extended(dev_desc, 0, 0, 1, 0); | |
fe8c2806 WD |
282 | } |
283 | ||
284 | int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info) | |
285 | { | |
d27b5f93 | 286 | return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0); |
fe8c2806 WD |
287 | } |
288 | ||
289 | ||
cde5c64d | 290 | #endif |