spl_block.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * (C) Copyright 2013
  3. *
  4. * Ma Haijun <mahaijuns@gmail.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <spl.h>
  26. #include <asm/u-boot.h>
  27. #include <asm/utils.h>
  28. #include <version.h>
  29. #include <part.h>
  30. #include <fat.h>
  31. #include <ext4fs.h>
  32. /* should be implemented by board */
  33. extern void spl_block_device_init(void);
  34. block_dev_desc_t * spl_get_block_device(void)
  35. {
  36. block_dev_desc_t * device;
  37. spl_block_device_init();
  38. device = get_dev(CONFIG_SPL_BLOCKDEV_INTERFACE, CONFIG_SPL_BLOCKDEV_ID);
  39. if (!device) {
  40. printf("blk device %s%d not exists\n",
  41. CONFIG_SPL_BLOCKDEV_INTERFACE,
  42. CONFIG_SPL_BLOCKDEV_ID);
  43. hang();
  44. }
  45. return device;
  46. }
  47. #ifdef CONFIG_SPL_FAT_SUPPORT
  48. static int block_load_image_fat(const char *filename)
  49. {
  50. int err;
  51. struct image_header *header;
  52. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  53. sizeof(struct image_header));
  54. err = file_fat_read(filename, header, sizeof(struct image_header));
  55. if (err <= 0)
  56. goto end;
  57. spl_parse_image_header(header);
  58. err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
  59. end:
  60. if (err <= 0)
  61. printf("spl: error reading image %s, err - %d\n",
  62. filename, err);
  63. return (err <= 0);
  64. }
  65. #ifdef CONFIG_SPL_OS_BOOT
  66. static int block_load_image_fat_os(void)
  67. {
  68. int err;
  69. err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
  70. (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
  71. if (err <= 0) {
  72. return -1;
  73. }
  74. return block_load_image_fat(CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
  75. }
  76. #endif
  77. void spl_block_load_image(void)
  78. {
  79. int err;
  80. block_dev_desc_t * device;
  81. device = spl_get_block_device();
  82. err = fat_register_device(device, CONFIG_BLOCKDEV_FAT_BOOT_PARTITION);
  83. if (err) {
  84. printf("spl: fat register err - %d\n", err);
  85. hang();
  86. }
  87. #ifdef CONFIG_SPL_OS_BOOT
  88. if (spl_start_uboot() || block_load_image_fat_os())
  89. #endif
  90. {
  91. err = block_load_image_fat(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
  92. if (err)
  93. hang();
  94. }
  95. }
  96. #elif defined(CONFIG_SPL_EXT4_SUPPORT) /* end CONFIG_SPL_FAT_SUPPORT */
  97. static int block_load_image_ext4(const char *filename)
  98. {
  99. int err;
  100. struct image_header *header;
  101. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  102. sizeof(struct image_header));
  103. err = ext4_read_file(filename, header, 0, sizeof(struct image_header));
  104. if (err <= 0)
  105. goto end;
  106. spl_parse_image_header(header);
  107. err = ext4_read_file(filename, (u8 *)spl_image.load_addr, 0, 0);
  108. end:
  109. return (err <= 0);
  110. }
  111. #ifdef CONFIG_SPL_OS_BOOT
  112. static int block_load_image_ext4_os(void)
  113. {
  114. int err;
  115. err = ext4_read_file(CONFIG_SPL_EXT4_LOAD_ARGS_NAME,
  116. (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0, 0);
  117. if (err <= 0) {
  118. return -1;
  119. }
  120. return block_load_image_ext4(CONFIG_SPL_EXT4_LOAD_KERNEL_NAME);
  121. }
  122. #endif
  123. void spl_block_load_image(void)
  124. {
  125. int err;
  126. block_dev_desc_t * device;
  127. device = spl_get_block_device();
  128. err = ext4_register_device(device, CONFIG_BLOCKDEV_EXT4_BOOT_PARTITION);
  129. if (err) {
  130. hang();
  131. }
  132. #ifdef CONFIG_SPL_OS_BOOT
  133. if (spl_start_uboot() || block_load_image_ext4_os())
  134. #endif
  135. {
  136. err = block_load_image_ext4(CONFIG_SPL_EXT4_LOAD_PAYLOAD_NAME);
  137. if (err)
  138. hang();
  139. }
  140. }
  141. #else /* end CONFIG_SPL_EXT4_SUPPORT */
  142. static int block_load_image_raw(block_dev_desc_t * device, lbaint_t sector)
  143. {
  144. int n;
  145. u32 image_size_sectors;
  146. struct image_header *header;
  147. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  148. sizeof(struct image_header));
  149. /* read image header to find the image size & load address */
  150. n = device->block_read(device->dev, sector, 1, header);
  151. if (n != 1) {
  152. printf("spl: blk read err\n");
  153. return 1;
  154. }
  155. spl_parse_image_header(header);
  156. /* convert size to sectors - round up */
  157. image_size_sectors = (spl_image.size + 512 - 1) / 512;
  158. n = device->block_read(device->dev, sector, image_size_sectors,
  159. (void *)spl_image.load_addr);
  160. if (n != image_size_sectors) {
  161. printf("spl: blk read err\n");
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. #ifdef CONFIG_SPL_OS_BOOT
  167. static int block_load_image_raw_os(block_dev_desc_t * device)
  168. {
  169. int n;
  170. n = device->block_read(device->dev, CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTOR,
  171. CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTORS,
  172. (u32 *)CONFIG_SYS_SPL_ARGS_ADDR);
  173. /* flush cache after read */
  174. flush_cache(addr, CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTORS * 512);
  175. if (n != CONFIG_SYS_BLOCK_RAW_MODE_ARGS_SECTORS) {
  176. printf("args blk read error\n");
  177. return -1;
  178. }
  179. return block_load_image_raw(device, CONFIG_SYS_BLOCK_RAW_MODE_KERNEL_SECTOR);
  180. }
  181. #endif
  182. void spl_block_load_image(void)
  183. {
  184. int err;
  185. block_dev_desc_t * device;
  186. device = spl_get_block_device();
  187. #ifdef CONFIG_SPL_OS_BOOT
  188. if (spl_start_uboot() || block_load_image_raw_os(device))
  189. #endif
  190. {
  191. err = block_load_image_raw(device,
  192. CONFIG_SYS_BLOCK_RAW_MODE_U_BOOT_SECTOR);
  193. if (err)
  194. hang();
  195. }
  196. }
  197. #endif /* CONFIG_SPL_FAT_SUPPORT */