ubirename.c 2.5 KB

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