applet_tables.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 source tree.
  9. */
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #undef ARRAY_SIZE
  18. #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
  19. #include "../include/autoconf.h"
  20. #include "../include/applet_metadata.h"
  21. struct bb_applet {
  22. const char *name;
  23. const char *main;
  24. enum bb_install_loc_t install_loc;
  25. enum bb_suid_t need_suid;
  26. /* true if instead of fork(); exec("applet"); waitpid();
  27. * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
  28. unsigned char noexec;
  29. /* Even nicer */
  30. /* true if instead of fork(); exec("applet"); waitpid();
  31. * one can simply call applet_main(argc,argv); */
  32. unsigned char nofork;
  33. };
  34. /* Define struct bb_applet applets[] */
  35. #include "../include/applets.h"
  36. enum { NUM_APPLETS = ARRAY_SIZE(applets) };
  37. static int offset[NUM_APPLETS];
  38. static int cmp_name(const void *a, const void *b)
  39. {
  40. const struct bb_applet *aa = a;
  41. const struct bb_applet *bb = b;
  42. return strcmp(aa->name, bb->name);
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. int i;
  47. int ofs;
  48. // unsigned MAX_APPLET_NAME_LEN = 1;
  49. qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
  50. ofs = 0;
  51. for (i = 0; i < NUM_APPLETS; i++) {
  52. offset[i] = ofs;
  53. ofs += strlen(applets[i].name) + 1;
  54. }
  55. /* We reuse 4 high-order bits of offset array for other purposes,
  56. * so if they are indeed needed, refuse to proceed */
  57. if (ofs > 0xfff)
  58. return 1;
  59. if (!argv[1])
  60. return 1;
  61. i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
  62. if (i < 0)
  63. return 1;
  64. dup2(i, 1);
  65. /* Keep in sync with include/busybox.h! */
  66. printf("/* This is a generated file, don't edit */\n\n");
  67. printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
  68. if (NUM_APPLETS == 1) {
  69. printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
  70. printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].main);
  71. }
  72. printf("\n");
  73. //printf("#ifndef SKIP_definitions\n");
  74. printf("const char applet_names[] ALIGN1 = \"\"\n");
  75. for (i = 0; i < NUM_APPLETS; i++) {
  76. printf("\"%s\" \"\\0\"\n", applets[i].name);
  77. // if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
  78. // MAX_APPLET_NAME_LEN = strlen(applets[i].name);
  79. }
  80. printf(";\n\n");
  81. printf("#ifndef SKIP_applet_main\n");
  82. printf("int (*const applet_main[])(int argc, char **argv) = {\n");
  83. for (i = 0; i < NUM_APPLETS; i++) {
  84. printf("%s_main,\n", applets[i].main);
  85. }
  86. printf("};\n");
  87. printf("#endif\n\n");
  88. printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
  89. for (i = 0; i < NUM_APPLETS; i++) {
  90. printf("0x%04x,\n",
  91. offset[i]
  92. #if ENABLE_FEATURE_PREFER_APPLETS
  93. + (applets[i].nofork << 12)
  94. + (applets[i].noexec << 13)
  95. #endif
  96. #if ENABLE_FEATURE_SUID
  97. + (applets[i].need_suid << 14) /* 2 bits */
  98. #endif
  99. );
  100. }
  101. printf("};\n\n");
  102. #if ENABLE_FEATURE_INSTALLER
  103. printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
  104. i = 0;
  105. while (i < NUM_APPLETS) {
  106. int v = applets[i].install_loc; /* 3 bits */
  107. if (++i < NUM_APPLETS)
  108. v |= applets[i].install_loc << 4; /* 3 bits */
  109. printf("0x%02x,\n", v);
  110. i++;
  111. }
  112. printf("};\n");
  113. #endif
  114. //printf("#endif /* SKIP_definitions */\n");
  115. // printf("\n");
  116. // printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
  117. if (argv[2]) {
  118. char line_old[80];
  119. char line_new[80];
  120. FILE *fp;
  121. line_old[0] = 0;
  122. fp = fopen(argv[2], "r");
  123. if (fp) {
  124. fgets(line_old, sizeof(line_old), fp);
  125. fclose(fp);
  126. }
  127. sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
  128. if (strcmp(line_old, line_new) != 0) {
  129. fp = fopen(argv[2], "w");
  130. if (!fp)
  131. return 1;
  132. fputs(line_new, fp);
  133. }
  134. }
  135. return 0;
  136. }