ramfs.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "mm.h"
  9. #include "serial.h"
  10. struct ramfs_hdr {
  11. char magic[8];
  12. unsigned int count;
  13. unsigned int total;
  14. };
  15. struct ramfs_ent {
  16. char path[RAMFS_PATH_MAX];
  17. unsigned int off;
  18. unsigned int size;
  19. };
  20. static const unsigned char *pack_base;
  21. static unsigned int pack_size;
  22. static const struct ramfs_hdr *pack_hdr;
  23. static unsigned int pack_pages; /* heap pages, when not embedded */
  24. static int str_eq(const char *a, const char *b)
  25. {
  26. unsigned int i = 0;
  27. while (a[i] && b[i] && a[i] == b[i])
  28. i++;
  29. return a[i] == '\0' && b[i] == '\0';
  30. }
  31. static unsigned int first_len(const char *path)
  32. {
  33. unsigned int n = 0;
  34. while (path[n] && path[n] != '/')
  35. n++;
  36. return n;
  37. }
  38. /* Same first component ("bin/a" vs "bin/b" -> 1)? */
  39. static int first_component_same(const char *a, const char *b)
  40. {
  41. unsigned int i, na = first_len(a), nb = first_len(b);
  42. if (na != nb)
  43. return 0;
  44. for (i = 0; i < na; i++)
  45. if (a[i] != b[i])
  46. return 0;
  47. return 1;
  48. }
  49. /* Skip a leading "dir/" prefix; NULL when the entry is not inside `dir`. */
  50. static const char *strip_dir(const char *path, const char *dir)
  51. {
  52. unsigned int i = 0;
  53. if (dir[0] == '\0')
  54. return path;
  55. while (dir[i] && path[i] == dir[i])
  56. i++;
  57. if (dir[i] != '\0')
  58. return 0;
  59. if (path[i] == '/')
  60. return path + i + 1;
  61. return 0;
  62. }
  63. /* Basic name check: no empties, no "." / "..". */
  64. static int path_ok(const char *path)
  65. {
  66. unsigned int i = 0, len = 0;
  67. if (!path || !path[0])
  68. return 0;
  69. for (;;) {
  70. char c = path[i];
  71. if (c == '\0' || c == '/') {
  72. if (len == 0)
  73. return 0;
  74. if (len == 1 && path[i - 1] == '.')
  75. return 0;
  76. if (len == 2 && path[i - 1] == '.' && path[i - 2] == '.')
  77. return 0;
  78. len = 0;
  79. if (c == '\0')
  80. return 1;
  81. } else {
  82. len++;
  83. }
  84. i++;
  85. }
  86. }
  87. /* Shape + bounds check of a pack; NULL when it is not a valid pack. */
  88. static const struct ramfs_hdr *ramfs_validate(const void *pack, unsigned int size)
  89. {
  90. const struct ramfs_hdr *h = (const struct ramfs_hdr *)pack;
  91. const struct ramfs_ent *ents;
  92. unsigned int i, need;
  93. if (size < sizeof(*h))
  94. return 0;
  95. if (h->magic[0] != 'N' || h->magic[1] != 'B' || h->magic[2] != 'P' ||
  96. h->magic[3] != 'A' || h->magic[4] != 'K' || h->magic[5] != '1')
  97. return 0;
  98. if (h->count == 0 || h->count > RAMFS_MAX_FILES)
  99. return 0;
  100. need = (unsigned int)(sizeof(*h) + h->count * sizeof(*ents));
  101. if (need > size || h->total > size || h->total < need)
  102. return 0;
  103. ents = (const struct ramfs_ent *)((const unsigned char *)pack + sizeof(*h));
  104. for (i = 0; i < h->count; i++) {
  105. if (!path_ok(ents[i].path))
  106. return 0;
  107. if (ents[i].off > size || ents[i].size > size - ents[i].off)
  108. return 0;
  109. }
  110. return h;
  111. }
  112. int ramfs_init(const void *pack, unsigned int size)
  113. {
  114. const struct ramfs_hdr *h = ramfs_validate(pack, size);
  115. if (!h)
  116. return -1;
  117. pack_base = (const unsigned char *)pack;
  118. pack_size = size;
  119. pack_hdr = h;
  120. pack_pages = 0;
  121. return 0;
  122. }
  123. int ramfs_replace(const void *pack, unsigned int size)
  124. {
  125. const struct ramfs_hdr *h = ramfs_validate(pack, size);
  126. const unsigned char *old_base = pack_base;
  127. unsigned int old_size = pack_size, old_pages = pack_pages;
  128. unsigned int i;
  129. if (!h)
  130. return -1;
  131. pack_base = (const unsigned char *)pack;
  132. pack_size = size;
  133. pack_hdr = h;
  134. pack_pages = (size + 0xFFFu) >> 12;
  135. /* Free a previously received pack. */
  136. for (i = 0; i < old_pages; i++)
  137. mm_free_page((unsigned int)old_base + i * 0x1000);
  138. (void)old_size;
  139. return 0;
  140. }
  141. const void *ramfs_lookup(const char *path, unsigned int *size)
  142. {
  143. const struct ramfs_ent *ents;
  144. unsigned int i;
  145. if (!pack_hdr || !path_ok(path))
  146. return 0;
  147. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  148. for (i = 0; i < pack_hdr->count; i++) {
  149. if (str_eq(ents[i].path, path)) {
  150. if (size)
  151. *size = ents[i].size;
  152. return pack_base + ents[i].off;
  153. }
  154. }
  155. return 0;
  156. }
  157. int ramfs_list(const char *dir, unsigned int index, struct ramfs_dirent *out)
  158. {
  159. const struct ramfs_ent *ents;
  160. unsigned int i, seen = 0;
  161. if (!pack_hdr)
  162. return 0;
  163. ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
  164. for (i = 0; i < pack_hdr->count; i++) {
  165. const char *name;
  166. unsigned int j, len;
  167. if (dir[0] == '\0') {
  168. /* Root: the unique first components, reported as dirs. */
  169. unsigned int dup = 0;
  170. for (j = 0; j < i; j++) {
  171. if (first_component_same(ents[i].path, ents[j].path)) {
  172. dup = 1;
  173. break;
  174. }
  175. }
  176. if (dup)
  177. continue;
  178. len = first_len(ents[i].path);
  179. if (len >= RAMFS_PATH_MAX)
  180. continue;
  181. if (seen != index) {
  182. seen++;
  183. continue;
  184. }
  185. for (j = 0; j < len; j++)
  186. out->name[j] = ents[i].path[j];
  187. out->name[len] = '\0';
  188. out->size = 0;
  189. out->is_dir = 1;
  190. return 1;
  191. }
  192. name = strip_dir(ents[i].path, dir);
  193. if (!name)
  194. continue;
  195. if (seen != index) {
  196. seen++;
  197. continue;
  198. }
  199. len = 0;
  200. while (name[len] && name[len] != '/' && len < RAMFS_PATH_MAX - 1)
  201. len++;
  202. for (j = 0; j < len; j++)
  203. out->name[j] = name[j];
  204. out->name[len] = '\0';
  205. out->size = ents[i].size;
  206. out->is_dir = (name[len] == '/') ? 1 : 0;
  207. return 1;
  208. }
  209. return 0;
  210. }
  211. unsigned int ramfs_count(void)
  212. {
  213. return pack_hdr ? pack_hdr->count : 0;
  214. }
  215. unsigned int ramfs_pack_size(void)
  216. {
  217. return pack_hdr ? pack_size : 0;
  218. }