ramfs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* ramfs: see ramfs.h. The pack format is produced by mkpack.c:
  2. *
  3. * char magic[8] = "NBPAK1"; u32 count; u32 total;
  4. * count x { char path[32]; u32 off; u32 size; } (offsets from pack start)
  5. * file data, 4-byte aligned
  6. */
  7. #include "ramfs.h"
  8. #include "inflate.h"
  9. #include "mm.h"
  10. #include "serial.h"
  11. #define RAMFS_MAX_DECOMPRESSED (8u * 1024u * 1024u)
  12. struct ramfs_hdr {
  13. char magic[8];
  14. unsigned int count;
  15. unsigned int total;
  16. };
  17. struct ramfs_ent {
  18. char path[RAMFS_PATH_MAX];
  19. unsigned int off;
  20. unsigned int size;
  21. };
  22. static const unsigned char *pack_base;
  23. static unsigned int pack_size;
  24. static const struct ramfs_hdr *pack_hdr;
  25. static unsigned int pack_pages; /* heap pages, when not embedded */
  26. static int str_eq(const char *a, const char *b)
  27. {
  28. unsigned int i = 0;
  29. while (a[i] && b[i] && a[i] == b[i])
  30. i++;
  31. return a[i] == '\0' && b[i] == '\0';
  32. }
  33. static unsigned int first_len(const char *path)
  34. {
  35. unsigned int n = 0;
  36. while (path[n] && path[n] != '/')
  37. n++;
  38. return n;
  39. }
  40. /* Same first component ("bin/a" vs "bin/b" -> 1)? */
  41. static int first_component_same(const char *a, const char *b)
  42. {
  43. unsigned int i, na = first_len(a), nb = first_len(b);
  44. if (na != nb)
  45. return 0;
  46. for (i = 0; i < na; i++)
  47. if (a[i] != b[i])
  48. return 0;
  49. return 1;
  50. }
  51. /* Skip a leading "dir/" prefix; NULL when the entry is not inside `dir`. */
  52. static const char *strip_dir(const char *path, const char *dir)
  53. {
  54. unsigned int i = 0;
  55. if (dir[0] == '\0')
  56. return path;
  57. while (dir[i] && path[i] == dir[i])
  58. i++;
  59. if (dir[i] != '\0')
  60. return 0;
  61. if (path[i] == '/')
  62. return path + i + 1;
  63. return 0;
  64. }
  65. /* Basic name check: no empties, no "." / "..". */
  66. static int path_ok(const char *path)
  67. {
  68. unsigned int i = 0, len = 0;
  69. if (!path || !path[0])
  70. return 0;
  71. for (;;) {
  72. char c = path[i];
  73. if (c == '\0' || c == '/') {
  74. if (len == 0)
  75. return 0;
  76. if (len == 1 && path[i - 1] == '.')
  77. return 0;
  78. if (len == 2 && path[i - 1] == '.' && path[i - 2] == '.')
  79. return 0;
  80. len = 0;
  81. if (c == '\0')
  82. return 1;
  83. } else {
  84. len++;
  85. }
  86. i++;
  87. }
  88. }
  89. /* Shape + bounds check of a pack; NULL when it is not a valid pack. */
  90. static const struct ramfs_hdr *ramfs_validate(const void *pack, unsigned int size)
  91. {
  92. const struct ramfs_hdr *h = (const struct ramfs_hdr *)pack;
  93. const struct ramfs_ent *ents;
  94. unsigned int i, need;
  95. if (size < sizeof(*h))
  96. return 0;
  97. if (h->magic[0] != 'N' || h->magic[1] != 'B' || h->magic[2] != 'P' ||
  98. h->magic[3] != 'A' || h->magic[4] != 'K' || h->magic[5] != '1')
  99. return 0;
  100. if (h->count == 0 || h->count > RAMFS_MAX_FILES)
  101. return 0;
  102. need = (unsigned int)(sizeof(*h) + h->count * sizeof(*ents));
  103. if (need > size || h->total > size || h->total < need)
  104. return 0;
  105. ents = (const struct ramfs_ent *)((const unsigned char *)pack + sizeof(*h));
  106. for (i = 0; i < h->count; i++) {
  107. if (!path_ok(ents[i].path))
  108. return 0;
  109. if (ents[i].off > size || ents[i].size > size - ents[i].off)
  110. return 0;
  111. }
  112. return h;
  113. }
  114. int ramfs_init(const void *pack, unsigned int size)
  115. {
  116. const struct ramfs_hdr *h;
  117. const unsigned char *p = (const unsigned char *)pack;
  118. /* The embedded pack is gzip-compressed (the same inflate code that the
  119. * PNG decoder uses); a raw pack is still accepted, which is what the
  120. * cmds-dev path passes to ramfs_replace(). */
  121. if (size >= 18 && p[0] == 0x1F && p[1] == 0x8B) {
  122. unsigned int isize = (unsigned int)p[size - 4] |
  123. ((unsigned int)p[size - 3] << 8) |
  124. ((unsigned int)p[size - 2] << 16) |
  125. ((unsigned int)p[size - 1] << 24);
  126. unsigned int pages, wpages, olen = 0;
  127. unsigned char *out, *ws;
  128. if (isize == 0 || isize > RAMFS_MAX_DECOMPRESSED)
  129. return -1;
  130. pages = (isize + 0xFFF) >> 12;
  131. wpages = (sizeof(struct inflate) + 0xFFF) >> 12;
  132. out = (unsigned char *)mm_alloc_pages(pages);
  133. if (!out)
  134. return -1;
  135. ws = (unsigned char *)mm_alloc_pages(wpages);
  136. if (!ws) {
  137. unsigned int i;
  138. for (i = 0; i < pages; i++)
  139. mm_free_page((unsigned int)out + i * 0x1000);
  140. return -1;
  141. }
  142. if (inflate_gzip_mem(pack, size, out, isize, &olen, ws) != 0 ||
  143. olen != isize) {
  144. unsigned int i;
  145. for (i = 0; i < pages; i++)
  146. mm_free_page((unsigned int)out + i * 0x1000);
  147. for (i = 0; i < wpages; i++)
  148. mm_free_page((unsigned int)ws + i * 0x1000);
  149. return -1;
  150. }
  151. {
  152. unsigned int i;
  153. for (i = 0; i < wpages; i++)
  154. mm_free_page((unsigned int)ws + i * 0x1000);
  155. }
  156. h = ramfs_validate(out, isize);
  157. if (!h) {
  158. unsigned int i;
  159. for (i = 0; i < pages; i++)
  160. mm_free_page((unsigned int)out + i * 0x1000);
  161. return -1;
  162. }
  163. pack_base = out;
  164. pack_size = isize;
  165. pack_hdr = h;
  166. pack_pages = pages;
  167. return 0;
  168. }
  169. h = ramfs_validate(pack, size);
  170. if (!h)
  171. return -1;
  172. pack_base = (const unsigned char *)pack;
  173. pack_size = size;
  174. pack_hdr = h;
  175. pack_pages = 0;
  176. return 0;
  177. }
  178. int ramfs_replace(const void *pack, unsigned int size)
  179. {
  180. const struct ramfs_hdr *h = ramfs_validate(pack, size);
  181. const unsigned char *old_base = pack_base;
  182. unsigned int old_size = pack_size, old_pages = pack_pages;
  183. unsigned int i;
  184. if (!h)
  185. return -1;
  186. pack_base = (const unsigned char *)pack;
  187. pack_size = size;
  188. pack_hdr = h;
  189. pack_pages = (size + 0xFFFu) >> 12;
  190. /* Free a previously received pack. */
  191. for (i = 0; i < old_pages; i++)
  192. mm_free_page((unsigned int)old_base + i * 0x1000);
  193. (void)old_size;
  194. return 0;
  195. }
  196. const void *ramfs_lookup(const char *path, unsigned int *size)
  197. {
  198. const struct ramfs_ent *ents;
  199. unsigned int i;
  200. if (!pack_hdr || !path_ok(path))
  201. return 0;
  202. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  203. for (i = 0; i < pack_hdr->count; i++) {
  204. if (str_eq(ents[i].path, path)) {
  205. if (size)
  206. *size = ents[i].size;
  207. return pack_base + ents[i].off;
  208. }
  209. }
  210. return 0;
  211. }
  212. int ramfs_list(const char *dir, unsigned int index, struct ramfs_dirent *out)
  213. {
  214. const struct ramfs_ent *ents;
  215. unsigned int i, seen = 0;
  216. if (!pack_hdr)
  217. return 0;
  218. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  219. for (i = 0; i < pack_hdr->count; i++) {
  220. const char *name;
  221. unsigned int j, len;
  222. if (dir[0] == '\0') {
  223. /* Root: the unique first components, reported as dirs. */
  224. unsigned int dup = 0;
  225. for (j = 0; j < i; j++) {
  226. if (first_component_same(ents[i].path, ents[j].path)) {
  227. dup = 1;
  228. break;
  229. }
  230. }
  231. if (dup)
  232. continue;
  233. len = first_len(ents[i].path);
  234. if (len >= RAMFS_PATH_MAX)
  235. continue;
  236. if (seen != index) {
  237. seen++;
  238. continue;
  239. }
  240. for (j = 0; j < len; j++)
  241. out->name[j] = ents[i].path[j];
  242. out->name[len] = '\0';
  243. out->size = 0;
  244. out->is_dir = 1;
  245. return 1;
  246. }
  247. name = strip_dir(ents[i].path, dir);
  248. if (!name)
  249. continue;
  250. if (seen != index) {
  251. seen++;
  252. continue;
  253. }
  254. len = 0;
  255. while (name[len] && name[len] != '/' && len < RAMFS_PATH_MAX - 1)
  256. len++;
  257. for (j = 0; j < len; j++)
  258. out->name[j] = name[j];
  259. out->name[len] = '\0';
  260. out->size = ents[i].size;
  261. out->is_dir = (name[len] == '/') ? 1 : 0;
  262. return 1;
  263. }
  264. return 0;
  265. }
  266. unsigned int ramfs_count(void)
  267. {
  268. return pack_hdr ? pack_hdr->count : 0;
  269. }
  270. unsigned int ramfs_pack_size(void)
  271. {
  272. return pack_hdr ? pack_size : 0;
  273. }