Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/partitions/efi.c
Line
Count
Source
1
/************************************************************
2
 * EFI GUID Partition Table handling
3
 *
4
 * http://www.uefi.org/specs/
5
 * http://www.intel.com/technology/efi/
6
 *
7
 * efi.[ch] by Matt Domsch <Matt_Domsch@dell.com>
8
 *   Copyright 2000,2001,2002,2004 Dell Inc.
9
 *
10
 * Copyright (C) 2013 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
11
 *
12
 * Under GPLv2 only
13
 */
14
15
#include <common.h>
16
#include <disks.h>
17
#include <init.h>
18
#include <magicvar.h>
19
#include <globalvar.h>
20
#include <asm/unaligned.h>
21
#include <crc.h>
22
#include <state.h>
23
#include <fcntl.h>
24
#include <linux/ctype.h>
25
#include <fuzz.h>
26
27
#include <efi/partition.h>
28
#include <partitions.h>
29
30
struct efi_partition_desc {
31
  struct partition_desc pd;
32
  gpt_header *gpt;
33
  gpt_entry *ptes;
34
  struct param_d *param_guid;
35
  bool good_pgpt;
36
  bool good_agpt;
37
};
38
39
struct efi_partition {
40
  struct partition part;
41
  gpt_entry *pte;
42
};
43
44
static const int force_gpt = IS_ENABLED(CONFIG_PARTITION_DISK_EFI_GPT_NO_FORCE);
45
46
struct gpt_refresh {
47
  struct block_device *blk;
48
  struct list_head list;
49
};
50
51
static LIST_HEAD(gpt_refreshes);
52
53
static unsigned int global_gpt_refresh;
54
55
static int gpt_refresh_one(struct block_device *blk)
56
0
{
57
0
  struct partition_desc *pdesc;
58
0
  int ret;
59
0
60
0
  if (!global_gpt_refresh)
61
0
    return 0;
62
0
63
0
  ret = cdev_open(&blk->cdev, O_RDWR);
64
0
  if (ret)
65
0
    return ret;
66
0
67
0
  pdesc = partition_table_read(blk);
68
0
  if (!pdesc) {
69
0
    ret = -EINVAL;
70
0
    goto out;
71
0
  }
72
0
73
0
  ret = partition_table_write(pdesc);
74
0
  if (ret)
75
0
    goto out;
76
0
77
0
  ret = 0;
78
0
79
0
  partition_table_free(pdesc);
80
0
out:
81
0
  cdev_close(&blk->cdev);
82
0
83
0
  if (ret)
84
0
    dev_err(blk->dev, "Refreshing partition table failed: %pe\n",
85
0
      ERR_PTR(ret));
86
0
87
0
  return ret;
88
0
}
89
90
static bool gpt_refresh_done;
91
92
static int do_gpt_refreshes(void)
93
32
{
94
32
  struct gpt_refresh *r, *tmp;
95
96
32
  if (!IS_ENABLED(CONFIG_PARTITION_DISK_EFI_REFRESH))
97
32
    return 0;
98
99
0
  if (!global_gpt_refresh)
100
0
    return 0;
101
102
0
  list_for_each_entry_safe(r, tmp, &gpt_refreshes, list) {
103
0
    gpt_refresh_one(r->blk);
104
0
    list_del(&r->list);
105
0
    free(r);
106
0
  }
107
108
0
  gpt_refresh_done = true;
109
110
0
  return 0;
111
0
}
112
postenvironment_initcall(do_gpt_refreshes);
113
114
static void add_gpt_refresh(struct block_device *blk)
115
729
{
116
729
  struct gpt_refresh *r;
117
118
729
  if (!IS_ENABLED(CONFIG_PARTITION_DISK_EFI_REFRESH))
119
729
    return;
120
121
0
  if (gpt_refresh_done) {
122
0
    gpt_refresh_one(blk);
123
0
    return;
124
0
  }
125
126
0
  r = xzalloc(sizeof(*r));
127
128
0
  r->blk = blk;
129
130
0
  list_add_tail(&r->list, &gpt_refreshes);
131
0
}
132
133
/**
134
* compute_partitions_entries_size() - return the size of all partitions
135
* @gpt: GPT header
136
*
137
* Description: return size of all partitions, 0 on error
138
*
139
* This is a helper function that compute the size of all partitions
140
* by multiplying the size of a single partition by the number of partitions
141
*/
142
static u32 compute_partitions_entries_size(const gpt_header *gpt)
143
3.80k
{
144
3.80k
  u32 nb_parts, sz_parts, total_size;
145
146
3.80k
  nb_parts = le32_to_cpu(gpt->num_partition_entries);
147
3.80k
  sz_parts = le32_to_cpu(gpt->sizeof_partition_entry);
148
149
3.80k
  if (check_mul_overflow(nb_parts, sz_parts, &total_size))
150
8
    return 0;
151
152
3.79k
  return total_size;
153
3.80k
}
154
155
/**
156
 * efi_crc32() - EFI version of crc32 function
157
 * @buf: buffer to calculate crc32 of
158
 * @len - length of buf
159
 *
160
 * Description: Returns EFI-style CRC32 value for @buf
161
 *
162
 * This function uses the little endian Ethernet polynomial
163
 * but seeds the function with ~0, and xor's with ~0 at the end.
164
 * Note, the EFI Specification, v1.02, has a reference to
165
 * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
166
 */
