1*78c54f94Skn /* $OpenBSD: sparc64_installboot.c,v 1.11 2022/09/05 11:12:20 kn Exp $ */
2b4544c7cSjsing
3b4544c7cSjsing /*
4b4544c7cSjsing * Copyright (c) 2012, 2013 Joel Sing <jsing@openbsd.org>
5b4544c7cSjsing *
6b4544c7cSjsing * Permission to use, copy, modify, and distribute this software for any
7b4544c7cSjsing * purpose with or without fee is hereby granted, provided that the above
8b4544c7cSjsing * copyright notice and this permission notice appear in all copies.
9b4544c7cSjsing *
10b4544c7cSjsing * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11b4544c7cSjsing * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12b4544c7cSjsing * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13b4544c7cSjsing * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14b4544c7cSjsing * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15b4544c7cSjsing * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16b4544c7cSjsing * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17b4544c7cSjsing */
18b4544c7cSjsing
199b2ea772Sderaadt #include <sys/param.h> /* DEV_BSIZE */
20b4544c7cSjsing #include <sys/stat.h>
21b4544c7cSjsing
22b4544c7cSjsing #include <err.h>
23b4544c7cSjsing #include <fcntl.h>
24b4544c7cSjsing #include <stdio.h>
25b4544c7cSjsing #include <stdlib.h>
26b4544c7cSjsing #include <string.h>
27b4544c7cSjsing #include <unistd.h>
28b4544c7cSjsing
29b4544c7cSjsing #include <ufs/ffs/fs.h>
30b4544c7cSjsing
31b4544c7cSjsing #include "installboot.h"
32b4544c7cSjsing
33b4544c7cSjsing char *bootldr;
34b4544c7cSjsing
35b4544c7cSjsing char *blkstore;
36b4544c7cSjsing char *ldrstore;
37b4544c7cSjsing size_t blksize;
38b4544c7cSjsing size_t ldrsize;
39b4544c7cSjsing
40b4544c7cSjsing void
md_init(void)41b4544c7cSjsing md_init(void)
42b4544c7cSjsing {
43b4544c7cSjsing stages = 2;
44b4544c7cSjsing stage1 = "/usr/mdec/bootblk";
45b4544c7cSjsing stage2 = "/usr/mdec/ofwboot";
46b4544c7cSjsing
47b4544c7cSjsing bootldr = "/ofwboot";
48b4544c7cSjsing }
49b4544c7cSjsing
50b4544c7cSjsing void
md_loadboot(void)51b4544c7cSjsing md_loadboot(void)
52b4544c7cSjsing {
53b4544c7cSjsing struct stat sb;
54b4544c7cSjsing size_t blocks;
55b4544c7cSjsing int fd;
56b4544c7cSjsing
57b4544c7cSjsing /* Load first-stage boot block. */
58df69c215Sderaadt if ((fd = open(stage1, O_RDONLY)) == -1)
59b4544c7cSjsing err(1, "open");
60b4544c7cSjsing if (fstat(fd, &sb) == -1)
61b4544c7cSjsing err(1, "fstat");
62b4544c7cSjsing blocks = howmany((size_t)sb.st_size, DEV_BSIZE);
63b4544c7cSjsing blksize = blocks * DEV_BSIZE;
64b4544c7cSjsing if (verbose)
65b4544c7cSjsing fprintf(stderr, "boot block is %zu bytes "
66b4544c7cSjsing "(%zu blocks @ %u bytes = %zu bytes)\n",
67b4544c7cSjsing (ssize_t)sb.st_size, blocks, DEV_BSIZE, blksize);
68b4544c7cSjsing if (blksize > SBSIZE - DEV_BSIZE)
69b4544c7cSjsing errx(1, "boot blocks too big (%zu > %d)",
70b4544c7cSjsing blksize, SBSIZE - DEV_BSIZE);
71210008b3Sjsing blkstore = calloc(1, blksize);
72b4544c7cSjsing if (blkstore == NULL)
73210008b3Sjsing err(1, "calloc");
74b4544c7cSjsing if (read(fd, blkstore, sb.st_size) != (ssize_t)sb.st_size)
75b4544c7cSjsing err(1, "read");
76b4544c7cSjsing close(fd);
77b4544c7cSjsing
78b4544c7cSjsing /* Load second-stage boot loader. */
79df69c215Sderaadt if ((fd = open(stage2, O_RDONLY)) == -1)
80b4544c7cSjsing err(1, "open");
81b4544c7cSjsing if (fstat(fd, &sb) == -1)
82b4544c7cSjsing err(1, "stat");
83b4544c7cSjsing ldrsize = sb.st_size;
84b4544c7cSjsing ldrstore = malloc(ldrsize);
85b4544c7cSjsing if (ldrstore == NULL)
86b4544c7cSjsing err(1, "malloc");
87b4544c7cSjsing if (read(fd, ldrstore, ldrsize) != (ssize_t)sb.st_size)
88b4544c7cSjsing err(1, "read");
89b4544c7cSjsing close(fd);
90b4544c7cSjsing }
91b4544c7cSjsing
92b4544c7cSjsing void
md_prepareboot(int devfd,char * dev)93c3e1bf61Skettenis md_prepareboot(int devfd, char *dev)
94c3e1bf61Skettenis {
95c3e1bf61Skettenis }
96c3e1bf61Skettenis
97c3e1bf61Skettenis void
md_installboot(int devfd,char * dev)98b4544c7cSjsing md_installboot(int devfd, char *dev)
99b4544c7cSjsing {
100*78c54f94Skn static int prefixed = 0;
101*78c54f94Skn
10268507edaSjsing /* XXX - is this necessary? */
10368507edaSjsing sync();
104b4544c7cSjsing
105*78c54f94Skn /*
106*78c54f94Skn * sr_install_bootblk() calls md_installboot() for every softraid chunk
107*78c54f94Skn * but the path must be prefixed only once.
108*78c54f94Skn */
109*78c54f94Skn if (!prefixed++)
110b4544c7cSjsing bootldr = fileprefix(root, bootldr);
111597864b7Skrw if (bootldr == NULL)
112597864b7Skrw exit(1);
1136ebdf465Skn if (verbose)
1146ebdf465Skn fprintf(stderr, "%s %s to %s\n",
1156ebdf465Skn (nowrite ? "would copy" : "copying"), stage2, bootldr);
116b4544c7cSjsing if (!nowrite)
117597864b7Skrw if (filecopy(stage2, bootldr) == -1)
118597864b7Skrw exit(1);
119b4544c7cSjsing
120b4544c7cSjsing /* Write bootblock into the superblock. */
121b4544c7cSjsing if (verbose)
122b4544c7cSjsing fprintf(stderr, "%s boot block to disk %s\n",
123b4544c7cSjsing (nowrite ? "would write" : "writing"), dev);
124b4544c7cSjsing if (nowrite)
125b4544c7cSjsing return;
126c462e017Skrw if (pwrite(devfd, blkstore, blksize, DEV_BSIZE) != (ssize_t)blksize)
127c462e017Skrw err(1, "pwrite");
128b4544c7cSjsing }
129