mtdpart.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * mtdpart.c
  3. *
  4. * Copyright 2015 The Chromium OS Authors.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Overview:
  11. * This utility adds or removes a partition from an MTD device.
  12. */
  13. #define PROGRAM_NAME "mtdpart"
  14. #include <fcntl.h>
  15. #include <getopt.h>
  16. #include <limits.h>
  17. #include <linux/blkpg.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. #include "common.h"
  25. static void display_help(int status)
  26. {
  27. fprintf(status == EXIT_SUCCESS ? stdout : stderr,
  28. "Usage: %1$s add [OPTION] <MTD_DEVICE> <PART_NAME> <START> <SIZE>\n"
  29. " %1$s del [OPTION] <MTD_DEVICE> <PART_NUMBER>\n"
  30. "Adds a partition to an MTD device, or remove an existing partition from it.\n"
  31. "\n"
  32. " -h, --help Display this help and exit\n"
  33. " -V, --version Output version information and exit\n"
  34. "\n"
  35. "START location and SIZE of the partition are in bytes. They should align on\n"
  36. "eraseblock size.\n",
  37. PROGRAM_NAME
  38. );
  39. exit(status);
  40. }
  41. static void display_version(void)
  42. {
  43. common_print_version();
  44. printf("%1$s comes with NO WARRANTY\n"
  45. "to the extent permitted by law.\n"
  46. "\n"
  47. "You may redistribute copies of %1$s\n"
  48. "under the terms of the GNU General Public Licence.\n"
  49. "See the file `COPYING' for more information.\n",
  50. PROGRAM_NAME);
  51. exit(EXIT_SUCCESS);
  52. }
  53. /* Command arguments */
  54. typedef enum {
  55. COMMAND_ADD,
  56. COMMAND_DEL
  57. } command_type;
  58. static command_type command; /* add or del */
  59. static const char *mtddev; /* mtd device name */
  60. static const char *part_name; /* partition name */
  61. static int part_no; /* partition number */
  62. static long long start_addr; /* start address */
  63. static long long length; /* partition size */
  64. static void process_options(int argc, char * const argv[])
  65. {
  66. int error = 0;
  67. for (;;) {
  68. int option_index = 0;
  69. static const char short_options[] = "hV";
  70. static const struct option long_options[] = {
  71. {"version", no_argument, 0, 'V'},
  72. {"help", no_argument, 0, 'h'},
  73. {0, 0, 0, 0},
  74. };
  75. int c = getopt_long(argc, argv, short_options,
  76. long_options, &option_index);
  77. if (c == EOF) {
  78. break;
  79. }
  80. switch (c) {
  81. case 'V':
  82. display_version();
  83. break;
  84. case 'h':
  85. display_help(EXIT_SUCCESS);
  86. break;
  87. case '?':
  88. error++;
  89. break;
  90. }
  91. }
  92. if ((argc - optind) < 3 || error)
  93. display_help(EXIT_FAILURE);
  94. const char *s_command = argv[optind++];
  95. mtddev = argv[optind++];
  96. if (strcmp(s_command, "del") == 0 && (argc - optind) == 1) {
  97. const char *s_part_no = argv[optind++];
  98. long tmp = simple_strtol(s_part_no, &error);
  99. if (tmp < 0)
  100. errmsg_die("Can't specify negative partition number: %ld",
  101. tmp);
  102. if (tmp > INT_MAX)
  103. errmsg_die("Partition number exceeds INT_MAX: %ld",
  104. tmp);
  105. part_no = tmp;
  106. command = COMMAND_DEL;
  107. } else if (strcmp(s_command, "add") == 0 && (argc - optind) == 3) {
  108. const char *s_start;
  109. const char *s_length;
  110. part_name = argv[optind++];
  111. s_start = argv[optind++];
  112. s_length = argv[optind++];
  113. if (strlen(part_name) >= BLKPG_DEVNAMELTH)
  114. errmsg_die("Partition name (%s) should be less than %d characters",
  115. part_name, BLKPG_DEVNAMELTH);
  116. start_addr = simple_strtoll(s_start, &error);
  117. if (start_addr < 0)
  118. errmsg_die("Can't specify negative start offset: %lld",
  119. start_addr);
  120. length = simple_strtoll(s_length, &error);
  121. if (length < 0)
  122. errmsg_die("Can't specify negative length: %lld",
  123. length);
  124. command = COMMAND_ADD;
  125. } else
  126. display_help(EXIT_FAILURE);
  127. if (error)
  128. display_help(EXIT_FAILURE);
  129. }
  130. int main(int argc, char * const argv[])
  131. {
  132. int fd;
  133. struct blkpg_partition part;
  134. struct blkpg_ioctl_arg arg;
  135. process_options(argc, argv);
  136. fd = open(mtddev, O_RDWR | O_CLOEXEC);
  137. if (fd == -1)
  138. sys_errmsg_die("Cannot open %s", mtddev);
  139. memset(&part, 0, sizeof(part));
  140. memset(&arg, 0, sizeof(arg));
  141. arg.datalen = sizeof(part);
  142. arg.data = &part;
  143. switch (command) {
  144. case COMMAND_ADD:
  145. part.start = start_addr;
  146. part.length = length;
  147. strncpy(part.devname, part_name,
  148. sizeof(part.devname) - 1);
  149. part.devname[sizeof(part.devname) - 1] = '\0';
  150. arg.op = BLKPG_ADD_PARTITION;
  151. break;
  152. case COMMAND_DEL:
  153. part.pno = part_no;
  154. arg.op = BLKPG_DEL_PARTITION;
  155. break;
  156. }
  157. if (ioctl(fd, BLKPG, &arg))
  158. sys_errmsg_die("Failed to issue BLKPG ioctl");
  159. close(fd);
  160. /* Exit happy */
  161. return EXIT_SUCCESS;
  162. }