1 /* $NetBSD: news.c,v 1.8 2019/05/07 04:35:31 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn and Izumi Tsutsui.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if !defined(__lint)
38 __RCSID("$NetBSD: news.c,v 1.8 2019/05/07 04:35:31 thorpej Exp $");
39 #endif /* !__lint */
40
41 #include <sys/param.h>
42
43 #include <assert.h>
44 #include <err.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "installboot.h"
52
53 static int news_copydisklabel(ib_params *, struct bbinfo_params *, uint8_t *);
54
55 static int news68k_clearboot(ib_params *);
56 static int news68k_setboot(ib_params *);
57 static int newsmips_clearboot(ib_params *);
58 static int newsmips_setboot(ib_params *);
59
60 struct ib_mach ib_mach_news68k = {
61 .name = "news68k",
62 .setboot = news68k_setboot,
63 .clearboot = news68k_clearboot,
64 .editboot = no_editboot,
65 .valid_flags = IB_STAGE2START,
66 };
67
68 struct ib_mach ib_mach_newsmips = {
69 .name = "newsmips",
70 .setboot = newsmips_setboot,
71 .clearboot = newsmips_clearboot,
72 .editboot = no_editboot,
73 .valid_flags = IB_STAGE2START,
74 };
75
76 /*
77 * news68k specific support
78 */
79
80 static struct bbinfo_params news68k_bbparams = {
81 NEWS68K_BBINFO_MAGIC,
82 NEWS_BOOT_BLOCK_OFFSET, /* write all 8K (including disklabel) */
83 NEWS_BOOT_BLOCK_BLOCKSIZE,
84 NEWS_BOOT_BLOCK_MAX_SIZE,
85 0,
86 BBINFO_BIG_ENDIAN,
87 };
88
89 static int
news68k_clearboot(ib_params * params)90 news68k_clearboot(ib_params *params)
91 {
92
93 assert(params != NULL);
94
95 return (shared_bbinfo_clearboot(params, &news68k_bbparams,
96 news_copydisklabel));
97 }
98
99 static int
news68k_setboot(ib_params * params)100 news68k_setboot(ib_params *params)
101 {
102
103 assert(params != NULL);
104
105 return (shared_bbinfo_setboot(params, &news68k_bbparams,
106 news_copydisklabel));
107 }
108
109
110 /*
111 * newsmips specific support
112 */
113
114 static struct bbinfo_params newsmips_bbparams = {
115 NEWSMIPS_BBINFO_MAGIC,
116 NEWS_BOOT_BLOCK_OFFSET, /* write all 8K (including disklabel) */
117 NEWS_BOOT_BLOCK_BLOCKSIZE,
118 NEWS_BOOT_BLOCK_MAX_SIZE,
119 0,
120 BBINFO_BIG_ENDIAN,
121 };
122
123 static int
newsmips_clearboot(ib_params * params)124 newsmips_clearboot(ib_params *params)
125 {
126
127 assert(params != NULL);
128
129 return (shared_bbinfo_clearboot(params, &newsmips_bbparams,
130 news_copydisklabel));
131 }
132
133 static int
newsmips_setboot(ib_params * params)134 newsmips_setboot(ib_params *params)
135 {
136
137 assert(params != NULL);
138
139 return (shared_bbinfo_setboot(params, &newsmips_bbparams,
140 news_copydisklabel));
141 }
142
143
144 /*
145 * news_copydisklabel --
146 * copy disklabel from existing location on disk into bootstrap,
147 * as the primary bootstrap contains the disklabel.
148 */
149 static int
news_copydisklabel(ib_params * params,struct bbinfo_params * bbparams,uint8_t * bb)150 news_copydisklabel(ib_params *params, struct bbinfo_params *bbparams,
151 uint8_t *bb)
152 {
153 uint8_t boot00[NEWS_BOOT_BLOCK_BLOCKSIZE];
154 ssize_t rv;
155
156 assert(params != NULL);
157 assert(params->fsfd != -1);
158 assert(bbparams != NULL);
159 assert(bb != NULL);
160
161 /* Read label sector to copy disklabel from */
162 memset(boot00, 0, sizeof(boot00));
163 rv = pread(params->fsfd, boot00, sizeof(boot00), 0);
164 if (rv == -1) {
165 warn("Reading label sector from `%s'", params->filesystem);
166 return (0);
167 }
168 /* Copy disklabel */
169 memcpy(bb + NEWS_BOOT_BLOCK_LABELOFFSET,
170 boot00 + NEWS_BOOT_BLOCK_LABELOFFSET,
171 sizeof(boot00) - NEWS_BOOT_BLOCK_LABELOFFSET);
172
173 return (1);
174 }
175