summaryrefslogtreecommitdiff
path: root/cgpt/cgpt_edit.c
blob: c7781ffca7879f0ebdd0b5e318dfd5bbb0d9f1c7 (plain)
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
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

int CgptEdit(CgptEditParams *params) {
  struct drive drive;
  GptHeader *h;
  int gpt_retval;

  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 = GptSanityCheck(&drive.gpt))) {
    Error("GptSanityCheck() returned %d: %s\n",
          gpt_retval, GptError(gpt_retval));
    goto bad;
  }

  if (CGPT_OK != CheckValid(&drive)) {
    Error("Please run 'cgpt repair' before changing settings.\n");
    goto bad;
  }

  h = (GptHeader *)drive.gpt.primary_header;
  if (params->set_unique) {
    memcpy(&h->disk_uuid, &params->unique_guid, sizeof(h->disk_uuid));
  }
  // Copy to secondary
  RepairHeader(&drive.gpt, MASK_PRIMARY);
  drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_HEADER2);

  UpdateCrc(&drive.gpt);

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

bad:

  DriveClose(&drive, 0);
  return CGPT_FAILED;
}