boot.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* $OpenBSD: boot.c,v 1.4 2014/02/23 20:01:04 miod Exp $ */
  2. /* $NetBSD: boot.c,v 1.3 2013/03/05 15:34:53 tsutsui Exp $ */
  3. /*
  4. * Copyright (c) 1992 OMRON Corporation.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * OMRON Corporation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. *
  37. * @(#)boot.c 8.1 (Berkeley) 6/10/93
  38. */
  39. /*
  40. * Copyright (c) 1992, 1993
  41. * The Regents of the University of California. All rights reserved.
  42. *
  43. * This code is derived from software contributed to Berkeley by
  44. * OMRON Corporation.
  45. *
  46. * Redistribution and use in source and binary forms, with or without
  47. * modification, are permitted provided that the following conditions
  48. * are met:
  49. * 1. Redistributions of source code must retain the above copyright
  50. * notice, this list of conditions and the following disclaimer.
  51. * 2. Redistributions in binary form must reproduce the above copyright
  52. * notice, this list of conditions and the following disclaimer in the
  53. * documentation and/or other materials provided with the distribution.
  54. * 3. Neither the name of the University nor the names of its contributors
  55. * may be used to endorse or promote products derived from this software
  56. * without specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  59. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  62. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  63. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  64. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  65. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  67. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  68. * SUCH DAMAGE.
  69. *
  70. * @(#)boot.c 8.1 (Berkeley) 6/10/93
  71. */
  72. /*
  73. * boot.c -- boot program
  74. * by A.Fujita, MAR-01-1992
  75. */
  76. #include <sys/param.h>
  77. #include <sys/reboot.h>
  78. #include <sys/stat.h>
  79. #define _KERNEL
  80. #include <sys/fcntl.h>
  81. #undef _KERNEL
  82. #include <lib/libkern/libkern.h>
  83. #include <luna88k/stand/boot/samachdep.h>
  84. #include <luna88k/stand/boot/status.h>
  85. #include <lib/libsa/loadfile.h>
  86. int howto;
  87. int loadrandom(const char *, char *, size_t);
  88. #if 0
  89. static int get_boot_device(const char *, int *, int *, int *);
  90. #endif
  91. void (*cpu_boot)(uint32_t, uint32_t);
  92. uint32_t cpu_bootarg1;
  93. uint32_t cpu_bootarg2;
  94. char rnddata[BOOTRANDOM_MAX];
  95. #if 0
  96. int
  97. get_boot_device(const char *s, int *devp, int *unitp, int *partp)
  98. {
  99. const char *p = s;
  100. int unit = 0, part = 0;
  101. while (*p != '(') {
  102. if (*p == '\0')
  103. goto error;
  104. p++;
  105. }
  106. p++;
  107. for (; *p != ',' && *p != ')'; p++) {
  108. if (*p == '\0')
  109. goto error;
  110. if (*p >= '0' && *p <= '9')
  111. unit = (unit * 10) + (*p - '0');
  112. }
  113. if (*p == ',')
  114. p++;
  115. for (; *p != ')'; p++) {
  116. if (*p == '\0')
  117. goto error;
  118. if (*p >= '0' && *p <= '9')
  119. part = (part * 10) + (*p - '0');
  120. }
  121. *devp = 0; /* XXX not yet */
  122. *unitp = unit; /* XXX should pass SCSI ID, not logical unit number */
  123. *partp = part;
  124. return 0;
  125. error:
  126. return -1;
  127. }
  128. #endif
  129. int
  130. boot(int argc, char *argv[])
  131. {
  132. char *line, *lparen, *rparen;
  133. char rndpath[MAXPATHLEN];
  134. static int rnd_loaded = 0;
  135. if (argc < 2)
  136. line = default_file;
  137. else
  138. line = argv[1];
  139. printf("Booting %s\n", line);
  140. /*
  141. * Try and load randomness from the boot device.
  142. */
  143. if (rnd_loaded == 0) {
  144. lparen = strchr(line, '(');
  145. if (lparen != NULL)
  146. rparen = strchr(line, ')');
  147. else
  148. rparen = NULL;
  149. if (rparen != NULL &&
  150. rparen + 1 - line < sizeof rndpath) {
  151. rparen++;
  152. memcpy(rndpath, line, rparen - line);
  153. rndpath[rparen - line] = '\0';
  154. strlcat(rndpath, BOOTRANDOM, sizeof rndpath);
  155. } else
  156. strlcpy(rndpath, BOOTRANDOM, sizeof rndpath);
  157. rnd_loaded = loadrandom(rndpath, rnddata, sizeof(rnddata));
  158. }
  159. return bootunix(line);
  160. }
  161. int
  162. bootunix(char *line)
  163. {
  164. int io;
  165. #if 0
  166. int dev, unit, part;
  167. #endif
  168. u_long marks[MARK_MAX];
  169. #if 0
  170. if (get_boot_device(line, &dev, &unit, &part) != 0) {
  171. printf("Bad file name %s\n", line);
  172. return ST_ERROR;
  173. }
  174. #endif
  175. /* Note marks[MARK_START] is passed as an load address offset */
  176. memset(marks, 0, sizeof(marks));
  177. io = loadfile(line, marks, LOAD_KERNEL);
  178. if (io >= 0) {
  179. #ifdef DEBUG
  180. printf("entry = 0x%lx\n", marks[MARK_ENTRY]);
  181. printf("ssym = 0x%lx\n", marks[MARK_SYM]);
  182. printf("esym = 0x%lx\n", marks[MARK_END]);
  183. #endif
  184. cpu_bootarg1 = BOOT_MAGIC;
  185. cpu_bootarg2 = marks[MARK_END];
  186. cpu_boot = (void (*)(uint32_t, uint32_t))marks[MARK_ENTRY];
  187. (*cpu_boot)(cpu_bootarg1, cpu_bootarg2);
  188. }
  189. printf("Booting kernel failed. (%s)\n", strerror(errno));
  190. return ST_ERROR;
  191. }
  192. int
  193. loadrandom(const char *name, char *buf, size_t buflen)
  194. {
  195. struct stat sb;
  196. int fd;
  197. int rc = 0;
  198. fd = open(name, O_RDONLY);
  199. if (fd == -1) {
  200. if (errno != EPERM)
  201. printf("cannot open %s: %s\n", name, strerror(errno));
  202. return 0;
  203. }
  204. if (fstat(fd, &sb) == -1 || sb.st_uid != 0 || !S_ISREG(sb.st_mode) ||
  205. (sb.st_mode & (S_IWOTH|S_IROTH)))
  206. goto fail;
  207. (void) read(fd, buf, buflen);
  208. rc = 1;
  209. fail:
  210. close(fd);
  211. return rc;
  212. }