xref: /aosp_15_r20/external/vboot_reference/cgpt/cgpt_edit.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2018 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include "cgpt.h"
7 #include "cgptlib_internal.h"
8 #include "cgpt_params.h"
9 #include "vboot_host.h"
10 
CgptEdit(CgptEditParams * params)11 int CgptEdit(CgptEditParams *params) {
12   struct drive drive;
13   GptHeader *h;
14   int gpt_retval;
15 
16   if (params == NULL)
17     return CGPT_FAILED;
18 
19   if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
20                            params->drive_size))
21     return CGPT_FAILED;
22 
23   if (GPT_SUCCESS != (gpt_retval = GptValidityCheck(&drive.gpt))) {
24     Error("GptValidityCheck() returned %d: %s\n",
25           gpt_retval, GptError(gpt_retval));
26     goto bad;
27   }
28 
29   if (CGPT_OK != CheckValid(&drive)) {
30     Error("Please run 'cgpt repair' before changing settings.\n");
31     goto bad;
32   }
33 
34   h = (GptHeader *)drive.gpt.primary_header;
35   if (params->set_unique) {
36     memcpy(&h->disk_uuid, &params->unique_guid, sizeof(h->disk_uuid));
37   }
38   // Copy to secondary
39   RepairHeader(&drive.gpt, MASK_PRIMARY);
40   drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_HEADER2);
41 
42   UpdateCrc(&drive.gpt);
43 
44   // Write it all out.
45   return DriveClose(&drive, 1);
46 
47 bad:
48 
49   DriveClose(&drive, 0);
50   return CGPT_FAILED;
51 }
52