167
static inline u32
168
efi_crc32(const void *buf, unsigned long len)
169
5.13k
{
170
5.13k
  return crc32(0, buf, len);
171
5.13k
}
172
173
/**
174
 * last_lba(): return number of last logical block of device
175
 * @bdev: block device
176
 *
177
 * Description: Returns last LBA value on success, 0 on error.
178
 * This is stored (by sd and ide-geometry) in
179
 *  the part[0] entry for this disk, and is the number of
180
 *  physical sectors available on the disk.
181
 */
182
static u64 last_lba(struct block_device *bdev)
183
182k
{
184
182k
  if (!bdev)
185
0
    return 0;
186
182k
  return bdev->num_blocks - 1;
187
182k
}
188
189
/**
190
 * alloc_read_gpt_entries(): reads partition entries from disk
191
 * @dev_desc
192
 * @gpt - GPT header
193
 *
194
 * Description: Returns ptes on success,  NULL on error.
195
 * Allocates space for PTEs based on information found in @gpt.
196
 * Notes: remember to free pte when you're done!
197
 */
198
static gpt_entry *alloc_read_gpt_entries(struct block_device *blk,
199
           gpt_header * pgpt_head)
200
1.98k
{
201
1.98k
  u32 count = 0;
202
1.98k
  gpt_entry *pte = NULL;
203
1.98k
  unsigned long from;
204
1.98k
  blkcnt_t size;
205
1.98k
  int ret;
206
207
1.98k
  count = compute_partitions_entries_size(pgpt_head);
208
1.98k
  if (!count)
209
9
    return NULL;
210
211
1.97k
  pte = calloc(blockdevice_round_block_nbytes(blk, count), 1);
212
1.97k
  if (!pte)
213
108
    return NULL;
214
215
1.86k
  from = le64_to_cpu(pgpt_head->partition_entry_lba);
216
1.86k
  size = blockdevice_round_nblocks(blk, count);
217
1.86k
  ret = block_read(blk, pte, from, size);
218
1.86k
  if (ret) {
219
43
    free(pte);
220
43
    return NULL;
221
43
  }
222
1.82k
  return pte;
223
1.86k
}
224
225
/**
226
 * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
227
 * @state
228
 * @lba is the Logical Block Address of the partition table
229
 *
230
 * Description: returns GPT header on success, NULL on error.   Allocates
231
 * and fills a GPT header starting at @ from @state->bdev.
232
 * Note: remember to free gpt when finished with it.
233
 */
234
static gpt_header *alloc_read_gpt_header(struct block_device *blk,
235
           u64 lba)
236
4.86k
{
237
4.86k
  gpt_header *gpt;
238
4.86k
  unsigned ssz = BLOCKSIZE(blk);
239
4.86k
  int ret;
240
241
4.86k
  gpt = calloc(ssz, 1);
242
4.86k
  if (!gpt)
243
0
    return NULL;
244
245
4.86k
  ret = block_read(blk, gpt, lba, 1);
246
4.86k
  if (ret) {
247
815
    free(gpt);
248
815
    return NULL;
249
815
  }
250
251
4.04k
  return gpt;
252
4.86k
}
253
254
/**
255
 * is_gpt_valid() - tests one GPT header and PTEs for validity
256
 *
257
 * lba is the logical block address of the GPT header to test
258
 * gpt is a GPT header ptr, filled on return.
259
 * ptes is a PTEs ptr, filled on return.
260
 *
261
 * Description: returns 1 if valid,  0 on error.
262
 * If valid, returns pointers to PTEs.
263
 */
264
static int is_gpt_valid(struct block_device *blk, u64 lba,
265
      gpt_header **gpt, gpt_entry **ptes)
266
4.86k
{
267
4.86k
  u32 crc, origcrc, count, ptescrc;
268
4.86k
  u64 lastlba;
269
270
4.86k
  if (!ptes)
271
0
    return 0;
272
4.86k
  if (!(*gpt = alloc_read_gpt_header(blk, lba)))
273
815
    return 0;
274
275
  /* Check the GPT header signature */
276
4.04k
  if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
277
440
    dev_dbg(blk->dev, "GUID Partition Table Header signature at LBA"
278
440
      "%llu is wrong: 0x%llX != 0x%llX\n", lba,
279
0
      (unsigned long long)le64_to_cpu((*gpt)->signature),
280
0
      (unsigned long long)GPT_HEADER_SIGNATURE);
281
440
    goto fail;
282
440
  }
283
284
3.60k
  if (le32_to_cpu((*gpt)->header_size) < sizeof(struct _gpt_header) ||
285
3.57k
      le32_to_cpu((*gpt)->header_size) > BLOCKSIZE(blk))
286
288
    goto fail;
287
288
  /* Check the GUID Partition Table CRC */
289
3.31k
  origcrc = le32_to_cpu((*gpt)->header_crc32);
290
3.31k
  (*gpt)->header_crc32 = 0;
291
3.31k
  crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
292
293
3.31k
  if (crc != origcrc && !fuzz_insecure_checksum_accepted(crc, origcrc)) {
294
38
    dev_dbg(blk->dev, "GUID Partition Table Header CRC is wrong: %x != %x\n",
295
38
       crc, origcrc);
296
38
    goto fail;
297
38
  }
298
3.28k
  (*gpt)->header_crc32 = cpu_to_le32(origcrc);
