ramfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. unsigned int pages;
  119. if (!pack || size < sizeof(struct ramfs_hdr) ||
  120. size > RAMFS_MAX_DECOMPRESSED)
  121. return -1;
  122. /* The embedded pack is gzip-compressed (the same inflate code that the
  123. * PNG decoder uses); a raw pack is still accepted, which is what a
  124. * kexec reload passes when it carries the running rootfs over. Either
  125. * way the pack is copied into page-allocated memory, so the boot-time
  126. * region can be released. */
  127. if (size >= 18 && p[0] == 0x1F && p[1] == 0x8B) {
  128. unsigned int isize = (unsigned int)p[size - 4] |
  129. ((unsigned int)p[size - 3] << 8) |
  130. ((unsigned int)p[size - 2] << 16) |
  131. ((unsigned int)p[size - 1] << 24);
  132. unsigned int wpages, olen = 0;
  133. unsigned char *out, *ws;
  134. if (isize == 0 || isize > RAMFS_MAX_DECOMPRESSED)
  135. return -1;
  136. pages = (isize + 0xFFF) >> 12;
  137. wpages = (sizeof(struct inflate) + 0xFFF) >> 12;
  138. out = (unsigned char *)mm_alloc_pages(pages);
  139. if (!out)
  140. return -1;
  141. ws = (unsigned char *)mm_alloc_pages(wpages);
  142. if (!ws) {
  143. unsigned int i;
  144. for (i = 0; i < pages; i++)
  145. mm_free_page((unsigned int)out + i * 0x1000);
  146. return -1;
  147. }
  148. if (inflate_gzip_mem(pack, size, out, isize, &olen, ws) != 0 ||
  149. olen != isize) {
  150. unsigned int i;
  151. for (i = 0; i < pages; i++)
  152. mm_free_page((unsigned int)out + i * 0x1000);
  153. for (i = 0; i < wpages; i++)
  154. mm_free_page((unsigned int)ws + i * 0x1000);
  155. return -1;
  156. }
  157. {
  158. unsigned int i;
  159. for (i = 0; i < wpages; i++)
  160. mm_free_page((unsigned int)ws + i * 0x1000);
  161. }
  162. h = ramfs_validate(out, isize);
  163. if (!h) {
  164. unsigned int i;
  165. for (i = 0; i < pages; i++)
  166. mm_free_page((unsigned int)out + i * 0x1000);
  167. return -1;
  168. }
  169. pack_base = out;
  170. pack_size = isize;
  171. pack_hdr = h;
  172. pack_pages = pages;
  173. return 0;
  174. }
  175. /* Raw pack: copy it so the caller can release the boot region. */
  176. pages = (size + 0xFFF) >> 12;
  177. {
  178. unsigned char *copy = (unsigned char *)mm_alloc_pages(pages);
  179. unsigned int i;
  180. if (!copy)
  181. return -1;
  182. for (i = 0; i < size; i++)
  183. copy[i] = p[i];
  184. h = ramfs_validate(copy, size);
  185. if (!h) {
  186. for (i = 0; i < pages; i++)
  187. mm_free_page((unsigned int)copy + i * 0x1000);
  188. return -1;
  189. }
  190. pack_base = copy;
  191. }
  192. pack_size = size;
  193. pack_hdr = h;
  194. pack_pages = pages;
  195. return 0;
  196. }
  197. int ramfs_replace(const void *pack, unsigned int size)
  198. {
  199. const struct ramfs_hdr *h = ramfs_validate(pack, size);
  200. const unsigned char *old_base = pack_base;
  201. unsigned int old_size = pack_size, old_pages = pack_pages;
  202. unsigned int i;
  203. if (!h)
  204. return -1;
  205. pack_base = (const unsigned char *)pack;
  206. pack_size = size;
  207. pack_hdr = h;
  208. pack_pages = (size + 0xFFFu) >> 12;
  209. /* Free a previously received pack. */
  210. for (i = 0; i < old_pages; i++)
  211. mm_free_page((unsigned int)old_base + i * 0x1000);
  212. (void)old_size;
  213. return 0;
  214. }
  215. const void *ramfs_lookup(const char *path, unsigned int *size)
  216. {
  217. const struct ramfs_ent *ents;
  218. unsigned int i;
  219. if (!pack_hdr || !path_ok(path))
  220. return 0;
  221. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  222. for (i = 0; i < pack_hdr->count; i++) {
  223. if (str_eq(ents[i].path, path)) {
  224. if (size)
  225. *size = ents[i].size;
  226. return pack_base + ents[i].off;
  227. }
  228. }
  229. return 0;
  230. }
  231. int ramfs_list(const char *dir, unsigned int index, struct ramfs_dirent *out)
  232. {
  233. const struct ramfs_ent *ents;
  234. unsigned int i, seen = 0;
  235. if (!pack_hdr)
  236. return 0;
  237. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  238. for (i = 0; i < pack_hdr->count; i++) {
  239. const char *name;
  240. unsigned int j, len;
  241. if (dir[0] == '\0') {
  242. /* Root: the unique first components, reported as dirs. */
  243. unsigned int dup = 0;
  244. for (j = 0; j < i; j++) {
  245. if (first_component_same(ents[i].path, ents[j].path)) {
  246. dup = 1;
  247. break;
  248. }
  249. }
  250. if (dup)
  251. continue;
  252. len = first_len(ents[i].path);
  253. if (len >= RAMFS_PATH_MAX)
  254. continue;
  255. if (seen != index) {
  256. seen++;
  257. continue;
  258. }
  259. for (j = 0; j < len; j++)
  260. out->name[j] = ents[i].path[j];
  261. out->name[len] = '\0';
  262. out->size = 0;
  263. out->is_dir = 1;
  264. return 1;
  265. }
  266. name = strip_dir(ents[i].path, dir);
  267. if (!name)
  268. continue;
  269. if (seen != index) {
  270. seen++;
  271. continue;
  272. }
  273. len = 0;
  274. while (name[len] && name[len] != '/' && len < RAMFS_PATH_MAX - 1)
  275. len++;
  276. for (j = 0; j < len; j++)
  277. out->name[j] = name[j];
  278. out->name[len] = '\0';
  279. out->size = ents[i].size;
  280. out->is_dir = (name[len] == '/') ? 1 : 0;
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. unsigned int ramfs_count(void)
  286. {
  287. return pack_hdr ? pack_hdr->count : 0;
  288. }
  289. unsigned int ramfs_pack_size(void)
  290. {
  291. return pack_hdr ? pack_size : 0;
  292. }
  293. /* Base of the owned pack (NULL when there is no pack or it is not ours).
  294. * A kexec reload passes this to the new kernel so a kernel-only update can
  295. * keep the commands. */
  296. const void *ramfs_pack_base(void)
  297. {
  298. return pack_pages ? pack_base : 0;
  299. }