cmd_prioritize.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <uuid/uuid.h>
  9. #include "cgpt.h"
  10. #include "vboot_host.h"
  11. extern const char* progname;
  12. static void Usage(void)
  13. {
  14. printf("\nUsage: %s prioritize [OPTIONS] DRIVE\n\n"
  15. "Reorder the priority of all active ChromeOS Kernel partitions.\n\n"
  16. "Options:\n"
  17. " -D NUM Size (in bytes) of the disk where partitions reside\n"
  18. " default 0, meaning partitions and GPT structs are\n"
  19. " both on DRIVE\n"
  20. " -P NUM Highest priority to use in the new ordering. The\n"
  21. " other partitions will be ranked in decreasing\n"
  22. " priority while preserving their original order.\n"
  23. " If necessary the lowest ranks will be coalesced.\n"
  24. " No active kernels will be lowered to priority 0.\n"
  25. " -i NUM Specify the partition to make the highest in the new\n"
  26. " order.\n"
  27. " -f Friends of the given partition (those with the same\n"
  28. " starting priority) are also updated to the new\n"
  29. " highest priority.\n"
  30. "\n"
  31. "With no options this will set the lowest active kernel to\n"
  32. "priority 1 while maintaining the original order.\n"
  33. "\n", progname);
  34. }
  35. int cmd_prioritize(int argc, char *argv[]) {
  36. CgptPrioritizeParams params;
  37. memset(&params, 0, sizeof(params));
  38. int c;
  39. int errorcnt = 0;
  40. char *e = 0;
  41. opterr = 0; // quiet, you
  42. while ((c=getopt(argc, argv, ":hi:fP:D:")) != -1)
  43. {
  44. switch (c)
  45. {
  46. case 'D':
  47. params.drive_size = strtoull(optarg, &e, 0);
  48. errorcnt += check_int_parse(c, e);
  49. break;
  50. case 'i':
  51. params.set_partition = (uint32_t)strtoul(optarg, &e, 0);
  52. errorcnt += check_int_parse(c, e);
  53. break;
  54. case 'f':
  55. params.set_friends = 1;
  56. break;
  57. case 'P':
  58. params.max_priority = (int)strtol(optarg, &e, 0);
  59. errorcnt += check_int_parse(c, e);
  60. errorcnt += check_int_limit(c, params.max_priority, 1, 15);
  61. break;
  62. case 'h':
  63. Usage();
  64. return CGPT_OK;
  65. case '?':
  66. Error("unrecognized option: -%c\n", optopt);
  67. errorcnt++;
  68. break;
  69. case ':':
  70. Error("missing argument to -%c\n", optopt);
  71. errorcnt++;
  72. break;
  73. default:
  74. errorcnt++;
  75. break;
  76. }
  77. }
  78. if (errorcnt)
  79. {
  80. Usage();
  81. return CGPT_FAILED;
  82. }
  83. if (params.set_friends && !params.set_partition) {
  84. Error("the -f option is only useful with the -i option\n");
  85. Usage();
  86. return CGPT_FAILED;
  87. }
  88. if (optind >= argc) {
  89. Error("missing drive argument\n");
  90. return CGPT_FAILED;
  91. }
  92. params.drive_name = argv[optind];
  93. return CgptPrioritize(&params);
  94. }