299
300
  /* Check that the my_lba entry points to the LBA that contains
301
   * the GUID Partition Table */
302
3.28k
  if (le64_to_cpu((*gpt)->my_lba) != lba) {
303
383
    dev_dbg(blk->dev, "GPT: my_lba incorrect: %llX != %llX\n",
304
383
      (unsigned long long)le64_to_cpu((*gpt)->my_lba),
305
0
      (unsigned long long)lba);
306
383
    goto fail;
307
383
  }
308
309
  /* Check the first_usable_lba and last_usable_lba are within the disk. */
310
2.89k
  lastlba = last_lba(blk);
311
2.89k
  if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
312
369
    dev_dbg(blk->dev, "GPT: first_usable_lba incorrect: %llu > %llu\n",
313
369
       (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
314
0
       (unsigned long long)lastlba);
315
369
    goto fail;
316
369
  }
317
2.52k
  if (le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
318
387
    dev_dbg(blk->dev, "GPT: last_usable_lba incorrect: %llu > %llu\n",
319
387
       (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
320
0
       (unsigned long long)lastlba);
321
387
    goto fail;
322
387
  }
323
324
  /*
325
   * The entry array is allocated from sizeof_partition_entry, but
326
   * walked as an array of gpt_entry, so anything else than the
327
   * size of that structure would index past the allocation.
328
   */
329
2.14k
  if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
330
161
    dev_dbg(blk->dev, "GUID Partition Entry Size check failed: %u != %zu\n",
331
161
      le32_to_cpu((*gpt)->sizeof_partition_entry),
332
0
      sizeof(gpt_entry));
333
161
    goto fail;
334
161
  }
335
336
1.98k
  if (!(*ptes = alloc_read_gpt_entries(blk, *gpt)))
337
160
    goto fail;
338
339
  /* Check the size of all partitions */
340
1.82k
  count = compute_partitions_entries_size(*gpt);
341
1.82k
  if (!count)
342
0
    goto fail_ptes;
343
344
  /* Check the GUID Partition Table Entry Array CRC */
345
1.82k
  crc = efi_crc32((const unsigned char *)*ptes, count);
346
347
1.82k
  ptescrc = le32_to_cpu((*gpt)->partition_entry_array_crc32);
348
1.82k
  if (crc != ptescrc && !fuzz_insecure_checksum_accepted(crc, ptescrc)) {
349
73
    dev_dbg(blk->dev, "GUID Partitition Entry Array CRC check failed: 0x%08x 0x%08x\n",
350
73
      crc, ptescrc);
351
73
    goto fail_ptes;
352
73
  }
353
354
  /* We're done, all's well */
355
1.74k
  return 1;
356
357
73
 fail_ptes:
358
73
  free(*ptes);
359
73
  *ptes = NULL;
360
2.29k
 fail:
361
2.29k
  free(*gpt);
362
2.29k
  *gpt = NULL;
363
2.29k
  return 0;
364
73
}
365
366
/**
367
 * is_pte_valid() - tests one PTE for validity
368
 * @pte is the pte to check
369
 * @lastlba is last lba of the disk
370
 *
371
 * Description: returns 1 if valid,  0 on error.
372
 */
373
static inline int
374
is_pte_valid(const gpt_entry *pte, const u64 lastlba)
375
177k
{
376
177k
  u64 start = le64_to_cpu(pte->starting_lba);
377
177k
  u64 end = le64_to_cpu(pte->ending_lba);
378
379
177k
  if (guid_is_null(&pte->partition_type_guid) ||
380
95.6k
      start > lastlba ||
381
49.1k
      end > lastlba ||
382
36.7k
      start > end)
383
142k
    return 0;
384
35.1k
  return 1;
385
177k
}
386
387
/**
388
 * compare_gpts() - Search disk for valid GPT headers and PTEs
389
 * @pgpt is the primary GPT header
390
 * @agpt is the alternate GPT header
391
 * @lastlba is the last LBA number
392
 * Description: Returns nothing.  Sanity checks pgpt and agpt fields
393
 * and prints warnings on discrepancies.
394
 *
395
 */
396
static void
397
compare_gpts(struct block_device *blk, gpt_header *pgpt, gpt_header *agpt,
398
       u64 lastlba)
399
1.01k
{
400
1.01k
  struct device *dev = blk->dev;
401
1.01k
  u32 pgptcrc, agptcrc;
402
1.01k
  int error_found = 0;
403
404
1.01k
  if (!pgpt || !agpt)
405
281
    return;
406
733
  if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
407
710
    dev_warn(dev,
408
710
           "GPT:Primary header LBA != Alt. header alternate_lba\n");
409
710
    dev_warn(dev, "GPT:%llu != %llu\n",
410
1.42k
           (unsigned long long)le64_to_cpu(pgpt->my_lba),
411
1.42k
           (unsigned long long)le64_to_cpu(agpt->alternate_lba));
412
710
    error_found++;
413
710
  }
414
733
  if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
415
652
    dev_warn(dev,
416
652
           "GPT:Primary header alternate_lba != Alt. header my_lba\n");
417
652
    dev_warn(dev, "GPT:%llu != %llu\n",
418
1.30k
           (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
419
1.30k
           (unsigned long long)le64_to_cpu(agpt->my_lba));
420
652
    error_found++;
421
652
  }
