mkpack.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* mkpack: build the command pack that the kernel embeds and mounts at /ramfs.
  2. *
  3. * Usage: mkpack <out.pack> name=file [name=file ...]
  4. *
  5. * Format (little-endian):
  6. * offset 0 char magic[8] = "NBPAK1\0\0"
  7. * offset 8 u32 count
  8. * offset 12 u32 total_size
  9. * offset 16 count entries: { char path[32]; u32 offset; u32 size; }
  10. * ... file data, each 4-byte aligned
  11. * All offsets are relative to the start of the pack. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdint.h>
  16. static const char MAGIC[8] = { 'N', 'B', 'P', 'A', 'K', '1', '\0', '\0' };
  17. #define PATH_MAX_ 32
  18. struct hdr {
  19. char magic[8];
  20. uint32_t count;
  21. uint32_t total;
  22. };
  23. struct ent {
  24. char path[PATH_MAX_];
  25. uint32_t off;
  26. uint32_t size;
  27. };
  28. static void put32le(unsigned char *p, uint32_t v)
  29. {
  30. p[0] = (unsigned char)(v & 0xFF);
  31. p[1] = (unsigned char)((v >> 8) & 0xFF);
  32. p[2] = (unsigned char)((v >> 16) & 0xFF);
  33. p[3] = (unsigned char)((v >> 24) & 0xFF);
  34. }
  35. static unsigned char *read_file(const char *path, long *size)
  36. {
  37. FILE *f = fopen(path, "rb");
  38. unsigned char *data;
  39. long n;
  40. if (!f) {
  41. fprintf(stderr, "mkpack: %s: cannot open\n", path);
  42. exit(1);
  43. }
  44. fseek(f, 0, SEEK_END);
  45. n = ftell(f);
  46. fseek(f, 0, SEEK_SET);
  47. if (n <= 0) {
  48. fprintf(stderr, "mkpack: %s: empty\n", path);
  49. exit(1);
  50. }
  51. data = malloc((size_t)n);
  52. if (!data || fread(data, 1, (size_t)n, f) != (size_t)n) {
  53. fprintf(stderr, "mkpack: %s: read failed\n", path);
  54. exit(1);
  55. }
  56. fclose(f);
  57. *size = n;
  58. return data;
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. struct hdr h;
  63. struct ent *ents;
  64. unsigned char *out;
  65. unsigned int i, off, total;
  66. const char *eq;
  67. if (argc < 3) {
  68. fprintf(stderr, "usage: %s <out.pack> name=file [name=file ...]\n",
  69. argv[0]);
  70. return 1;
  71. }
  72. ents = calloc((size_t)(argc - 2), sizeof(*ents));
  73. if (!ents) {
  74. fprintf(stderr, "mkpack: out of memory\n");
  75. return 1;
  76. }
  77. /* Reserve the header + table; data follows. Build in memory: the
  78. * pack is a few hundred KB at most. */
  79. off = (unsigned int)(sizeof(h) + (unsigned int)(argc - 2) * sizeof(*ents));
  80. out = malloc(8u * 1024u * 1024u);
  81. if (!out) {
  82. fprintf(stderr, "mkpack: out of memory\n");
  83. return 1;
  84. }
  85. for (i = 0; i < (unsigned int)(argc - 2); i++) {
  86. unsigned char *data;
  87. long n;
  88. unsigned int name_len;
  89. eq = strchr(argv[2 + i], '=');
  90. if (!eq || eq == argv[2 + i]) {
  91. fprintf(stderr, "mkpack: expected name=file, got '%s'\n",
  92. argv[2 + i]);
  93. return 1;
  94. }
  95. name_len = (unsigned int)(eq - argv[2 + i]);
  96. if (name_len >= PATH_MAX_) {
  97. fprintf(stderr, "mkpack: name too long: %.*s\n",
  98. (int)name_len, argv[2 + i]);
  99. return 1;
  100. }
  101. memcpy(ents[i].path, argv[2 + i], name_len);
  102. ents[i].path[name_len] = '\0';
  103. data = read_file(eq + 1, &n);
  104. off = (off + 3u) & ~3u; /* align each file */
  105. if (off + (unsigned int)n > 8u * 1024u * 1024u) {
  106. fprintf(stderr, "mkpack: pack too large\n");
  107. return 1;
  108. }
  109. memcpy(out + off, data, (size_t)n);
  110. ents[i].off = off;
  111. ents[i].size = (uint32_t)n;
  112. off += (unsigned int)n;
  113. free(data);
  114. }
  115. total = off;
  116. memcpy(h.magic, MAGIC, sizeof(MAGIC));
  117. put32le((unsigned char *)&h.count, (uint32_t)(argc - 2));
  118. put32le((unsigned char *)&h.total, total);
  119. memcpy(out, &h, sizeof(h));
  120. for (i = 0; i < (unsigned int)(argc - 2); i++) {
  121. unsigned char *e = out + sizeof(h) + i * sizeof(*ents);
  122. memcpy(e, ents[i].path, PATH_MAX_);
  123. put32le(e + PATH_MAX_, ents[i].off);
  124. put32le(e + PATH_MAX_ + 4, ents[i].size);
  125. }
  126. {
  127. FILE *f = fopen(argv[1], "wb");
  128. if (!f || fwrite(out, 1, total, f) != total) {
  129. fprintf(stderr, "mkpack: %s: write failed\n", argv[1]);
  130. return 1;
  131. }
  132. fclose(f);
  133. }
  134. fprintf(stderr, "mkpack: %s: %u files, %u bytes\n", argv[1],
  135. (unsigned int)(argc - 2), total);
  136. return 0;
  137. }