applet_tables.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Applet table generator.
  4. * Runs on host and produces include/applet_tables.h
  5. *
  6. * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com>
  7. *
  8. * Licensed under GPLv2, see file License in this tarball for details.
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "../include/autoconf.h"
  14. #include "../include/busybox.h"
  15. struct bb_applet {
  16. const char *name;
  17. const char *main;
  18. enum bb_install_loc_t install_loc;
  19. enum bb_suid_t need_suid;
  20. /* true if instead of fork(); exec("applet"); waitpid();
  21. * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
  22. unsigned char noexec;
  23. /* Even nicer */
  24. /* true if instead of fork(); exec("applet"); waitpid();
  25. * one can simply call applet_main(argc,argv); */
  26. unsigned char nofork;
  27. };
  28. /* Define struct bb_applet applets[] */
  29. #include "../include/applets.h"
  30. enum { NUM_APPLETS = ARRAY_SIZE(applets) };
  31. static int offset[NUM_APPLETS];
  32. static int cmp_name(const void *a, const void *b)
  33. {
  34. const struct bb_applet *aa = a;
  35. const struct bb_applet *bb = b;
  36. return strcmp(aa->name, bb->name);
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. int i;
  41. int ofs;
  42. unsigned MAX_APPLET_NAME_LEN = 1;
  43. qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
  44. ofs = 0;
  45. for (i = 0; i < NUM_APPLETS; i++) {
  46. offset[i] = ofs;
  47. ofs += strlen(applets[i].name) + 1;
  48. }
  49. /* We reuse 4 high-order bits of offset array for other purposes,
  50. * so if they are indeed needed, refuse to proceed */
  51. if (ofs > 0xfff)
  52. return 1;
  53. if (!argv[1])
  54. return 1;
  55. i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
  56. if (i < 0)
  57. return 1;
  58. dup2(i, 1);
  59. /* Keep in sync with include/busybox.h! */
  60. puts("/* This is a generated file, don't edit */\n");
  61. printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
  62. if (NUM_APPLETS == 1) {
  63. printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
  64. printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].name);
  65. }
  66. puts("\nconst char applet_names[] ALIGN1 = \"\"");
  67. for (i = 0; i < NUM_APPLETS; i++) {
  68. printf("\"%s\" \"\\0\"\n", applets[i].name);
  69. if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
  70. MAX_APPLET_NAME_LEN = strlen(applets[i].name);
  71. }
  72. puts(";");
  73. puts("\nint (*const applet_main[])(int argc, char **argv) = {");
  74. for (i = 0; i < NUM_APPLETS; i++) {
  75. printf("%s_main,\n", applets[i].main);
  76. }
  77. puts("};");
  78. puts("const uint16_t applet_nameofs[] ALIGN2 = {");
  79. for (i = 0; i < NUM_APPLETS; i++) {
  80. printf("0x%04x,\n",
  81. offset[i]
  82. #if ENABLE_FEATURE_PREFER_APPLETS
  83. + (applets[i].nofork << 12)
  84. + (applets[i].noexec << 13)
  85. #endif
  86. #if ENABLE_FEATURE_SUID
  87. + (applets[i].need_suid << 14) /* 2 bits */
  88. #endif
  89. );
  90. }
  91. puts("};");
  92. #if ENABLE_FEATURE_INSTALLER
  93. puts("const uint8_t applet_install_loc[] ALIGN1 = {");
  94. i = 0;
  95. while (i < NUM_APPLETS) {
  96. int v = applets[i].install_loc; /* 3 bits */
  97. if (++i < NUM_APPLETS)
  98. v |= applets[i].install_loc << 4; /* 3 bits */
  99. printf("0x%02x,\n", v);
  100. i++;
  101. }
  102. puts("};\n");
  103. #endif
  104. printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
  105. return 0;
  106. }