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