Commit fae2bf22a2b1aee85734fc2643ac6ede88cbbd01

Authored by Chang Hyun Park
Committed by Tom Rini
1 parent 36f2e8e0d1

gpt: The leXX_to_int() calls replaced with ones defined at <compiler.h>

Custom definitions of le_XX_to_int functions have been replaced with
standard ones, defined at <compiler.h>

Replacement of several GPT related structures members with ones
indicating its endianness and proper size.

Signed-off-by: Chang Hyun Park <heartinpiece@outlook.com>
Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>

Showing 2 changed files with 88 additions and 114 deletions Side-by-side Diff

... ... @@ -34,7 +34,7 @@
34 34 #include <command.h>
35 35 #include <ide.h>
36 36 #include <malloc.h>
37   -#include "part_efi.h"
  37 +#include <part_efi.h>
38 38 #include <linux/ctype.h>
39 39  
40 40 #if defined(CONFIG_CMD_IDE) || \
... ... @@ -44,34 +44,6 @@
44 44 defined(CONFIG_MMC) || \
45 45 defined(CONFIG_SYSTEMACE)
46 46  
47   -/* Convert char[2] in little endian format to the host format integer
48   - */
49   -static inline unsigned short le16_to_int(unsigned char *le16)
50   -{
51   - return ((le16[1] << 8) + le16[0]);
52   -}
53   -
54   -/* Convert char[4] in little endian format to the host format integer
55   - */
56   -static inline unsigned long le32_to_int(unsigned char *le32)
57   -{
58   - return ((le32[3] << 24) + (le32[2] << 16) + (le32[1] << 8) + le32[0]);
59   -}
60   -
61   -/* Convert char[8] in little endian format to the host format integer
62   - */
63   -static inline unsigned long long le64_to_int(unsigned char *le64)
64   -{
65   - return (((unsigned long long)le64[7] << 56) +
66   - ((unsigned long long)le64[6] << 48) +
67   - ((unsigned long long)le64[5] << 40) +
68   - ((unsigned long long)le64[4] << 32) +
69   - ((unsigned long long)le64[3] << 24) +
70   - ((unsigned long long)le64[2] << 16) +
71   - ((unsigned long long)le64[1] << 8) +
72   - (unsigned long long)le64[0]);
73   -}
74   -
75 47 /**
76 48 * efi_crc32() - EFI version of crc32 function
77 49 * @buf: buffer to calculate crc32 of
... ... @@ -79,7 +51,7 @@
79 51 *
80 52 * Description: Returns EFI-style CRC32 value for @buf
81 53 */
82   -static inline unsigned long efi_crc32(const void *buf, unsigned long len)
  54 +static inline u32 efi_crc32(const void *buf, u32 len)
83 55 {
84 56 return crc32(0, buf, len);
85 57 }
86 58  
... ... @@ -171,14 +143,14 @@
171 143 printf("\tType UUID\n");
172 144 printf("\tPartition UUID\n");
173 145  
174   - for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) {
  146 + for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
175 147 /* Stop at the first non valid PTE */
176 148 if (!is_pte_valid(&gpt_pte[i]))
177 149 break;
178 150  
179 151 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
180   - le64_to_int(gpt_pte[i].starting_lba),
181   - le64_to_int(gpt_pte[i].ending_lba),
  152 + le64_to_cpu(gpt_pte[i].starting_lba),
  153 + le64_to_cpu(gpt_pte[i].ending_lba),
182 154 print_efiname(&gpt_pte[i]));
183 155 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
184 156 uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
... ... @@ -211,7 +183,7 @@
211 183 return -1;
212 184 }
213 185  
214   - if (part > le32_to_int(gpt_head->num_partition_entries) ||
  186 + if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
215 187 !is_pte_valid(&gpt_pte[part - 1])) {
216 188 printf("%s: *** ERROR: Invalid partition number %d ***\n",
217 189 __func__, part);
218 190  
... ... @@ -219,9 +191,9 @@
219 191 }
220 192  
221 193 /* The ulong casting limits the maximum disk size to 2 TB */
222   - info->start = (ulong) le64_to_int(gpt_pte[part - 1].starting_lba);
  194 + info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
