1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <string.h>
6 
7 #include "cgpt.h"
8 #include "cgptlib_internal.h"
9 #include "vboot_host.h"
10 
CgptLegacy(CgptLegacyParams * params)11 int CgptLegacy(CgptLegacyParams *params) {
12   struct drive drive;
13   GptHeader *h1, *h2;
14 
15   if (params == NULL)
16     return CGPT_FAILED;
17 
18   if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
19                            params->drive_size))
20     return CGPT_FAILED;
21 
22   h1 = (GptHeader *)drive.gpt.primary_header;
23   h2 = (GptHeader *)drive.gpt.secondary_header;
24   if (params->efipart) {
25     memcpy(h1->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
26     memcpy(h2->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
27     RepairEntries(&drive.gpt, MASK_SECONDARY);
28     drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
29                            GPT_MODIFIED_HEADER2);
30   } else {
31     memcpy(h1->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE);
32     memcpy(h2->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE);
33     memset(drive.gpt.primary_entries, 0, drive.gpt.sector_bytes);
34     drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
35                            GPT_MODIFIED_HEADER2);
36   }
37 
38   UpdateCrc(&drive.gpt);
39 
40   // Write it all out
41   return DriveClose(&drive, 1);
42 }
43