applet_tables.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. printf("/* This is a generated file, don't edit */\n\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. printf("\n");
  67. printf("#ifndef SKIP_definitions\n");
  68. printf("const char applet_names[] ALIGN1 = \"\"\n");
  69. for (i = 0; i < NUM_APPLETS; i++) {
  70. printf("\"%s\" \"\\0\"\n", applets[i].name);
  71. if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
  72. MAX_APPLET_NAME_LEN = strlen(applets[i].name);
  73. }
  74. printf(";\n\n");
  75. printf("#ifndef SKIP_applet_main\n");
  76. printf("int (*const applet_main[])(int argc, char **argv) = {\n");
  77. for (i = 0; i < NUM_APPLETS; i++) {
  78. printf("%s_main,\n", applets[i].main);
  79. }
  80. printf("};\n");
  81. printf("#endif\n\n");
  82. printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
  83. for (i = 0; i < NUM_APPLETS; i++) {
  84. printf("0x%04x,\n",
  85. offset[i]
  86. #if ENABLE_FEATURE_PREFER_APPLETS
  87. + (applets[i].nofork << 12)
  88. + (applets[i].noexec << 13)
  89. #endif
  90. #if ENABLE_FEATURE_SUID
  91. + (applets[i].need_suid << 14) /* 2 bits */
  92. #endif
  93. );
  94. }
  95. printf("};\n\n");
  96. #if ENABLE_FEATURE_INSTALLER
  97. printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
  98. i = 0;
  99. while (i < NUM_APPLETS) {
  100. int v = applets[i].install_loc; /* 3 bits */
  101. if (++i < NUM_APPLETS)
  102. v |= applets[i].install_loc << 4; /* 3 bits */
  103. printf("0x%02x,\n", v);
  104. i++;
  105. }
  106. printf("};\n");
  107. #endif
  108. printf("#endif /* SKIP_definitions */\n");
  109. printf("\n");
  110. printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
  111. return 0;
  112. }