223 195 /* The ending LBA is inclusive, to calculate size, add 1 to it */
224   - info->size = ((ulong)le64_to_int(gpt_pte[part - 1].ending_lba) + 1)
  196 + info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
225 197 - info->start;
226 198 info->blksz = GPT_BLOCK_SIZE;
227 199  
... ... @@ -264,7 +236,7 @@
264 236 static int pmbr_part_valid(struct partition *part)
265 237 {
266 238 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
267   - le32_to_int(part->start_sect) == 1UL) {
  239 + le32_to_cpu(part->start_sect) == 1UL) {
268 240 return 1;
269 241 }
270 242  
271 243  
... ... @@ -283,9 +255,8 @@
283 255 {
284 256 int i = 0;
285 257  
286   - if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
  258 + if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
287 259 return 0;
288   - }
289 260  
290 261 for (i = 0; i < 4; i++) {
291 262 if (pmbr_part_valid(&mbr->partition_record[i])) {
... ... @@ -308,8 +279,8 @@
308 279 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
309 280 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
310 281 {
311   - unsigned char crc32_backup[4] = { 0 };
312   - unsigned long calc_crc32;
  282 + u32 crc32_backup = 0;
  283 + u32 calc_crc32;
313 284 unsigned long long lastlba;
314 285  
315 286 if (!dev_desc || !pgpt_head) {
316 287  
317 288  
318 289  
319 290  
320 291  
321 292  
322 293  
323 294  
324 295  
325 296  
326 297  
327 298  
328 299  
... ... @@ -324,54 +295,54 @@
324 295 }
325 296  
326 297 /* Check the GPT header signature */
327   - if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
  298 + if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
328 299 printf("GUID Partition Table Header signature is wrong:"
329 300 "0x%llX != 0x%llX\n",
330   - (unsigned long long)le64_to_int(pgpt_head->signature),
331   - (unsigned long long)GPT_HEADER_SIGNATURE);
  301 + le64_to_cpu(pgpt_head->signature),
  302 + GPT_HEADER_SIGNATURE);
332 303 return 0;
333 304 }
334 305  
335 306 /* Check the GUID Partition Table CRC */
336   - memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
337   - memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
  307 + memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
  308 + memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
338 309  
339 310 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
340   - le32_to_int(pgpt_head->header_size));
  311 + le32_to_cpu(pgpt_head->header_size));
341 312  
342   - memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
  313 + memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
343 314  
344   - if (calc_crc32 != le32_to_int(crc32_backup)) {
  315 + if (calc_crc32 != le32_to_cpu(crc32_backup)) {
345 316 printf("GUID Partition Table Header CRC is wrong:"
346   - "0x%08lX != 0x%08lX\n",
347   - le32_to_int(crc32_backup), calc_crc32);
  317 + "0x%x != 0x%x\n",
  318 + le32_to_cpu(crc32_backup), calc_crc32);
348 319 return 0;
349 320 }
350 321  
351 322 /* Check that the my_lba entry points to the LBA that contains the GPT */
352   - if (le64_to_int(pgpt_head->my_lba) != lba) {
  323 + if (le64_to_cpu(pgpt_head->my_lba) != lba) {
353 324 printf("GPT: my_lba incorrect: %llX != %llX\n",
354   - (unsigned long long)le64_to_int(pgpt_head->my_lba),
355   - (unsigned long long)lba);
  325 + le64_to_cpu(pgpt_head->my_lba),
  326 + lba);
356 327 return 0;
357 328 }
358 329  
359 330 /* Check the first_usable_lba and last_usable_lba are within the disk. */
360 331 lastlba = (unsigned long long)dev_desc->lba;
361   - if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
  332 + if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
362 333 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
363   - le64_to_int(pgpt_head->first_usable_lba), lastlba);
  334 + le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
364 335 return 0;
365 336 }
366   - if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
  337 + if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
367 338 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
368   - le64_to_int(pgpt_head->last_usable_lba), lastlba);
  339 + (u64) le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
369 340 return 0;
370 341 }
371 342  
372 343 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
373   - le64_to_int(pgpt_head->first_usable_lba),
374   - le64_to_int(pgpt_head->last_usable_lba), lastlba);
  344 + le64_to_cpu(pgpt_head->first_usable_lba),
  345 + le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
