ramfs.c 8.7 KB

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