ramfs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "cursor.h"
  12. #define RAMFS_MAX_DECOMPRESSED (8u * 1024u * 1024u)
  13. struct ramfs_hdr {
  14. char magic[8];
  15. unsigned int count;
  16. unsigned int total;
  17. };
  18. struct ramfs_ent {
  19. char path[RAMFS_PATH_MAX];
  20. unsigned int off;
  21. unsigned int size;
  22. };
  23. static const unsigned char *pack_base;
  24. static unsigned int pack_size;
  25. static const struct ramfs_hdr *pack_hdr;
  26. static unsigned int pack_pages; /* heap pages, when not embedded */
  27. /* Boot-time rootfs image, retained for `install self`. */
  28. static const unsigned char *boot_base;
  29. static unsigned int boot_size;
  30. static int boot_gzip;
  31. static int boot_owned;
  32. void ramfs_set_boot_image(const void *base, unsigned int size, int gzip)
  33. {
  34. boot_base = (const unsigned char *)base;
  35. boot_size = size;
  36. boot_gzip = gzip;
  37. boot_owned = gzip ? 1 : 0;
  38. }
  39. int ramfs_boot_image(const void **base, unsigned int *size, int *gzip)
  40. {
  41. if (!boot_base || !boot_size)
  42. return 0;
  43. if (base)
  44. *base = boot_base;
  45. if (size)
  46. *size = boot_size;
  47. if (gzip)
  48. *gzip = boot_gzip;
  49. return 1;
  50. }
  51. static int str_eq(const char *a, const char *b)
  52. {
  53. unsigned int i = 0;
  54. while (a[i] && b[i] && a[i] == b[i])
  55. i++;
  56. return a[i] == '\0' && b[i] == '\0';
  57. }
  58. static unsigned int first_len(const char *path)
  59. {
  60. unsigned int n = 0;
  61. while (path[n] && path[n] != '/')
  62. n++;
  63. return n;
  64. }
  65. /* Same first component ("bin/a" vs "bin/b" -> 1)? */
  66. static int first_component_same(const char *a, const char *b)
  67. {
  68. unsigned int i, na = first_len(a), nb = first_len(b);
  69. if (na != nb)
  70. return 0;
  71. for (i = 0; i < na; i++)
  72. if (a[i] != b[i])
  73. return 0;
  74. return 1;
  75. }
  76. /* Skip a leading "dir/" prefix; NULL when the entry is not inside `dir`. */
  77. static const char *strip_dir(const char *path, const char *dir)
  78. {
  79. unsigned int i = 0;
  80. if (dir[0] == '\0')
  81. return path;
  82. while (dir[i] && path[i] == dir[i])
  83. i++;
  84. if (dir[i] != '\0')
  85. return 0;
  86. if (path[i] == '/')
  87. return path + i + 1;
  88. return 0;
  89. }
  90. /* Basic name check: no empties, no "." / "..". */
  91. static int path_ok(const char *path)
  92. {
  93. unsigned int i = 0, len = 0;
  94. if (!path || !path[0])
  95. return 0;
  96. for (;;) {
  97. char c = path[i];
  98. if (c == '\0' || c == '/') {
  99. if (len == 0)
  100. return 0;
  101. if (len == 1 && path[i - 1] == '.')
  102. return 0;
  103. if (len == 2 && path[i - 1] == '.' && path[i - 2] == '.')
  104. return 0;
  105. len = 0;
  106. if (c == '\0')
  107. return 1;
  108. } else {
  109. len++;
  110. }
  111. i++;
  112. }
  113. }
  114. /* Shape + bounds check of a pack; NULL when it is not a valid pack. */
  115. static const struct ramfs_hdr *ramfs_validate(const void *pack, unsigned int size)
  116. {
  117. const struct ramfs_hdr *h = (const struct ramfs_hdr *)pack;
  118. const struct ramfs_ent *ents;
  119. unsigned int i, need;
  120. if (size < sizeof(*h))
  121. return 0;
  122. if (h->magic[0] != 'N' || h->magic[1] != 'B' || h->magic[2] != 'P' ||
  123. h->magic[3] != 'A' || h->magic[4] != 'K' || h->magic[5] != '1')
  124. return 0;
  125. if (h->count == 0 || h->count > RAMFS_MAX_FILES)
  126. return 0;
  127. need = (unsigned int)(sizeof(*h) + h->count * sizeof(*ents));
  128. if (need > size || h->total > size || h->total < need)
  129. return 0;
  130. ents = (const struct ramfs_ent *)((const unsigned char *)pack + sizeof(*h));
  131. for (i = 0; i < h->count; i++) {
  132. if (!path_ok(ents[i].path))
  133. return 0;
  134. if (ents[i].off > size || ents[i].size > size - ents[i].off)
  135. return 0;
  136. }
  137. return h;
  138. }
  139. int ramfs_init(const void *pack, unsigned int size)
  140. {
  141. const struct ramfs_hdr *h;
  142. const unsigned char *p = (const unsigned char *)pack;
  143. unsigned int pages;
  144. if (!pack || size < sizeof(struct ramfs_hdr) ||
  145. size > RAMFS_MAX_DECOMPRESSED)
  146. return -1;
  147. /* The embedded pack is gzip-compressed (the same inflate code that the
  148. * PNG decoder uses); a raw pack is still accepted, which is what a
  149. * kexec reload passes when it carries the running rootfs over. Either
  150. * way the pack is copied into page-allocated memory, so the boot-time
  151. * region can be released. */
  152. if (size >= 18 && p[0] == 0x1F && p[1] == 0x8B) {
  153. unsigned int isize = (unsigned int)p[size - 4] |
  154. ((unsigned int)p[size - 3] << 8) |
  155. ((unsigned int)p[size - 2] << 16) |
  156. ((unsigned int)p[size - 1] << 24);
  157. unsigned int wpages, olen = 0;
  158. unsigned char *out, *ws;
  159. if (isize == 0 || isize > RAMFS_MAX_DECOMPRESSED)
  160. return -1;
  161. pages = (isize + 0xFFF) >> 12;
  162. wpages = (sizeof(struct inflate) + 0xFFF) >> 12;
  163. out = (unsigned char *)mm_alloc_pages(pages);
  164. if (!out)
  165. return -1;
  166. ws = (unsigned char *)mm_alloc_pages(wpages);
  167. if (!ws) {
  168. unsigned int i;
  169. for (i = 0; i < pages; i++)
  170. mm_free_page((unsigned int)out + i * 0x1000);
  171. return -1;
  172. }
  173. if (inflate_gzip_mem(pack, size, out, isize, &olen, ws) != 0 ||
  174. olen != isize) {
  175. unsigned int i;
  176. for (i = 0; i < pages; i++)
  177. mm_free_page((unsigned int)out + i * 0x1000);
  178. for (i = 0; i < wpages; i++)
  179. mm_free_page((unsigned int)ws + i * 0x1000);
  180. return -1;
  181. }
  182. {
  183. unsigned int i;
  184. for (i = 0; i < wpages; i++)
  185. mm_free_page((unsigned int)ws + i * 0x1000);
  186. }
  187. h = ramfs_validate(out, isize);
  188. if (!h) {
  189. unsigned int i;
  190. for (i = 0; i < pages; i++)
  191. mm_free_page((unsigned int)out + i * 0x1000);
  192. return -1;
  193. }
  194. pack_base = out;
  195. pack_size = isize;
  196. pack_hdr = h;
  197. pack_pages = pages;
  198. return 0;
  199. }
  200. /* Raw pack: copy it so the caller can release the boot region. */
  201. pages = (size + 0xFFF) >> 12;
  202. {
  203. unsigned char *copy = (unsigned char *)mm_alloc_pages(pages);
  204. unsigned int i;
  205. if (!copy)
  206. return -1;
  207. for (i = 0; i < size; i++)
  208. copy[i] = p[i];
  209. h = ramfs_validate(copy, size);
  210. if (!h) {
  211. for (i = 0; i < pages; i++)
  212. mm_free_page((unsigned int)copy + i * 0x1000);
  213. return -1;
  214. }
  215. pack_base = copy;
  216. }
  217. pack_size = size;
  218. pack_hdr = h;
  219. pack_pages = pages;
  220. return 0;
  221. }
  222. int ramfs_replace(const void *pack, unsigned int size)
  223. {
  224. const struct ramfs_hdr *h = ramfs_validate(pack, size);
  225. const unsigned char *old_base = pack_base;
  226. unsigned int old_size = pack_size, old_pages = pack_pages;
  227. unsigned int i;
  228. if (!h)
  229. return -1;
  230. pack_base = (const unsigned char *)pack;
  231. pack_size = size;
  232. pack_hdr = h;
  233. pack_pages = (size + 0xFFFu) >> 12;
  234. /* A runtime pack replacement no longer matches the boot image that
  235. * `install self` would write, so drop (and, for a gzip boot image,
  236. * release) it. */
  237. if (boot_owned && boot_base && boot_base != (const unsigned char *)pack)
  238. mm_free_range((unsigned int)boot_base, boot_size);
  239. boot_base = 0;
  240. boot_size = 0;
  241. boot_gzip = 0;
  242. boot_owned = 0;
  243. /* Free a previously received pack. */
  244. for (i = 0; i < old_pages; i++)
  245. mm_free_page((unsigned int)old_base + i * 0x1000);
  246. (void)old_size;
  247. /* The new pack may carry different cursors. */
  248. cursor_load();
  249. return 0;
  250. }
  251. const void *ramfs_lookup(const char *path, unsigned int *size)
  252. {
  253. const struct ramfs_ent *ents;
  254. unsigned int i;
  255. if (!pack_hdr || !path_ok(path))
  256. return 0;
  257. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  258. for (i = 0; i < pack_hdr->count; i++) {
  259. if (str_eq(ents[i].path, path)) {
  260. if (size)
  261. *size = ents[i].size;
  262. return pack_base + ents[i].off;
  263. }
  264. }
  265. return 0;
  266. }
  267. int ramfs_list(const char *dir, unsigned int index, struct ramfs_dirent *out)
  268. {
  269. const struct ramfs_ent *ents;
  270. unsigned int i, seen = 0;
  271. if (!pack_hdr)
  272. return 0;
  273. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  274. for (i = 0; i < pack_hdr->count; i++) {
  275. const char *name;
  276. unsigned int j, len;
  277. if (dir[0] == '\0') {
  278. /* Root: the unique first components, reported as dirs. */
  279. unsigned int dup = 0;
  280. for (j = 0; j < i; j++) {
  281. if (first_component_same(ents[i].path, ents[j].path)) {
  282. dup = 1;
  283. break;
  284. }
  285. }
  286. if (dup)
  287. continue;
  288. len = first_len(ents[i].path);
  289. if (len >= RAMFS_PATH_MAX)
  290. continue;
  291. if (seen != index) {
  292. seen++;
  293. continue;
  294. }
  295. for (j = 0; j < len; j++)
  296. out->name[j] = ents[i].path[j];
  297. out->name[len] = '\0';
  298. out->size = 0;
  299. out->is_dir = 1;
  300. return 1;
  301. }
  302. name = strip_dir(ents[i].path, dir);
  303. if (!name)
  304. continue;
  305. if (seen != index) {
  306. seen++;
  307. continue;
  308. }
  309. len = 0;
  310. while (name[len] && name[len] != '/' && len < RAMFS_PATH_MAX - 1)
  311. len++;
  312. for (j = 0; j < len; j++)
  313. out->name[j] = name[j];
  314. out->name[len] = '\0';
  315. out->size = ents[i].size;
  316. out->is_dir = (name[len] == '/') ? 1 : 0;
  317. return 1;
  318. }
  319. return 0;
  320. }
  321. unsigned int ramfs_count(void)
  322. {
  323. return pack_hdr ? pack_hdr->count : 0;
  324. }
  325. unsigned int ramfs_pack_size(void)
  326. {
  327. return pack_hdr ? pack_size : 0;
  328. }
  329. /* Base of the owned pack (NULL when there is no pack or it is not ours).
  330. * A kexec reload passes this to the new kernel so a kernel-only update can
  331. * keep the commands. */
  332. const void *ramfs_pack_base(void)
  333. {
  334. return pack_pages ? pack_base : 0;
  335. }