main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "uart.h"
  24. #include "atag.h"
  25. #include "fb.h"
  26. #include "console.h"
  27. #include "block.h"
  28. #include "vfs.h"
  29. #include "memchunk.h"
  30. #include "usb.h"
  31. #include "dwc_usb.h"
  32. #include "output.h"
  33. #include "log.h"
  34. #define UNUSED(x) (void)(x)
  35. uint32_t _atags;
  36. uint32_t _arm_m_type;
  37. char rpi_boot_name[] = "rpi_boot";
  38. static char *atag_cmd_line;
  39. static char *boot_cfg_names[] =
  40. {
  41. "/boot/rpi_boot.cfg",
  42. "/boot/rpi-boot.cfg",
  43. "/boot/grub/grub.cfg",
  44. 0
  45. };
  46. void atag_cb(struct atag *tag)
  47. {
  48. switch(tag->hdr.tag)
  49. {
  50. case ATAG_CORE:
  51. #ifdef ATAG_DEBUG
  52. puts("ATAG_CORE");
  53. if(tag->hdr.size == 5)
  54. {
  55. puts("flags");
  56. puthex(tag->u.core.flags);
  57. puts("");
  58. puts("pagesize");
  59. puthex(tag->u.core.pagesize);
  60. puts("");
  61. puts("rootdev");
  62. puthex(tag->u.core.rootdev);
  63. puts("");
  64. }
  65. #endif
  66. break;
  67. case ATAG_MEM:
  68. #ifdef ATAG_DEBUG
  69. puts("ATAG_MEM");
  70. puts("start");
  71. puthex(tag->u.mem.start);
  72. puts("");
  73. puts("size");
  74. puthex(tag->u.mem.size);
  75. puts("");
  76. #endif
  77. {
  78. uint32_t start = tag->u.mem.start;
  79. uint32_t size = tag->u.mem.size;
  80. if(start < 0x100000)
  81. start = 0x100000;
  82. size -= 0x100000;
  83. chunk_register_free(start, size);
  84. }
  85. break;
  86. case ATAG_NONE:
  87. break;
  88. case ATAG_CMDLINE:
  89. #ifdef ATAG_DEBUG
  90. puts("ATAG_CMDLINE");
  91. puts(&tag->u.cmdline.cmdline[0]);
  92. #endif
  93. atag_cmd_line = &tag->u.cmdline.cmdline[0];
  94. break;
  95. default:
  96. puts("Unknown ATAG");
  97. puthex(tag->hdr.tag);
  98. break;
  99. };
  100. puts("");
  101. }
  102. void libfs_init();
  103. extern int (*stdout_putc)(int);
  104. extern int (*stderr_putc)(int);
  105. extern int (*stream_putc)(int, FILE*);
  106. extern int def_stream_putc(int, FILE*);
  107. int cfg_parse(char *buf);
  108. void kernel_main(uint32_t boot_dev, uint32_t arm_m_type, uint32_t atags)
  109. {
  110. atag_cmd_line = (void *)0;
  111. _atags = atags;
  112. _arm_m_type = arm_m_type;
  113. UNUSED(boot_dev);
  114. // First use the serial console
  115. stdout_putc = split_putc;
  116. stderr_putc = split_putc;
  117. stream_putc = def_stream_putc;
  118. output_init();
  119. output_enable_uart();
  120. // Dump ATAGS
  121. parse_atags(atags, atag_cb);
  122. #ifdef ENABLE_FRAMEBUFFER
  123. int result = fb_init();
  124. if(result == 0)
  125. {
  126. puts("Successfully set up frame buffer");
  127. #ifdef DEBUG2
  128. printf("FB: width: %i, height: %i, bpp: %i\n",
  129. fb_get_width(), fb_get_height(),
  130. fb_get_bpp());
  131. #endif
  132. }
  133. else
  134. {
  135. puts("Error setting up framebuffer:");
  136. puthex(result);
  137. }
  138. #endif
  139. // Switch to the framebuffer for output
  140. output_enable_fb();
  141. // Allocate a log in memory
  142. #ifdef ENABLE_CONSOLE_LOGFILE
  143. register_log_file(NULL, 0x1000);
  144. output_enable_log();
  145. #endif
  146. printf("Welcome to Rpi bootloader\n");
  147. printf("Compiled on %s at %s\n", __DATE__, __TIME__);
  148. printf("ARM system type is %x\n", arm_m_type);
  149. if(atag_cmd_line != (void *)0)
  150. printf("Command line: %s\n", atag_cmd_line);
  151. // Register the various file systems
  152. libfs_init();
  153. // List devices
  154. printf("MAIN: device list: ");
  155. vfs_list_devices();
  156. printf("\n");
  157. // Look for a boot configuration file, starting with the default device,
  158. // then iterating through all devices
  159. FILE *f = (void*)0;
  160. // Default device
  161. char **fname = boot_cfg_names;
  162. char *found_cfg;
  163. while(*fname)
  164. {
  165. f = fopen(*fname, "r");
  166. if(f)
  167. {
  168. found_cfg = *fname;
  169. break;
  170. }
  171. fname++;
  172. }
  173. if(!f)
  174. {
  175. // Try other devices
  176. char **dev = vfs_get_device_list();
  177. while(*dev)
  178. {
  179. int dev_len = strlen(*dev);
  180. fname = boot_cfg_names;
  181. while(*fname)
  182. {
  183. int fname_len = strlen(*fname);
  184. char *new_str = (char *)malloc(dev_len + fname_len + 3);
  185. new_str[0] = 0;
  186. strcat(new_str, "(");
  187. strcat(new_str, *dev);
  188. strcat(new_str, ")");
  189. strcat(new_str, *fname);
  190. f = fopen(new_str, "r");
  191. if(f)
  192. {
  193. found_cfg = new_str;
  194. break;
  195. }
  196. free(new_str);
  197. fname++;
  198. }
  199. if(f)
  200. break;
  201. dev++;
  202. }
  203. }
  204. if(!f)
  205. {
  206. printf("MAIN: No bootloader configuration file found\n");
  207. }
  208. else
  209. {
  210. long flen = fsize(f);
  211. printf("MAIN: Found bootloader configuration: %s\n", found_cfg);
  212. char *buf = (char *)malloc(flen+1);
  213. buf[flen] = 0; // null terminate
  214. fread(buf, 1, flen, f);
  215. fclose(f);
  216. cfg_parse(buf);
  217. }
  218. }