cramfs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * based on: cmd_jffs2.c
  5. *
  6. * Add support for a CRAMFS located in RAM
  7. */
  8. /*
  9. * CRAMFS support
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <env.h>
  14. #include <image.h>
  15. #include <malloc.h>
  16. #include <mapmem.h>
  17. #include <linux/list.h>
  18. #include <linux/ctype.h>
  19. #include <jffs2/jffs2.h>
  20. #include <jffs2/load_kernel.h>
  21. #include <cramfs/cramfs_fs.h>
  22. #include <asm/io.h>
  23. /* enable/disable debugging messages */
  24. #define DEBUG_CRAMFS
  25. #undef DEBUG_CRAMFS
  26. #ifdef DEBUG_CRAMFS
  27. # define DEBUGF(fmt, args...) printf(fmt ,##args)
  28. #else
  29. # define DEBUGF(fmt, args...)
  30. #endif
  31. #include <flash.h>
  32. #ifndef CONFIG_MTD_NOR_FLASH
  33. # define OFFSET_ADJUSTMENT 0
  34. #else
  35. # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
  36. #endif
  37. #ifndef CONFIG_FS_JFFS2
  38. #include <linux/stat.h>
  39. char *mkmodestr(unsigned long mode, char *str)
  40. {
  41. static const char *l = "xwr";
  42. int mask = 1, i;
  43. char c;
  44. switch (mode & S_IFMT) {
  45. case S_IFDIR: str[0] = 'd'; break;
  46. case S_IFBLK: str[0] = 'b'; break;
  47. case S_IFCHR: str[0] = 'c'; break;
  48. case S_IFIFO: str[0] = 'f'; break;
  49. case S_IFLNK: str[0] = 'l'; break;
  50. case S_IFSOCK: str[0] = 's'; break;
  51. case S_IFREG: str[0] = '-'; break;
  52. default: str[0] = '?';
  53. }
  54. for(i = 0; i < 9; i++) {
  55. c = l[i%3];
  56. str[9-i] = (mode & mask)?c:'-';
  57. mask = mask<<1;
  58. }
  59. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  60. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  61. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  62. str[10] = '\0';
  63. return str;
  64. }
  65. #endif /* CONFIG_FS_JFFS2 */
  66. extern int cramfs_check (struct part_info *info);
  67. extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
  68. extern int cramfs_ls (struct part_info *info, char *filename);
  69. extern int cramfs_info (struct part_info *info);
  70. /***************************************************/
  71. /* U-Boot commands */
  72. /***************************************************/
  73. /**
  74. * Routine implementing fsload u-boot command. This routine tries to load
  75. * a requested file from cramfs filesystem at location 'cramfsaddr'.
  76. * cramfsaddr is an evironment variable.
  77. *
  78. * @param cmdtp command internal data
  79. * @param flag command flag
  80. * @param argc number of arguments supplied to the command
  81. * @param argv arguments list
  82. * @return 0 on success, 1 otherwise
  83. */
  84. int do_cramfs_load(struct cmd_tbl *cmdtp, int flag, int argc,
  85. char *const argv[])
  86. {
  87. char *filename;
  88. int size;
  89. ulong offset = image_load_addr;
  90. char *offset_virt;
  91. struct part_info part;
  92. struct mtd_device dev;
  93. struct mtdids id;
  94. ulong addr;
  95. addr = hextoul(env_get("cramfsaddr"), NULL);
  96. /* hack! */
  97. /* cramfs_* only supports NOR flash chips */
  98. /* fake the device type */
  99. id.type = MTD_DEV_TYPE_NOR;
  100. id.num = 0;
  101. dev.id = &id;
  102. part.dev = &dev;
  103. /* fake the address offset */
  104. part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
  105. /* pre-set Boot file name */
  106. filename = env_get("bootfile");
  107. if (!filename)
  108. filename = "uImage";
  109. if (argc == 2) {
  110. filename = argv[1];
  111. }
  112. if (argc == 3) {
  113. offset = simple_strtoul(argv[1], NULL, 0);
  114. image_load_addr = offset;
  115. filename = argv[2];
  116. }
  117. offset_virt = map_sysmem(offset, 0);
  118. size = 0;
  119. if (cramfs_check(&part))
  120. size = cramfs_load (offset_virt, &part, filename);
  121. if (size > 0) {
  122. printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
  123. size, offset);
  124. env_set_hex("filesize", size);
  125. } else {
  126. printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
  127. }
  128. unmap_sysmem(offset_virt);
  129. unmap_sysmem((void *)(uintptr_t)part.offset);
  130. return !(size > 0);
  131. }
  132. /**
  133. * Routine implementing u-boot ls command which lists content of a given
  134. * directory at location 'cramfsaddr'.
  135. * cramfsaddr is an evironment variable.
  136. *
  137. * @param cmdtp command internal data
  138. * @param flag command flag
  139. * @param argc number of arguments supplied to the command
  140. * @param argv arguments list
  141. * @return 0 on success, 1 otherwise
  142. */
  143. int do_cramfs_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  144. {
  145. char *filename = "/";
  146. int ret;
  147. struct part_info part;
  148. struct mtd_device dev;
  149. struct mtdids id;
  150. ulong addr;
  151. addr = hextoul(env_get("cramfsaddr"), NULL);
  152. /* hack! */
  153. /* cramfs_* only supports NOR flash chips */
  154. /* fake the device type */
  155. id.type = MTD_DEV_TYPE_NOR;
  156. id.num = 0;
  157. dev.id = &id;
  158. part.dev = &dev;
  159. /* fake the address offset */
  160. part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
  161. if (argc == 2)
  162. filename = argv[1];
  163. ret = 0;
  164. if (cramfs_check(&part))
  165. ret = cramfs_ls (&part, filename);
  166. unmap_sysmem((void *)(uintptr_t)part.offset);
  167. return ret ? 0 : 1;
  168. }
  169. /* command line only */
  170. /***************************************************/
  171. U_BOOT_CMD(
  172. cramfsload, 3, 0, do_cramfs_load,
  173. "load binary file from a filesystem image",
  174. "[ off ] [ filename ]\n"
  175. " - load binary file from address 'cramfsaddr'\n"
  176. " with offset 'off'\n"
  177. );
  178. U_BOOT_CMD(
  179. cramfsls, 2, 1, do_cramfs_ls,
  180. "list files in a directory (default /)",
  181. "[ directory ]\n"
  182. " - list files in a directory.\n"
  183. );