aboutsummaryrefslogtreecommitdiffstats
path: root/com32/modules/sdi.c
blob: e57ad4a3239a6d429bfc107b9fba40f1637156c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *   Boston MA 02110-1301, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * sdi.c
 *
 * Loader for the Microsoft System Deployment Image (SDI) format.
 * Based on a historical patch by Remi Lefevre.
 */

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <minmax.h>
#include <sys/stat.h>
#include <console.h>
#include <dprintf.h>

#include <syslinux/loadfile.h>
#include <syslinux/movebits.h>
#include <syslinux/bootrm.h>

typedef uint8_t guid_t[16];

struct SDIHeader {
    uint32_t Signature;
    char Version[4];
    uint64_t MDBtype;
    uint64_t BootCodeOffset;
    uint64_t BootCodeSize;
    uint64_t VendorID;
    uint64_t DeviceID;
    guid_t DeviceModel;
    uint64_t DeviceRole;
    uint64_t Reserved1;
    guid_t RuntimeGUID;
    uint64_t RuntimeOEMrev;
    uint64_t Reserved2;
    uint64_t PageAlignment;	/* BLOB alignment value in pages */
    uint64_t Reserved3[48];
    uint64_t Checksum;
};

#define SDI_LOAD_ADDR	(16 << 20)	/* 16 MB */
#define SDI_SIGNATURE	('$' + ('S' << 8) + ('D' << 16) + ('I' << 24))

static inline void error(const char *msg)
{
    fputs(msg, stderr);
}

static int boot_sdi(void *ptr, size_t len)
{
    const struct SDIHeader *hdr = ptr;
    struct syslinux_memmap *mmap = NULL, *amap = NULL;
    struct syslinux_rm_regs regs;
    struct syslinux_movelist *ml = NULL;

    /* **** Basic sanity checking **** */
    if (hdr->Signature != SDI_SIGNATURE) {
	fputs("No $SDI signature in file\n", stdout);
	goto bail;
    }
    if (memcmp(hdr->Version, "0001", 4)) {
	int i;
	fputs("Warning: unknown SDI version: ", stdout);
	for (i = 0; i < 4; i++)
	    putchar(hdr->Version[i]);
	putchar('\n');
	/* Then try anyway... */
    }

    /* **** Setup **** */
    mmap = syslinux_memory_map();
    amap = syslinux_dup_memmap(mmap);
    if (!mmap || !amap)
	goto bail;

    /* **** Map the BOOT BLOB to 0x7c00 **** */
    if (!hdr->BootCodeOffset) {
	fputs("No BOOT BLOB in image\n", stdout);
	goto bail;
    }
    if (!hdr->BootCodeSize) {
	fputs("BOOT BLOB is empty\n", stdout);
	goto bail;
    }
    if (len < hdr->BootCodeOffset + hdr->BootCodeSize) {
	fputs("BOOT BLOB extends beyond file\n", stdout);
	goto bail;
    }

    if (syslinux_memmap_type(amap, 0x7c00, hdr->BootCodeSize) != SMT_FREE) {
	fputs("BOOT BLOB too large for memory\n", stdout);
	goto bail;
    }
    if (syslinux_add_memmap(&amap, 0x7c00, hdr->BootCodeSize, SMT_ALLOC))
	goto bail;
    if (syslinux_add_movelist(&ml, 0x7c00, (addr_t) ptr + hdr->BootCodeOffset,
			      hdr->BootCodeSize))
	goto bail;

    /* **** Map the entire image to SDI_LOAD_ADDR **** */
    if (syslinux_memmap_type(amap, SDI_LOAD_ADDR, len) != SMT_FREE) {
	fputs("Image too large for memory\n", stdout);
	goto bail;
    }
    if (syslinux_add_memmap(&amap, SDI_LOAD_ADDR, len, SMT_ALLOC))
	goto bail;
    if (syslinux_add_movelist(&ml, SDI_LOAD_ADDR, (addr_t) ptr, len))
	goto bail;

    /* **** Set up registers **** */
    memset(&regs, 0, sizeof regs);
    regs.ip = 0x7c00;
    regs.esp.l = 0x7c00;
    regs.edx.l = SDI_LOAD_ADDR | 0x41;

    fputs("Booting...\n", stdout);
    syslinux_shuffle_boot_rm(ml, mmap, 0, &regs);

bail:
    syslinux_free_memmap(amap);
    syslinux_free_memmap(mmap);
    syslinux_free_movelist(ml);
    return -1;
}

/*
 * Check that the sum of all bytes from first 512 bytes (SDI header)
 * is 0 modulo 256.
 */
int has_valid_header(unsigned char *header)
{
    unsigned char checksum;
    unsigned int i;

    checksum = 0;
    for (i = 0; i < sizeof(struct SDIHeader); i++)
	checksum += header[i];
    return (!checksum);
}

int main(int argc, char *argv[])
{
    void *data;
    size_t data_len;

    if (argc != 2) {
	error("Usage: sdi.c32 sdi_file\n");
	return 1;
    }

    fputs("Loading ", stdout);
    fputs(argv[1], stdout);
    fputs("... ", stdout);
    if (zloadfile(argv[1], &data, &data_len)) {
	error("failed!\n");
	return 1;
    }
    fputs("ok\n", stdout);

    if (!has_valid_header(data)) {
	error("SDI header is corrupted\n");
	return 1;
    }

    boot_sdi(data, data_len);
    error("Invalid SDI file or insufficient memory\n");
    return 1;
}