422
733
  if (le64_to_cpu(pgpt->first_usable_lba) !=
423
733
      le64_to_cpu(agpt->first_usable_lba)) {
424
38
    dev_warn(dev, "GPT:first_usable_lbas don't match.\n");
425
38
    dev_warn(dev, "GPT:%llu != %llu\n",
426
76
           (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
427
76
           (unsigned long long)le64_to_cpu(agpt->first_usable_lba));
428
38
    error_found++;
429
38
  }
430
733
  if (le64_to_cpu(pgpt->last_usable_lba) !=
431
733
      le64_to_cpu(agpt->last_usable_lba)) {
432
78
    dev_warn(dev, "GPT:last_usable_lbas don't match.\n");
433
78
    dev_warn(dev, "GPT:%llu != %llu\n",
434
156
           (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
435
156
           (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
436
78
    error_found++;
437
78
  }
438
733
  if (!guid_equal(&pgpt->disk_guid, &agpt->disk_guid)) {
439
128
    dev_warn(dev, "GPT:disk_guids don't match.\n");
440
128
    error_found++;
441
128
  }
442
733
  if (le32_to_cpu(pgpt->num_partition_entries) !=
443
733
      le32_to_cpu(agpt->num_partition_entries)) {
444
98
    dev_warn(dev, "GPT:num_partition_entries don't match: "
445
98
           "0x%x != 0x%x\n",
446
98
           le32_to_cpu(pgpt->num_partition_entries),
447
98
           le32_to_cpu(agpt->num_partition_entries));
448
98
    error_found++;
449
98
  }
450
733
  if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
451
733
      le32_to_cpu(agpt->sizeof_partition_entry)) {
452
0
    dev_warn(dev,
453
0
           "GPT:sizeof_partition_entry values don't match: "
454
0
           "0x%x != 0x%x\n",
455
0
           le32_to_cpu(pgpt->sizeof_partition_entry),
456
0
           le32_to_cpu(agpt->sizeof_partition_entry));
457
0
    error_found++;
458
0
  }
459
733
  pgptcrc = le32_to_cpu(pgpt->partition_entry_array_crc32);
460
733
  agptcrc = le32_to_cpu(agpt->partition_entry_array_crc32);
461
733
  if (pgptcrc != agptcrc &&
462
124
      !fuzz_insecure_checksum_accepted(pgptcrc, agptcrc)) {
463
41
    dev_warn(dev,
464
41
           "GPT:partition_entry_array_crc32 values don't match: "
465
41
           "0x%x != 0x%x\n", pgptcrc, agptcrc);
466
41
    error_found++;
467
41
  }
468
733
  if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
469
724
    dev_warn(dev,
470
724
           "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
471
724
    dev_warn(dev, "GPT:%llu != %llu\n",
472
724
      (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
473
724
      (unsigned long long)lastlba);
474
724
    error_found++;
475
724
  }
476
477
733
  if (le64_to_cpu(agpt->my_lba) != lastlba) {
478
72
    dev_warn(dev,
479
72
           "GPT:Alternate GPT header not at the end of the disk.\n");
480
72
    dev_warn(dev, "GPT:%llu != %llu\n",
481
72
      (unsigned long long)le64_to_cpu(agpt->my_lba),
482
72
      (unsigned long long)lastlba);
483
72
    error_found++;
484
72
  }
485
486
733
  if (error_found) {
487
729
    add_gpt_refresh(blk);
488
729
    if (!IS_ENABLED(CONFIG_PARTITION_DISK_EFI_REFRESH))
489
729
      dev_info(dev, "GPT: will repair later if global.system.gpt_refresh=1\n");
490
0
    else
491
729
      dev_warn(dev, "GPT: Use parted to correct GPT errors.\n");
492
729
  }
493
494
733
  return;
495
1.01k
}
496
497
/**
498
 * find_valid_gpt() - Search disk for valid GPT headers and PTEs
499
 * @buf buffer containing the first lba
500
 * @epd the efi partition descriptor context pointer
501
 * Description: Returns 0 for success, negative error code otherwise.
502
 * If valid, epd is filled with pointers to newly allocated GPT header and PTEs.
503
 * Validity depends on PMBR being valid (or being overridden by the
504
 * 'gpt' kernel command line option) and finding either the Primary
505
 * GPT header and PTEs valid, or the Alternate GPT header and PTEs
506
 * valid.  If the Primary GPT header is not valid, the Alternate GPT header
507
 * is not checked unless the 'gpt' kernel command line option is passed.
508
 * This protects against devices which misreport their size, and forces
509
 * the user to decide to use the Alternate GPT.
510
 */
511
static int find_valid_gpt(struct efi_partition_desc *epd, void *buf)
512
1.96k
{
513
1.96k
  struct block_device *blk = epd->pd.blk;
514
1.96k
  int good_pgpt = 0, good_agpt = 0;
515
1.96k
  gpt_header *pgpt = NULL, *agpt = NULL;
516
1.96k
  gpt_entry *pptes = NULL, *aptes = NULL;
517
1.96k
  u64 lastlba;
518
519
1.96k
  lastlba = last_lba(blk);
520
1.96k
  if (force_gpt) {
521
    /* This will be added to the EFI Spec. per Intel after v1.02. */
522
1.96k
    if (file_detect_partition_table(buf, 2 * BLOCKSIZE(blk),
523
1.96k
            BLOCKSIZE(blk)) != filetype_gpt)
524
0
      goto fail;
525
1.96k
  }
526
527
1.96k
  good_pgpt = is_gpt_valid(blk, GPT_PRIMARY_PARTITION_TABLE_LBA,
528
1.96k
         &pgpt, &pptes);
529
1.96k
  if (good_pgpt)
530
1.01k
    good_agpt = is_gpt_valid(blk,
531
1.01k
           le64_to_cpu(pgpt->alternate_lba),
532
1.01k
           &agpt, &aptes);
533
1.96k
  if (!good_agpt && force_gpt)
534
1.88k
    good_agpt = is_gpt_valid(blk, lastlba, &agpt, &aptes);
535
536
  /* The obviously unsuccessful case */
537
1.96k
  if (!good_pgpt && !good_agpt)
538
951
    goto fail;
539
540
1.01k
  if (IS_ENABLED(CONFIG_PARTITION_DISK_EFI_GPT_COMPARE))
541
1.01k
    compare_gpts(blk, pgpt, agpt, lastlba);
542
543
1.01k
  epd->good_pgpt = good_pgpt;
544
1.01k
  epd->good_agpt = good_agpt;
545
546
  /* The good cases */
547
1.01k
  if (good_pgpt) {
548
1.01k
    epd->gpt  = pgpt;
549
1.01k
    epd->ptes = pptes;
550
1.01k
    free(agpt);
551
1.01k
    free(aptes);
552
1.01k
    if (!good_agpt)
553
1.01k
      dev_warn(blk->dev, "Alternate GPT is invalid, using primary GPT.\n");
554
1.01k
    return 0;
555
1.01k
  } else {
556
2
    epd->gpt  = agpt;
557
2
    epd->ptes = aptes;
558
2
    free(pgpt);
559
2
    free(pptes);
560
2
    dev_warn(blk->dev, "Primary GPT is invalid, using alternate GPT.\n");
561
2
    return 0;
562
2
  }
563
564
951
 fail:
565
951
  free(pgpt);
566
951
  free(agpt);
567
951
  free(pptes);
568
951
  free(aptes);
569
570
951
  return -EINVAL;
571
1.01k
}
572
573
static void part_set_efi_name(gpt_entry *pte, char *dest)
574
14.0k
{
575
14.0k
  int i;
576
577
519k
  for (i = 0; i < GPT_PARTNAME_MAX_SIZE ; i++) {
578
505k
    u8 c;
579
505k
    c = pte->partition_name[i] & 0xff;
580
505k
    c = (c && !isprint(c)) ? '.' : c;
581
505k
    dest[i] = c;
582
505k
  }
583
14.0k
  dest[i] = 0;
584
14.0k
}
585
586
static void extract_flags(const gpt_entry *p, struct partition *pentry)
587
14.0k
{
588
14.0k
  static const guid_t system_guid = PARTITION_SYSTEM_GUID;
589
590
14.0k
  if (p->attributes.required_to_function)
591
577
    pentry->flags |= DEVFS_PARTITION_REQUIRED;
592
14.0k
  if (p->attributes.no_block_io_protocol)
593
754
    pentry->flags |= DEVFS_PARTITION_NO_EXPORT;
594
14.0k
  if (p->attributes.legacy_bios_bootable)
595
576
    pentry->flags |= DEVFS_PARTITION_BOOTABLE_LEGACY;
596
597
14.0k
  if (guid_equal(&p->partition_type_guid, &system_guid))
598
22
    pentry->flags |=  DEVFS_PARTITION_BOOTABLE_ESP;
599
600
14.0k
  pentry->typeflags = p->attributes.type_guid_specific;
601
14.0k
}
602
603
static void part_get_efi_name(gpt_entry *pte, const char *src)
604
0
{
605
0
  int i;
606
0
  int len = strlen(src);
607
0
608
0
  for (i = 0; i < GPT_PARTNAME_MAX_SIZE ; i++) {
609
0
    if (i <= len)
610
0
      pte->partition_name[i] = src[i];
611
0
    else
612
0
      pte->partition_name[i] = 0;
613
0
  }
614
0
}
615
616
static void add_gpt_diskuuid_param(struct efi_partition_desc *epd,
617
           struct block_device *blk)
618
1.01k
{
619
1.01k
  epd->param_guid = dev_add_param_string_fixed(blk->dev,
620
1.01k
                 "guid", blk->cdev.diskuuid);
621
1.01k
}
622
623
static struct partition_desc *efi_partition(void *buf, struct block_device *blk)
624
1.96k
{
625
1.96k
  gpt_header *gpt = NULL;
626
1.96k
  gpt_entry *ptes = NULL;
627
1.96k
  int i = 0;
628
1.96k
  int nb_part;
629
1.96k
  struct efi_partition *epart;
630
1.96k
  struct partition *pentry;
631
1.96k
  struct efi_partition_desc *epd;
632
1.96k
  int ret;
633
634
1.96k
  epd = xzalloc(sizeof(*epd));
635
1.96k
  partition_desc_init(&epd->pd, blk);
636
637
1.96k
  ret = find_valid_gpt(epd, buf);
638
1.96k
  if (ret)
639
951
    goto err;
640
641
1.01k
  gpt = epd->gpt;
642
1.01k
  ptes = epd->ptes;
643
644
1.01k
  blk->cdev.flags |= DEVFS_IS_GPT_PARTITIONED;
645
646
1.01k
  nb_part = le32_to_cpu(gpt->num_partition_entries);
647
648
1.01k
  snprintf(blk->cdev.diskuuid, sizeof(blk->cdev.diskuuid), "%pUl", &gpt->disk_guid);
649
1.01k
  add_gpt_diskuuid_param(epd, blk);
650
651
178k
  for (i = 0; i < nb_part; i++) {
652
177k
    if (!is_pte_valid(&ptes[i], last_lba(blk))) {
653
142k
      dev_dbg(blk->dev, "Invalid pte %d\n", i);
654
142k
      continue;
655
142k
    }
656
657
35.1k
    if (i >= MAX_PARTITION) {
658
21.1k
      dev_warn(blk->dev, "ignoring GPT partition %d beyond the %d we support\n",
659
21.1k
         i + 1, MAX_PARTITION);
660
21.1k
      continue;
661
21.1k
    }
662
663
14.0k
    epart = xzalloc(sizeof(*epart));
664
14.0k
    epart->pte = &ptes[i];
665
14.0k
    pentry = &epart->part;
666
14.0k
    extract_flags(&ptes[i], pentry);
667
14.0k
    pentry->first_sec = le64_to_cpu(ptes[i].starting_lba);
668
14.0k
    pentry->size = le64_to_cpu(ptes[i].ending_lba) - pentry->first_sec;
669
14.0k
    pentry->size++;
670
14.0k
    part_set_efi_name(&ptes[i], pentry->name);
671
14.0k
    snprintf(pentry->partuuid, sizeof(pentry->partuuid), "%pUl", &ptes[i].unique_partition_guid);
672
14.0k
    pentry->typeuuid = ptes[i].partition_type_guid;
673
14.0k
    pentry->num = i;
674
14.0k
    list_add_tail(&pentry->list, &epd->pd.partitions);
675
14.0k
  }
676
677
1.01k
  return &epd->pd;
678
951
err:
679
951
  free(epd);
680
681
951
  return NULL;
682
1.96k
}
683
684
static void efi_partition_free(struct partition_desc *pd)
685
1.01k
{
686
1.01k
  struct efi_partition_desc *epd = container_of(pd, struct efi_partition_desc, pd);
687
1.01k
  struct partition *part, *tmp;
688
689
14.0k
  list_for_each_entry_safe(part, tmp, &pd->partitions, list) {
690
14.0k
    struct efi_partition *epart = container_of(part, struct efi_partition, part);
691
692
14.0k
    free(epart);
693
14.0k
  }
694
695
1.01k
  param_remove(epd->param_guid);
696
1.01k
  free(epd->ptes);
697
1.01k
  free(epd->gpt);
698
1.01k
  free(epd);
699
1.01k
}
700
701
static __maybe_unused struct partition_desc *efi_partition_create_table(struct block_device *blk)
702
0
{
703
0
  struct efi_partition_desc *epd = xzalloc(sizeof(*epd));
704
0
  gpt_header *gpt;
705
0
  unsigned int num_partition_entries = 128;
706
0
  unsigned int gpt_size = (sizeof(gpt_entry) * num_partition_entries) / SECTOR_SIZE;
707
0
  unsigned int first_usable_lba = partition_first_usable_lba(blk);
708
0
709
0
  partition_desc_init(&epd->pd, blk);
710
0
711
0
  epd->gpt = xzalloc(BLOCKSIZE(blk));
712
0
  gpt = epd->gpt;
713
0
714
0
  gpt->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
715
0
  gpt->revision = cpu_to_le32(0x100);
716
0
  gpt->header_size = cpu_to_le32(sizeof(*gpt));
717
0
  gpt->my_lba = cpu_to_le64(1);
718
0
  gpt->alternate_lba = cpu_to_le64(last_lba(blk));
719
0
  gpt->first_usable_lba = cpu_to_le64(first_usable_lba);
720
0
  gpt->last_usable_lba = cpu_to_le64(last_lba(blk) - (gpt_size + 2));;
721
0
  generate_random_guid((unsigned char *)&gpt->disk_guid);
722
0
  gpt->partition_entry_lba = cpu_to_le64(first_usable_lba - gpt_size);
723
0
  gpt->num_partition_entries = cpu_to_le32(num_partition_entries);
724
0
  gpt->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
725
0
726
0
  add_gpt_diskuuid_param(epd, blk);
727
0
728
0
  pr_info("Created new disk label with GUID %pU\n", &gpt->disk_guid);
729
0
730
0
  epd->ptes = xzalloc(num_partition_entries * sizeof(gpt_entry));
731
0
732
0
  return &epd->pd;
733
0
}
734
735
static guid_t partition_linux_data_guid = PARTITION_LINUX_DATA_GUID;
736
static guid_t partition_basic_data_guid = PARTITION_BASIC_DATA_GUID;
737
static guid_t partition_barebox_env_guid = PARTITION_BAREBOX_ENVIRONMENT_GUID;
738
static guid_t partition_barebox_state_guid = BAREBOX_STATE_PARTITION_GUID;
739
740
static const guid_t *fs_type_to_guid(const char *fstype)
741
0
{
742
0
  if (!strcmp(fstype, "ext2"))
743
0
    return &partition_linux_data_guid;
744
0
  if (!strcmp(fstype, "ext3"))
745
0
    return &partition_linux_data_guid;
746
0
  if (!strcmp(fstype, "ext4"))
747
0
    return &partition_linux_data_guid;
748
0
  if (!strcmp(fstype, "fat16"))
749
0
    return &partition_basic_data_guid;
750
0
  if (!strcmp(fstype, "fat32"))
751
0
    return &partition_basic_data_guid;
752
0
  if (!strcmp(fstype, "bbenv"))
753
0
    return &partition_barebox_env_guid;
754
0
  if (!strcmp(fstype, "barebox-state"))
755
0
    return &partition_barebox_state_guid;
756
0
757
0
  return NULL;
758
0
}
759
760
static __maybe_unused int efi_partition_mkpart(struct partition_desc *pd,
761
                 const char *name, const char *fs_type,
762
                 uint64_t start_lba, uint64_t end_lba)
763
0
{
764
0
  struct efi_partition_desc *epd = container_of(pd, struct efi_partition_desc, pd);
765
0
  struct efi_partition *epart;
766
0
  struct partition *part;
767
0
  gpt_header *gpt = epd->gpt;
768
0
  int num_parts = le32_to_cpu(gpt->num_partition_entries);
769
0
  gpt_entry *pte;
770
0
  int i;
771
0
  const guid_t *guid;
772
0
773
0
  if (start_lba < 34) {
774
0
    pr_err("invalid start LBA %llu, minimum is 34\n", start_lba);
775
0
    return -EINVAL;
776
0
  }
777
0
778
0
  if (end_lba >= last_lba(pd->blk) - 33) {
779
0
    pr_err("invalid end LBA %llu, maximum is %llu\n", end_lba,
780
0
           last_lba(pd->blk) - 34);
781
0
    return -EINVAL;
782
0
  }
783
0
784
0
  for (i = 0; i < num_parts; i++) {
785
0
    if (!is_pte_valid(&epd->ptes[i], last_lba(pd->blk)))
786
0
      break;
787
0
  }
788
0
789
0
  if (i == num_parts) {
790
0
    pr_err("partition table is full\n");
791
0
    return -ENOSPC;
792
0
  }
793
0
794
0
  guid = fs_type_to_guid(fs_type);
795
0
  if (!guid) {
796
0
    pr_err("Unknown fs type %s\n", fs_type);
797
0
    return -EINVAL;
798
0
  }
799
0
800
0
  pte = &epd->ptes[i];
801
0
  epart = xzalloc(sizeof(*epart));
802
0
  part = &epart->part;
803
0
804
0
  part->first_sec = start_lba;
805
0
  part->size = end_lba - start_lba + 1;
806
0
  part->typeuuid = *guid;
807
0
808
0
  pte->partition_type_guid = *guid;
809
0
  generate_random_guid((unsigned char *)&pte->unique_partition_guid);
810
0
  pte->starting_lba = cpu_to_le64(start_lba);
811
0
  pte->ending_lba = cpu_to_le64(end_lba);
812
0
  part_get_efi_name(pte, name);
813
0
  part_set_efi_name(pte, part->name);
814
0
  part->num = i;
815
0
816
0
  list_add_tail(&part->list, &pd->partitions);
817
0
818
0
  return 0;
819
0
}
820
821
static __maybe_unused int efi_partition_rmpart(struct partition_desc *pd, struct partition *part)
822
0
{
823
0
  struct efi_partition *epart = container_of(part, struct efi_partition, part);
824
0
825
0
  memset(epart->pte, 0, sizeof(*epart->pte));
826
0
827
0
  list_del(&part->list);
828
0
  free(epart);
829
0
830
0
  return 0;
831
0
}
832
833
static int efi_protective_mbr(struct block_device *blk)
834
0
{
835
0
  struct partition_desc *pdesc;
836
0
  int ret;
837
0
838
0
  pdesc = partition_table_new(blk, "msdos");
839
0
  if (IS_ERR(pdesc)) {
840
0
    printf("Error: Cannot create partition table: %pe\n", pdesc);
841
0
    return PTR_ERR(pdesc);
842
0
  }
843
0
844
0
  /*
845
0
   * For the protective MBR call directly into the mkpart hook. partition_create()
846
0
   * does not allow us to create a partition starting at LBA1
847
0
   */
848
0
  ret = pdesc->parser->mkpart(pdesc, "primary", "0xee", 1, last_lba(blk));
849
0
  if (ret) {
850
0
    pr_err("Cannot create partition: %pe\n", ERR_PTR(ret));
851
0
    goto out;
852
0
  }
853
0
854
0
  ret = partition_table_write(pdesc);
855
0
  if (ret) {
856
0
    pr_err("Cannot write partition: %pe\n", ERR_PTR(ret));
857
0
    goto out;
858
0
  }
859
0
out:
860
0
  partition_table_free(pdesc);
861
0
862
0
  return ret;
863
0
}
864
865
static int __efi_partition_write(struct efi_partition_desc *epd, bool primary)
866
0
{
867
0
  struct block_device *blk = epd->pd.blk;
868
0
  gpt_header *gpt;
869
0
  unsigned int count, size;
870
0
  uint64_t my_lba, partition_entry_lba;
871
0
  int ret;
872
0
873
0
  gpt = xmemdup(epd->gpt, BLOCKSIZE(blk));
874
0
875
0
  count = le32_to_cpu(gpt->num_partition_entries) *
876
0
    le32_to_cpu(gpt->sizeof_partition_entry);
877
0
878
0
  size = blockdevice_round_nblocks(blk, count);
879
0
880
0
  if (primary) {
881
0
    my_lba = 1;
882
0
883
0
    if (epd->good_pgpt) {
884
0
      /*
885
0
       * The primary GPT was good, we can just leave the partition
886
0
       * entries where they are.
887
0
       */
888
0
      partition_entry_lba = le64_to_cpu(gpt->partition_entry_lba);
889
0
    } else {
890
0
      /*
891
0
       * The primary GPT was bad. When this is not a new partition
892
0
       * table, but instead read from the disk, the header is from the
893
0
       * alternate GPT, meaning the partition_entry_lba also points
894
0
       * to the alternate partitions. We have to find a place for the
895
0
       * primary partition entries in this case. We could store them
896
0
       * right after the GPT header, but this might destroy a bootloader
897
0
       * stored there. Instead, substract the size we need from the
898
0
       * first usable LBA.
899
0
       */
900
0
      uint64_t first_usable_lba;
901
0
902
0
      first_usable_lba = le64_to_cpu(gpt->first_usable_lba);
903
0
904
0
      if (first_usable_lba < 32 + 1 + 1) {
905
0
        pr_err("First usable LBA is %llu which doesn't leave "
906
0
               "enough space for partition entries\n",
907
0
          first_usable_lba);
908
0
        return -EINVAL;
909
0
      }
910
0
      partition_entry_lba = first_usable_lba - 32;
911
0
    }
912
0
    gpt->alternate_lba = cpu_to_le64(last_lba(blk));
913
0
  } else {
914
0
    my_lba = last_lba(blk);
915
0
    partition_entry_lba = last_lba(blk) - 32;
916
0
    gpt->alternate_lba = cpu_to_le64(1);
917
0
  }
918
0
919
0
  gpt->my_lba = cpu_to_le64(my_lba);
920
0
  gpt->partition_entry_lba = cpu_to_le64(partition_entry_lba);
921
0
  gpt->partition_entry_array_crc32 = cpu_to_le32(efi_crc32(
922
0
      (const unsigned char *)epd->ptes, count));
923
0
  gpt->header_crc32 = 0;
924
0
  gpt->header_crc32 = cpu_to_le32(efi_crc32((const unsigned char *)gpt,
925
0
              le32_to_cpu(gpt->header_size)));
926
0
927
0
  ret = block_write(blk, gpt, my_lba, 1);
928
0
  if (ret)
929
0
    goto err_block_write;
930
0
931
0
  ret = block_write(blk, epd->ptes, partition_entry_lba, size);
932
0
  if (ret)
933
0
    goto err_block_write;
934
0
935
0
err_block_write:
936
0
  free(gpt);
937
0
938
0
  return ret;
939
0
}
940
941
static __maybe_unused int efi_partition_write(struct partition_desc *pd)
942
0
{
943
0
  struct block_device *blk = pd->blk;
944
0
  struct efi_partition_desc *epd = container_of(pd, struct efi_partition_desc, pd);
945
0
  gpt_header *gpt = epd->gpt;
946
0
  int ret;
947
0
948
0
  if (le32_to_cpu(gpt->num_partition_entries) != 128) {
949
0
    /*
950
0
     * This is not yet properly implemented. At least writing of the
951
0
     * alternative GPT is not correctly implemented for this case as
952
0
     * we can't assume that the partition entries are written at
953
0
     * last_lba() - 32, we would have to calculate that from the number
954
0
     * of partition entries.
955
0
     */
956
0
    pr_err("num_partition_entries is != 128. This is not yet supported for writing\n");
957
0
    return -EINVAL;
958
0
  }
959
0
960
0
  ret = efi_protective_mbr(blk);
961
0
  if (ret)
962
0
    return ret;
963
0
964
0
  ret = __efi_partition_write(epd, !epd->good_pgpt);
965
0
  if (ret)
966
0
    goto err_block_write;
967
0
968
0
  ret = __efi_partition_write(epd, epd->good_pgpt);
969
0
  if (ret)
970
0
    goto err_block_write;
971
0
972
0
  ret = 0;
973
0
974
0
err_block_write:
975
0
  if (ret)
976
0
    pr_err("Cannot write to block device: %pe\n", ERR_PTR(ret));
977
0
978
0
  return ret;
979
0
}
980
981
static struct partition_parser efi_partition_parser = {
982
  .parse = efi_partition,
983
  .partition_free = efi_partition_free,
984
#ifdef CONFIG_PARTITION_MANIPULATION
985
  .create = efi_partition_create_table,
986
  .mkpart = efi_partition_mkpart,
987
  .rmpart = efi_partition_rmpart,
988
  .write = efi_partition_write,
989
#endif
990
  .type = filetype_gpt,
991
  .name = "gpt",
992
};
993
994
#ifdef CONFIG_PARTITION_DISK_EFI_REFRESH
995
BAREBOX_MAGICVAR(global.system.gpt_refresh, "When true, refresh GPT partitions when necessary");
996
#endif
997
998
static int efi_partition_init(void)
999
32
{
1000
32
  if (IS_ENABLED(CONFIG_PARTITION_DISK_EFI_REFRESH))
1001
0
    globalvar_add_simple_bool("system.gpt_refresh",
1002
0
            &global_gpt_refresh);
1003
1004
32
  return partition_parser_register(&efi_partition_parser);
1005
32
}
1006
postconsole_initcall(efi_partition_init);