375 346  
376 347 /* Read and allocate Partition Table Entries */
377 348 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
378 349  
379 350  
... ... @@ -382,13 +353,13 @@
382 353  
383 354 /* Check the GUID Partition Table Entry Array CRC */
384 355 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
385   - le32_to_int(pgpt_head->num_partition_entries) *
386   - le32_to_int(pgpt_head->sizeof_partition_entry));
  356 + le32_to_cpu(pgpt_head->num_partition_entries) *
  357 + le32_to_cpu(pgpt_head->sizeof_partition_entry));
387 358  
388   - if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
  359 + if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
389 360 printf("GUID Partition Table Entry Array CRC is wrong:"
390   - "0x%08lX != 0x%08lX\n",
391   - le32_to_int(pgpt_head->partition_entry_array_crc32),
  361 + "0x%x != 0x%x\n",
  362 + le32_to_cpu(pgpt_head->partition_entry_array_crc32),
392 363 calc_crc32);
393 364  
394 365 free(*pgpt_pte);
395 366  
... ... @@ -419,12 +390,12 @@
419 390 return NULL;
420 391 }
421 392  
422   - count = le32_to_int(pgpt_head->num_partition_entries) *
423   - le32_to_int(pgpt_head->sizeof_partition_entry);
  393 + count = le32_to_cpu(pgpt_head->num_partition_entries) *
  394 + le32_to_cpu(pgpt_head->sizeof_partition_entry);
