cramfs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  85. {
  86. char *filename;
  87. int size;
  88. ulong offset = image_load_addr;
  89. char *offset_virt;
  90. struct part_info part;
  91. struct mtd_device dev;
  92. struct mtdids id;
  93. ulong addr;
  94. addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
  95. /* hack! */
  96. /* cramfs_* only supports NOR flash chips */
  97. /* fake the device type */
  98. id.type = MTD_DEV_TYPE_NOR;
  99. id.num = 0;
  100. dev.id = &id;
  101. part.dev = &dev;
  102. /* fake the address offset */
  103. part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
  104. /* pre-set Boot file name */
  105. filename = env_get("bootfile");
  106. if (!filename)
  107. filename = "uImage";
  108. if (argc == 2) {
  109. filename = argv[1];
  110. }
  111. if (argc == 3) {
  112. offset = simple_strtoul(argv[1], NULL, 0);
  113. image_load_addr = offset;
  114. filename = argv[2];
  115. }
  116. offset_virt = map_sysmem(offset, 0);
  117. size = 0;
  118. if (cramfs_check(&part))
  119. size = cramfs_load (offset_virt, &part, filename);
  120. if (size > 0) {
  121. printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
  122. size, offset);
  123. env_set_hex("filesize", size);
  124. } else {
  125. printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
  126. }
  127. unmap_sysmem(offset_virt);
  128. unmap_sysmem((void *)(uintptr_t)part.offset);
  129. return !(size > 0);
  130. }
  131. /**
  132. * Routine implementing u-boot ls command which lists content of a given
  133. * directory at location 'cramfsaddr'.
  134. * cramfsaddr is an evironment variable.
  135. *
  136. * @param cmdtp command internal data
  137. * @param flag command flag
  138. * @param argc number of arguments supplied to the command
  139. * @param argv arguments list
  140. * @return 0 on success, 1 otherwise
  141. */
  142. int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  143. {
  144. char *filename = "/";
  145. int ret;
  146. struct part_info part;
  147. struct mtd_device dev;
  148. struct mtdids id;
  149. ulong addr;
  150. addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
  151. /* hack! */
  152. /* cramfs_* only supports NOR flash chips */
  153. /* fake the device type */
  154. id.type = MTD_DEV_TYPE_NOR;
  155. id.num = 0;
  156. dev.id = &id;
  157. part.dev = &dev;
  158. /* fake the address offset */
  159. part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
  160. if (argc == 2)
  161. filename = argv[1];
  162. ret = 0;
  163. if (cramfs_check(&part))
  164. ret = cramfs_ls (&part, filename);
  165. unmap_sysmem((void *)(uintptr_t)part.offset);
  166. return ret ? 0 : 1;
  167. }
  168. /* command line only */
  169. /***************************************************/
  170. U_BOOT_CMD(
  171. cramfsload, 3, 0, do_cramfs_load,
  172. "load binary file from a filesystem image",
  173. "[ off ] [ filename ]\n"
  174. " - load binary file from address 'cramfsaddr'\n"
  175. " with offset 'off'\n"
  176. );
  177. U_BOOT_CMD(
  178. cramfsls, 2, 1, do_cramfs_ls,
  179. "list files in a directory (default /)",
  180. "[ directory ]\n"
  181. " - list files in a directory.\n"
  182. );