8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / boot / common / mboot_extra.c
blob170d638bb5eefd8e1a913b48d7cdcd31fb6c7488
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
26 #include <stdio.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <unistd.h>
30 #include <libintl.h>
31 #include <sys/multiboot.h>
32 #include <sys/sysmacros.h>
34 #include "bblk_einfo.h"
35 #include "boot_utils.h"
36 #include "mboot_extra.h"
39 * Common functions to deal with the fake-multiboot encapsulation of the
40 * bootblock and the location of the extra information area.
43 /* mboot checksum routine. */
44 uint32_t
45 compute_checksum(char *data, uint32_t size)
47 uint32_t *ck_ptr;
48 uint32_t cksum = 0;
49 int i;
51 ck_ptr = (uint32_t *)data;
52 for (i = 0; i < size; i += sizeof (uint32_t))
53 cksum += *ck_ptr++;
55 return (-cksum);
58 /* Given a buffer, look for a multiboot header within it. */
59 int
60 find_multiboot(char *buffer, uint32_t buf_size, uint32_t *mboot_off)
62 multiboot_header_t *mboot;
63 uint32_t *iter;
64 uint32_t cksum;
65 uint32_t boundary;
66 int i = 0;
68 iter = (uint32_t *)buffer;
69 *mboot_off = 0;
70 /* multiboot header has to be within the first 32K. */
71 boundary = MBOOT_SCAN_SIZE;
72 if (boundary > buf_size)
73 boundary = buf_size;
75 boundary = boundary - sizeof (multiboot_header_t);
77 for (i = 0; i < boundary; i += 4, iter++) {
79 mboot = (multiboot_header_t *)iter;
80 if (mboot->magic != MB_HEADER_MAGIC)
81 continue;
83 /* Found magic signature -- check checksum. */
84 cksum = -(mboot->flags + mboot->magic);
85 if (mboot->checksum != cksum) {
86 BOOT_DEBUG("multiboot magic found at %p, but checksum "
87 "mismatches (is %x, should be %x)\n", mboot,
88 mboot->checksum, cksum);
89 continue;
90 } else {
91 if (!(mboot->flags & BB_MBOOT_AOUT_FLAG)) {
92 BOOT_DEBUG("multiboot structure found, but no "
93 "AOUT kludge specified, skipping.\n");
94 continue;
95 } else {
96 /* proper multiboot structure found. */
97 *mboot_off = i;
98 return (BC_SUCCESS);
103 return (BC_ERROR);
107 * Given a pointer to the extra information area (a sequence of bb_header_ext_t
108 * + payload chunks), find the extended information structure.
110 bblk_einfo_t *
111 find_einfo(char *extra, uint32_t size)
113 bb_header_ext_t *ext_header;
114 bblk_einfo_t *einfo;
115 uint32_t cksum;
117 assert(extra != NULL);
119 ext_header = (bb_header_ext_t *)extra;
120 if (ext_header->size > size) {
121 BOOT_DEBUG("Unable to find extended versioning information, "
122 "data size too big\n");
123 return (NULL);
126 cksum = compute_checksum(extra + sizeof (bb_header_ext_t),
127 ext_header->size);
128 BOOT_DEBUG("Extended information header checksum is %x\n", cksum);
130 if (cksum != ext_header->checksum) {
131 BOOT_DEBUG("Unable to find extended versioning information, "
132 "data looks corrupted\n");
133 return (NULL);
137 * Currently we only have one extra header so it must be encapsulating
138 * the extended information structure.
140 einfo = (bblk_einfo_t *)(extra + sizeof (bb_header_ext_t));
141 if (memcmp(einfo->magic, EINFO_MAGIC, EINFO_MAGIC_SIZE) != 0) {
142 BOOT_DEBUG("Unable to read stage2 extended versioning "
143 "information, wrong magic identifier\n");
144 BOOT_DEBUG("Found %s, expected %s\n", einfo->magic,
145 EINFO_MAGIC);
146 return (NULL);
149 return (einfo);
153 * Given a pointer to the extra area, add the extended information structure
154 * encapsulated by a bb_header_ext_t structure.
156 void
157 add_einfo(char *extra, char *updt_str, bblk_hs_t *hs, uint32_t avail_space)
159 bb_header_ext_t *ext_hdr;
160 uint32_t used_space;
161 unsigned char *dest;
162 int ret;
164 assert(extra != NULL);
166 if (updt_str == NULL) {
167 BOOT_DEBUG("WARNING: no update string passed to "
168 "add_stage2_einfo()\n");
169 return;
172 /* Reserve space for the extra header. */
173 ext_hdr = (bb_header_ext_t *)extra;
174 dest = (unsigned char *)extra + sizeof (*ext_hdr);
175 /* Place the extended information structure. */
176 ret = prepare_and_write_einfo(dest, updt_str, hs, avail_space,
177 &used_space);
178 if (ret != 0) {
179 (void) fprintf(stderr, gettext("Unable to write the extended "
180 "versioning information\n"));
181 return;
184 /* Fill the extended information associated header. */
185 ext_hdr->size = P2ROUNDUP(used_space, 8);
186 ext_hdr->checksum = compute_checksum((char *)dest, ext_hdr->size);