Blame view
disk/part_dos.c
8.09 KB
fe8c2806c Initial revision |
1 2 3 4 5 |
/* * (C) Copyright 2001 * Raymond Lo, lo@routefree.com * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * |
1a4596601 Add GPL-2.0+ SPDX... |
6 |
* SPDX-License-Identifier: GPL-2.0+ |
fe8c2806c 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> |
cf92e05c0 Move ALLOC_CACHE_... |
20 |
#include <memalign.h> |
fe8c2806c Initial revision |
21 |
#include "part_dos.h" |
1811a928c Move most CONFIG_... |
22 |
#ifdef CONFIG_HAVE_BLOCK_DEVICE |
fe8c2806c Initial revision |
23 |
|
4a36be9bd disk: part_dos.c:... |
24 |
#define DOS_PART_DEFAULT_SECTOR 512 |
fe8c2806c Initial revision |
25 26 |
/* Convert char[4] in little endian format to the host format integer */ |
d29892ba8 part_dos.c: Don't... |
27 |
static inline unsigned int le32_to_int(unsigned char *le32) |
fe8c2806c Initial revision |
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
{ 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 disk/part: check ... |
42 43 |
static inline int is_bootable(dos_partition_t *p) { |
eefa0a647 EFI: find EFI sys... |
44 |
return (p->sys_ind == 0xef) || (p->boot_ind == 0x80); |
40e0e5686 disk/part: check ... |
45 |
} |
d29892ba8 part_dos.c: Don't... |
46 |
static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector, |
e2e9b3789 disk: part_dos: p... |
47 |
int part_num, unsigned int disksig) |
fe8c2806c Initial revision |
48 |
{ |
d29892ba8 part_dos.c: Don't... |
49 50 |
lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4); lbaint_t lba_size = le32_to_int (p->size4); |
fe8c2806c Initial revision |
51 |
|
d29892ba8 part_dos.c: Don't... |
52 53 54 |
printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength "u\t%08x-%02x\t%02x%s%s ", |
e2e9b3789 disk: part_dos: p... |
55 |
part_num, lba_start, lba_size, disksig, part_num, p->sys_ind, |
40e0e5686 disk/part: check ... |
56 57 |
(is_extended(p->sys_ind) ? " Extd" : ""), (is_bootable(p) ? " Boot" : "")); |
fe8c2806c Initial revision |
58 |
} |
7205e4075 * Patches by Deni... |
59 60 |
static int test_block_type(unsigned char *buffer) { |
9d956e0fe disk/part_dos: ch... |
61 62 |
int slot; struct dos_partition *p; |
7205e4075 * Patches by Deni... |
63 64 65 66 |
if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) || (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) { return (-1); } /* no DOS Signature at all */ |
9d956e0fe disk/part_dos: ch... |
67 68 69 70 71 72 73 74 75 76 77 78 79 |
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 FAT32: fix suppor... |
80 |
} |
7205e4075 * Patches by Deni... |
81 82 |
return DOS_MBR; /* Is MBR */ } |
fe8c2806c Initial revision |
83 |
|
084bf4c24 part: Rename test... |
84 |
static int part_test_dos(struct blk_desc *dev_desc) |
fe8c2806c Initial revision |
85 |
{ |
3ea052051 disk: part_dos: U... |
86 |
#ifndef CONFIG_SPL_BUILD |
8639e34d2 part: Allocate on... |
87 |
ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr, 1); |
fe8c2806c Initial revision |
88 |
|
ff98cb905 part: extract MBR... |
89 |
if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1) |
d1efb6442 disk: part_dos: d... |
90 |
return -1; |
ff98cb905 part: extract MBR... |
91 |
if (test_block_type((unsigned char *)mbr) != DOS_MBR) |
d1efb6442 disk: part_dos: d... |
92 |
return -1; |
ff98cb905 part: extract MBR... |
93 94 95 96 97 |
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 disk: part_dos: U... |
98 99 100 101 102 103 104 105 106 |
#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 part: extract MBR... |
107 |
|
d1efb6442 disk: part_dos: d... |
108 |
return 0; |
fe8c2806c Initial revision |
109 110 111 112 |
} /* Print a partition that is relative to its Extended partition table */ |
4101f6879 dm: Drop the bloc... |
113 |
static void print_partition_extended(struct blk_desc *dev_desc, |
d29892ba8 part_dos.c: Don't... |
114 115 |
lbaint_t ext_part_sector, lbaint_t relative, |
e2e9b3789 disk: part_dos: p... |
116 |
int part_num, unsigned int disksig) |
fe8c2806c Initial revision |
117 |
{ |
dec049d92 part_dos: align d... |
118 |
ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
fe8c2806c Initial revision |
119 120 |
dos_partition_t *pt; int i; |
2a981dc2c dm: block: Adjust... |
121 |
if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) { |
d29892ba8 part_dos.c: Don't... |
122 123 |
printf ("** Can't read partition table on %d:" LBAFU " ** ", |
bcce53d04 dm: block: Rename... |
124 |
dev_desc->devnum, ext_part_sector); |
fe8c2806c Initial revision |
125 126 |
return; } |
7205e4075 * Patches by Deni... |
127 |
i=test_block_type(buffer); |
d1efb6442 disk: part_dos: d... |
128 |
if (i != DOS_MBR) { |
fe8c2806c Initial revision |
129 130 131 132 133 134 |
printf ("bad MBR sector signature 0x%02x%02x ", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); return; } |
d1efb6442 disk: part_dos: d... |
135 |
|
e2e9b3789 disk: part_dos: p... |
136 137 |
if (!ext_part_sector) disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]); |
fe8c2806c Initial revision |
138 139 140 141 |
/* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { /* |
8bde7f776 * Code cleanup: |
142 |
* fdisk does not show the extended partitions that |
fe8c2806c Initial revision |
143 144 145 146 147 |
* are not in the MBR */ if ((pt->sys_ind != 0) && (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { |
e2e9b3789 disk: part_dos: p... |
148 |
print_one_part(pt, ext_part_sector, part_num, disksig); |
fe8c2806c Initial revision |
149 150 151 152 153 154 155 156 157 158 159 160 161 |
} /* 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 part_dos.c: Don't... |
162 163 |
lbaint_t lba_start = le32_to_int (pt->start4) + relative; |
fe8c2806c Initial revision |
164 |
|
304b57113 disk: part_dos: c... |
165 166 |
print_partition_extended(dev_desc, lba_start, ext_part_sector == 0 ? lba_start : relative, |
e2e9b3789 disk: part_dos: p... |
167 |
part_num, disksig); |
fe8c2806c Initial revision |
168 169 170 171 172 173 174 175 176 |
} } return; } /* Print a partition that is relative to its Extended partition table */ |
3e8bd4695 dm: part: Rename ... |
177 178 179 180 |
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 Initial revision |
181 |
{ |
dec049d92 part_dos: align d... |
182 |
ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
fe8c2806c Initial revision |
183 184 |
dos_partition_t *pt; int i; |
4a36be9bd disk: part_dos.c:... |
185 |
int dos_type; |
fe8c2806c Initial revision |
186 |
|
2a981dc2c dm: block: Adjust... |
187 |
if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) { |
d29892ba8 part_dos.c: Don't... |
188 189 |
printf ("** Can't read partition table on %d:" LBAFU " ** ", |
bcce53d04 dm: block: Rename... |
190 |
dev_desc->devnum, ext_part_sector); |
fe8c2806c Initial revision |
191 192 193 194 195 196 197 198 199 200 |
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 cmd, disk: conver... |
201 |
#if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
d27b5f939 disk: part_msdos:... |
202 203 204 |
if (!ext_part_sector) disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]); #endif |
fe8c2806c Initial revision |
205 206 207 208 |
/* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { /* |
8bde7f776 * Code cleanup: |
209 210 |
* fdisk does not show the extended partitions that * are not in the MBR |
fe8c2806c Initial revision |
211 |
*/ |
78f4ca797 part_dos: check s... |
212 213 |
if (((pt->boot_ind & ~0x80) == 0) && (pt->sys_ind != 0) && |
7f70e8530 * Patch by David ... |
214 |
(part_num == which_part) && |
8f7102cf6 disk: part_dos: f... |
215 |
(ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) { |
4a36be9bd disk: part_dos.c:... |
216 |
info->blksz = DOS_PART_DEFAULT_SECTOR; |
e04350d29 disk: part_efi: c... |
217 218 219 |
info->start = (lbaint_t)(ext_part_sector + le32_to_int(pt->start4)); info->size = (lbaint_t)le32_to_int(pt->size4); |
da2ee24d9 disk: part: refac... |
220 221 |
part_set_generic_name(dev_desc, part_num, (char *)info->name); |
fe8c2806c Initial revision |
222 |
/* sprintf(info->type, "%d, pt->sys_ind); */ |
192bc6948 Fix GCC format-se... |
223 |
strcpy((char *)info->type, "U-Boot"); |
40e0e5686 disk/part: check ... |
224 |
info->bootable = is_bootable(pt); |
b331cd620 cmd, disk: conver... |
225 |
#if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
d27b5f939 disk: part_msdos:... |
226 227 |
sprintf(info->uuid, "%08x-%02x", disksig, part_num); #endif |
f0fb4fa7d SPL: add support ... |
228 |
info->sys_ind = pt->sys_ind; |
fe8c2806c Initial revision |
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
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 part_dos.c: Don't... |
243 244 |
lbaint_t lba_start = le32_to_int (pt->start4) + relative; |
fe8c2806c Initial revision |
245 |
|
3e8bd4695 dm: part: Rename ... |
246 |
return part_get_info_extended(dev_desc, lba_start, |
fe8c2806c Initial revision |
247 |
ext_part_sector == 0 ? lba_start : relative, |
d27b5f939 disk: part_msdos:... |
248 |
part_num, which_part, info, disksig); |
fe8c2806c Initial revision |
249 250 |
} } |
4a36be9bd disk: part_dos.c:... |
251 252 253 254 255 256 257 258 259 |
/* 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 Fix GCC format-se... |
260 |
strcpy((char *)info->type, "U-Boot"); |
b331cd620 cmd, disk: conver... |
261 |
#if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
4a36be9bd disk: part_dos.c:... |
262 263 264 265 |
info->uuid[0] = 0; #endif return 0; } |
fe8c2806c Initial revision |
266 267 |
return -1; } |
084bf4c24 part: Rename test... |
268 |
void part_print_dos(struct blk_desc *dev_desc) |
fe8c2806c Initial revision |
269 |
{ |
e2e9b3789 disk: part_dos: p... |
270 271 272 |
printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType "); print_partition_extended(dev_desc, 0, 0, 1, 0); |
fe8c2806c Initial revision |
273 |
} |
3e8bd4695 dm: part: Rename ... |
274 275 |
int part_get_info_dos(struct blk_desc *dev_desc, int part, disk_partition_t *info) |
fe8c2806c Initial revision |
276 |
{ |
3e8bd4695 dm: part: Rename ... |
277 |
return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0); |
fe8c2806c Initial revision |
278 |
} |
b6dd69a4d fastboot: add sup... |
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
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 dm: part: Convert... |
299 300 301 |
U_BOOT_PART_TYPE(dos) = { .name = "DOS", .part_type = PART_TYPE_DOS, |
87b8530fe disk: part: imple... |
302 |
.max_entries = DOS_ENTRY_NUMBERS, |
3e8bd4695 dm: part: Rename ... |
303 |
.get_info = part_get_info_ptr(part_get_info_dos), |
084bf4c24 part: Rename test... |
304 305 |
.print = part_print_ptr(part_print_dos), .test = part_test_dos, |
96e5b03c8 dm: part: Convert... |
306 |
}; |
fe8c2806c Initial revision |
307 |
|
cde5c64d1 disk/: Remove obs... |
308 |
#endif |