applet_tables.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
  43. ofs = 0;
  44. for (i = 0; i < NUM_APPLETS; i++) {
  45. offset[i] = ofs;
  46. ofs += strlen(applets[i].name) + 1;
  47. }
  48. /* We reuse 4 high-order bits of offset array for other purposes,
  49. * so if they are indeed needed, refuse to proceed */
  50. if (ofs > 0xfff)
  51. return 1;
  52. if (!argv[1])
  53. return 1;
  54. i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
  55. if (i < 0)
  56. return 1;
  57. dup2(i, 1);
  58. /* Keep in sync with include/busybox.h! */
  59. puts("/* This is a generated file, don't edit */");
  60. puts("const char applet_names[] ALIGN1 = \"\"\n");
  61. for (i = 0; i < NUM_APPLETS; i++) {
  62. printf("\"%s\" \"\\0\"\n", applets[i].name);
  63. }
  64. puts(";");
  65. puts("int (*const applet_main[])(int argc, char **argv) = {");
  66. for (i = 0; i < NUM_APPLETS; i++) {
  67. printf("%s_main,\n", applets[i].main);
  68. }
  69. puts("};");
  70. puts("const uint16_t applet_nameofs[] ALIGN2 = {");
  71. for (i = 0; i < NUM_APPLETS; i++) {
  72. printf("0x%04x,\n",
  73. offset[i]
  74. #if ENABLE_FEATURE_PREFER_APPLETS
  75. + (applets[i].nofork << 12)
  76. + (applets[i].noexec << 13)
  77. #endif
  78. #if ENABLE_FEATURE_SUID
  79. + (applets[i].need_suid << 14) /* 2 bits */
  80. #endif
  81. );
  82. }
  83. puts("};");
  84. #if ENABLE_FEATURE_INSTALLER
  85. puts("const uint8_t applet_install_loc[] ALIGN1 = {");
  86. i = 0;
  87. while (i < NUM_APPLETS) {
  88. int v = applets[i].install_loc; /* 3 bits */
  89. if (++i < NUM_APPLETS)
  90. v |= applets[i].install_loc << 4; /* 3 bits */
  91. printf("0x%02x,\n", v);
  92. i++;
  93. }
  94. puts("};");
  95. #endif
  96. return 0;
  97. }