File: cgpt_legacy.c

package info (click to toggle)
coreboot 24.12%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 210,640 kB
  • sloc: ansic: 1,640,478; sh: 15,676; python: 10,743; perl: 10,186; asm: 8,483; makefile: 5,097; cpp: 4,724; pascal: 2,327; ada: 1,928; yacc: 1,264; lex: 731; sed: 75; lisp: 5; ruby: 5; awk: 4
file content (66 lines) | stat: -rw-r--r-- 2,216 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Copyright 2012 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <string.h>

#include "cgpt.h"
#include "cgptlib_internal.h"
#include "vboot_host.h"

int CgptLegacy(CgptLegacyParams *params) {
  struct drive drive;
  int gpt_retval;
  GptHeader *h1, *h2;

  if (params == NULL)
    return CGPT_FAILED;

  if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
                           params->drive_size))
    return CGPT_FAILED;

  if (GPT_SUCCESS != (gpt_retval = GptValidityCheck(&drive.gpt))) {
    Error("GptValidityCheck() returned %d: %s\n",
          gpt_retval, GptError(gpt_retval));
    goto bad;
  }

  h1 = (GptHeader *)drive.gpt.primary_header;
  h2 = (GptHeader *)drive.gpt.secondary_header;
  if (params->mode == CGPT_LEGACY_MODE_EFIPART) {
    drive.gpt.ignored = MASK_NONE;
    memcpy(h1->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
    memcpy(h2->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
    RepairEntries(&drive.gpt, MASK_SECONDARY);
    drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
                           GPT_MODIFIED_HEADER2);
  } else if (params->mode == CGPT_LEGACY_MODE_IGNORE_PRIMARY) {
    if (!(drive.gpt.valid_headers & MASK_SECONDARY) ||
        !(drive.gpt.valid_entries & MASK_SECONDARY) ||
        drive.gpt.ignored & MASK_SECONDARY) {
      Error("Refusing to mark primary GPT ignored unless secondary is valid.");
      goto bad;
    }
    memset(h1, 0, sizeof(*h1));
    memcpy(h1->signature, GPT_HEADER_SIGNATURE_IGNORED,
           GPT_HEADER_SIGNATURE_SIZE);
    drive.gpt.modified |= GPT_MODIFIED_HEADER1;
  } else {
    memcpy(h1->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE);
    memcpy(h2->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE);
    memset(drive.gpt.primary_entries, 0, drive.gpt.sector_bytes);
    drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
                           GPT_MODIFIED_HEADER2);
  }

  UpdateCrc(&drive.gpt);

  // Write it all out
  return DriveClose(&drive, 1);

bad:
  (void) DriveClose(&drive, 0);
  return CGPT_FAILED;
}