424 395  
425   - debug("%s: count = %lu * %lu = %zu\n", __func__,
426   - le32_to_int(pgpt_head->num_partition_entries),
427   - le32_to_int(pgpt_head->sizeof_partition_entry), count);
  396 + debug("%s: count = %u * %u = %zu\n", __func__,
  397 + (u32) le32_to_cpu(pgpt_head->num_partition_entries),
  398 + (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
428 399  
429 400 /* Allocate memory for PTE, remember to FREE */
430 401 if (count != 0) {
... ... @@ -440,7 +411,7 @@
440 411  
441 412 /* Read GPT Entries from device */
442 413 if (dev_desc->block_read (dev_desc->dev,
443   - (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
  414 + le64_to_cpu(pgpt_head->partition_entry_lba),
444 415 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
445 416 != (count / GPT_BLOCK_SIZE)) {
446 417  
... ... @@ -29,6 +29,8 @@
29 29 * http://developer.intel.com/technology/efi/efi.htm
30 30 */
31 31  
  32 +#include <linux/compiler.h>
  33 +
32 34 #ifndef _DISK_PART_EFI_H
33 35 #define _DISK_PART_EFI_H
34 36  
... ... @@ -41,6 +43,8 @@
41 43 #define GPT_HEADER_REVISION_V1 0x00010000
42 44 #define GPT_PRIMARY_PARTITION_TABLE_LBA 1ULL
43 45 #define GPT_ENTRY_NAME "gpt"
  46 +#define GPT_ENTRY_NUMBERS 128
  47 +#define GPT_ENTRY_SIZE 128
44 48  
45 49 #define EFI_GUID(a,b,c,d0,d1,d2,d3,d4,d5,d6,d7) \
46 50 ((efi_guid_t) \
47 51  
48 52  
49 53  
50 54  
51 55  
52 56  
53 57  
54 58  
55 59  
56 60  
... ... @@ -72,74 +76,73 @@
72 76 0xa2, 0x3c, 0x23, 0x8f, 0x2a, 0x3d, 0xf9, 0x28)
73 77  
74 78 /* linux/include/efi.h */
75   -typedef unsigned short efi_char16_t;
  79 +typedef u16 efi_char16_t;
76 80  
77 81 typedef struct {
78   - unsigned char b[16];
  82 + u8 b[16];
79 83 } efi_guid_t;
80 84  
81 85 /* based on linux/include/genhd.h */
82 86 struct partition {
83   - unsigned char boot_ind; /* 0x80 - active */
84   - unsigned char head; /* starting head */
85   - unsigned char sector; /* starting sector */
86   - unsigned char cyl; /* starting cylinder */
87   - unsigned char sys_ind; /* What partition type */
88   - unsigned char end_head; /* end head */
89   - unsigned char end_sector; /* end sector */
90   - unsigned char end_cyl; /* end cylinder */
91   - unsigned char start_sect[4]; /* starting sector counting from 0 */
92   - unsigned char nr_sects[4]; /* nr of sectors in partition */
93   -} __attribute__ ((packed));
  87 + u8 boot_ind; /* 0x80 - active */
  88 + u8 head; /* starting head */
  89 + u8 sector; /* starting sector */
  90 + u8 cyl; /* starting cylinder */
  91 + u8 sys_ind; /* What partition type */
  92 + u8 end_head; /* end head */
  93 + u8 end_sector; /* end sector */
  94 + u8 end_cyl; /* end cylinder */
  95 + __le32 start_sect; /* starting sector counting from 0 */
  96 + __le32 nr_sects; /* nr of sectors in partition */
  97 +} __packed;
94 98  
95 99 /* based on linux/fs/partitions/efi.h */
96 100 typedef struct _gpt_header {
97   - unsigned char signature[8];
98   - unsigned char revision[4];
99   - unsigned char header_size[4];
100   - unsigned char header_crc32[4];
101   - unsigned char reserved1[4];
102   - unsigned char my_lba[8];
103   - unsigned char alternate_lba[8];
104   - unsigned char first_usable_lba[8];
105   - unsigned char last_usable_lba[8];
  101 + __le64 signature;
  102 + __le32 revision;
  103 + __le32 header_size;
  104 + __le32 header_crc32;
  105 + __le32 reserved1;
  106 + __le64 my_lba;
  107 + __le64 alternate_lba;
  108 + __le64 first_usable_lba;
  109 + __le64 last_usable_lba;
106 110 efi_guid_t disk_guid;
107   - unsigned char partition_entry_lba[8];
108   - unsigned char num_partition_entries[4];
109   - unsigned char sizeof_partition_entry[4];
110   - unsigned char partition_entry_array_crc32[4];
111   - unsigned char reserved2[GPT_BLOCK_SIZE - 92];
112   -} __attribute__ ((packed)) gpt_header;
  111 + __le64 partition_entry_lba;
  112 + __le32 num_partition_entries;
  113 + __le32 sizeof_partition_entry;
  114 + __le32 partition_entry_array_crc32;
  115 + u8 reserved2[GPT_BLOCK_SIZE - 92];
  116 +} __packed gpt_header;
113 117  
114 118 typedef union _gpt_entry_attributes {
115 119 struct {
116   - unsigned long long required_to_function:1;
117   - unsigned long long no_block_io_protocol:1;
118   - unsigned long long legacy_bios_bootable:1;
119   - unsigned long long reserved:45;
120   - unsigned long long type_guid_specific:16;
  120 + u64 required_to_function:1;
  121 + u64 no_block_io_protocol:1;
  122 + u64 legacy_bios_bootable:1;
  123 + u64 reserved:45;
  124 + u64 type_guid_specific:16;
121 125 } fields;
122 126 unsigned long long raw;
123   -} __attribute__ ((packed)) gpt_entry_attributes;
  127 +} __packed gpt_entry_attributes;
124 128  
125 129 #define PARTNAME_SZ (72 / sizeof(efi_char16_t))
126 130 typedef struct _gpt_entry {
127 131 efi_guid_t partition_type_guid;
128 132 efi_guid_t unique_partition_guid;
129   - unsigned char starting_lba[8];
130   - unsigned char ending_lba[8];
  133 + __le64 starting_lba;
  134 + __le64 ending_lba;
131 135 gpt_entry_attributes attributes;
132 136 efi_char16_t partition_name[PARTNAME_SZ];
133   -}
134   -__attribute__ ((packed)) gpt_entry;
  137 +} __packed gpt_entry;
135 138  
136 139 typedef struct _legacy_mbr {
137   - unsigned char boot_code[440];
138   - unsigned char unique_mbr_signature[4];
139   - unsigned char unknown[2];
  140 + u8 boot_code[440];
  141 + __le32 unique_mbr_signature;
  142 + __le16 unknown;
140 143 struct partition partition_record[4];
141   - unsigned char signature[2];
142   -} __attribute__ ((packed)) legacy_mbr;
  144 + __le16 signature;
  145 +} __packed legacy_mbr;
143 146  
144 147 #endif /* _DISK_PART_EFI_H */