cgpt_repair.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include <string.h>
  5. #include "cgpt.h"
  6. #include "cgptlib_internal.h"
  7. #include "vboot_host.h"
  8. int CgptRepair(CgptRepairParams *params) {
  9. struct drive drive;
  10. if (params == NULL)
  11. return CGPT_FAILED;
  12. if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
  13. params->drive_size))
  14. return CGPT_FAILED;
  15. int gpt_retval = GptSanityCheck(&drive.gpt);
  16. if (params->verbose)
  17. printf("GptSanityCheck() returned %d: %s\n",
  18. gpt_retval, GptError(gpt_retval));
  19. GptHeader *header;
  20. if (MASK_PRIMARY == drive.gpt.valid_headers ||
  21. MASK_BOTH == drive.gpt.valid_headers) {
  22. header = (GptHeader *)(drive.gpt.primary_header);
  23. } else {
  24. header = (GptHeader *)(drive.gpt.secondary_header);
  25. }
  26. if (MASK_PRIMARY == drive.gpt.valid_entries) {
  27. free(drive.gpt.secondary_entries);
  28. drive.gpt.secondary_entries =
  29. malloc(header->size_of_entry * header->number_of_entries);
  30. } else if (MASK_SECONDARY == drive.gpt.valid_entries) {
  31. free(drive.gpt.primary_entries);
  32. drive.gpt.primary_entries =
  33. malloc(header->size_of_entry * header->number_of_entries);
  34. }
  35. GptRepair(&drive.gpt);
  36. if (drive.gpt.modified & GPT_MODIFIED_HEADER1)
  37. printf("Primary Header is updated.\n");
  38. if (drive.gpt.modified & GPT_MODIFIED_ENTRIES1)
  39. printf("Primary Entries is updated.\n");
  40. if (drive.gpt.modified & GPT_MODIFIED_ENTRIES2)
  41. printf("Secondary Entries is updated.\n");
  42. if (drive.gpt.modified & GPT_MODIFIED_HEADER2)
  43. printf("Secondary Header is updated.\n");
  44. return DriveClose(&drive, 1);
  45. }