ubirename.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* ubirename - port of the ubirename from the mtd-utils package
  2. *
  3. * A utility to rename one UBI volume.
  4. *
  5. * 2016-03-01 Sven Eisenberg <sven.eisenberg@novero.com>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config UBIRENAME
  10. //config: bool "ubirename (2.4 kb)"
  11. //config: default y
  12. //config: help
  13. //config: Utility to rename UBI volumes
  14. //applet:IF_UBIRENAME(APPLET(ubirename, BB_DIR_USR_SBIN, BB_SUID_DROP))
  15. /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
  16. //kbuild:lib-$(CONFIG_UBIRENAME) += ubirename.o
  17. //usage:#define ubirename_trivial_usage
  18. //usage: "UBI_DEVICE OLD_VOLNAME NEW_VOLNAME [OLD2 NEW2]..."
  19. //usage:#define ubirename_full_usage "\n\n"
  20. //usage: "Rename UBI volumes on UBI_DEVICE"
  21. #include "libbb.h"
  22. #include <mtd/mtd-user.h>
  23. #ifndef __packed
  24. # define __packed __attribute__((packed))
  25. #endif
  26. // from ubi-media.h
  27. #define UBI_MAX_VOLUME_NAME 127
  28. #define UBI_MAX_VOLUMES 128
  29. // end ubi-media.h
  30. // from ubi-user.h
  31. /* ioctl commands of UBI character devices */
  32. #define UBI_IOC_MAGIC 'o'
  33. /* Re-name volumes */
  34. #define UBI_IOCRNVOL _IOW(UBI_IOC_MAGIC, 3, struct ubi_rnvol_req)
  35. /* Maximum amount of UBI volumes that can be re-named at one go */
  36. #define UBI_MAX_RNVOL 32
  37. struct ubi_rnvol_req {
  38. int32_t count;
  39. int8_t padding1[12];
  40. struct {
  41. int32_t vol_id;
  42. int16_t name_len;
  43. int8_t padding2[2];
  44. char name[UBI_MAX_VOLUME_NAME + 1];
  45. } ents[UBI_MAX_RNVOL];
  46. } __packed;
  47. // end ubi-user.h
  48. int ubirename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  49. int ubirename_main(int argc, char **argv)
  50. {
  51. struct ubi_rnvol_req *rnvol;
  52. const char *ubi_devname;
  53. unsigned ubi_devnum;
  54. unsigned n;
  55. /* argc can be 4, 6, 8, ... */
  56. if ((argc & 1) || (argc >>= 1) < 2)
  57. bb_show_usage();
  58. rnvol = xzalloc(sizeof(*rnvol));
  59. rnvol->count = --argc;
  60. if (argc > ARRAY_SIZE(rnvol->ents))
  61. bb_simple_error_msg_and_die("too many renames requested");
  62. ubi_devname = argv[1];
  63. ubi_devnum = ubi_devnum_from_devname(ubi_devname);
  64. n = 0;
  65. argv += 2;
  66. while (argv[0]) {
  67. rnvol->ents[n].vol_id = ubi_get_volid_by_name(ubi_devnum, argv[0]);
  68. /* strnlen avoids overflow of 16-bit field (paranoia) */
  69. rnvol->ents[n].name_len = strnlen(argv[1], sizeof(rnvol->ents[n].name));
  70. if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name))
  71. bb_error_msg_and_die("new name '%s' is too long", argv[1]);
  72. strcpy(rnvol->ents[n].name, argv[1]);
  73. n++;
  74. argv += 2;
  75. }
  76. xioctl(xopen(ubi_devname, O_RDONLY), UBI_IOCRNVOL, rnvol);
  77. return 0;
  78. }