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