cmd_boot.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <getopt.h>
  5. #include <string.h>
  6. #include "cgpt.h"
  7. #include "vboot_host.h"
  8. extern const char* progname;
  9. static void Usage(void)
  10. {
  11. printf("\nUsage: %s boot [OPTIONS] DRIVE\n\n"
  12. "Edit the PMBR sector for legacy BIOSes\n\n"
  13. "Options:\n"
  14. " -D NUM Size (in bytes) of the disk where partitions reside\n"
  15. " default 0, meaning partitions and GPT structs are\n"
  16. " both on DRIVE\n"
  17. " -i NUM Set bootable partition\n"
  18. " -b FILE Install bootloader code in the PMBR\n"
  19. " -p Create legacy PMBR partition table\n"
  20. "\n"
  21. "With no options, it will just print the PMBR boot guid\n"
  22. "\n", progname);
  23. }
  24. int cmd_boot(int argc, char *argv[]) {
  25. CgptBootParams params;
  26. memset(&params, 0, sizeof(params));
  27. int c;
  28. int errorcnt = 0;
  29. char *e = 0;
  30. opterr = 0; // quiet, you
  31. while ((c=getopt(argc, argv, ":hi:b:pD:")) != -1)
  32. {
  33. switch (c)
  34. {
  35. case 'D':
  36. params.drive_size = strtoull(optarg, &e, 0);
  37. errorcnt += check_int_parse(c, e);
  38. break;
  39. case 'i':
  40. params.partition = (uint32_t)strtoul(optarg, &e, 0);
  41. errorcnt += check_int_parse(c, e);
  42. break;
  43. case 'b':
  44. params.bootfile = optarg;
  45. break;
  46. case 'p':
  47. params.create_pmbr = 1;
  48. break;
  49. case 'h':
  50. Usage();
  51. return CGPT_OK;
  52. case '?':
  53. Error("unrecognized option: -%c\n", optopt);
  54. errorcnt++;
  55. break;
  56. case ':':
  57. Error("missing argument to -%c\n", optopt);
  58. errorcnt++;
  59. break;
  60. default:
  61. errorcnt++;
  62. break;
  63. }
  64. }
  65. if (errorcnt)
  66. {
  67. Usage();
  68. return CGPT_FAILED;
  69. }
  70. if (optind >= argc) {
  71. Error("missing drive argument\n");
  72. return CGPT_FAILED;
  73. }
  74. params.drive_name = argv[optind];
  75. return CgptBoot(&params);
  76. }