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