1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2012 Intel Corporation; author: H. Peter Anvin
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * ----------------------------------------------------------------------- */
28
29 /*
30 * syslinux/linux.h
31 *
32 * Definitions used to boot a Linux kernel
33 */
34
35 #ifndef _SYSLINUX_LINUX_H
36 #define _SYSLINUX_LINUX_H
37
38 #include <stddef.h>
39 #include <stdint.h>
40 #include <klibc/compiler.h>
41
42 /* A chunk of an initramfs. These are kept as a doubly-linked
43 circular list with headnode; the headnode is distinguished by
44 having len == 0. The data pointer can be NULL if data_len is zero;
45 if data_len < len then the balance of the region is zeroed. */
46
47 struct initramfs {
48 struct initramfs *prev, *next;
49 size_t len;
50 size_t align;
51 const void *data;
52 size_t data_len;
53 };
54 #define INITRAMFS_MAX_ALIGN 4096
55
56 struct setup_data_header {
57 uint64_t next;
58 uint32_t type;
59 uint32_t len;
60 } __packed;
61
62 struct setup_data {
63 struct setup_data *prev, *next;
64 const void *data;
65 struct setup_data_header hdr;
66 };
67
68 #define SETUP_NONE 0
69 #define SETUP_E820_EXT 1
70 #define SETUP_DTB 2
71
72 #define XLF_KERNEL_64 (1 << 0)
73 #define XLF_CAN_BE_LOADED_ABOVE_4G (1 << 1)
74 #define XLF_EFI_HANDOVER_32 (1 << 2)
75 #define XLF_EFI_HANDOVER_64 (1 << 3)
76
77 struct linux_header {
78 uint8_t boot_sector_1[0x0020];
79 uint16_t old_cmd_line_magic;
80 uint16_t old_cmd_line_offset;
81 uint8_t boot_sector_2[0x01f1 - 0x0024];
82 uint8_t setup_sects;
83 uint16_t root_flags;
84 uint32_t syssize;
85 uint16_t ram_size;
86 uint16_t vid_mode;
87 uint16_t root_dev;
88 uint16_t boot_flag;
89 uint16_t jump;
90 uint32_t header;
91 uint16_t version;
92 uint32_t realmode_swtch;
93 uint16_t start_sys;
94 uint16_t kernel_version;
95 uint8_t type_of_loader;
96 uint8_t loadflags;
97 uint16_t setup_move_size;
98 uint32_t code32_start;
99 uint32_t ramdisk_image;
100 uint32_t ramdisk_size;
101 uint32_t bootsect_kludge;
102 uint16_t heap_end_ptr;
103 uint16_t pad1;
104 uint32_t cmd_line_ptr;
105 uint32_t initrd_addr_max;
106 uint32_t kernel_alignment;
107 uint8_t relocatable_kernel;
108 uint8_t min_alignment;
109 uint16_t xloadflags;
110 uint32_t cmdline_max_len;
111 uint32_t hardware_subarch;
112 uint64_t hardware_subarch_data;
113 uint32_t payload_offset;
114 uint32_t payload_length;
115 uint64_t setup_data;
116 uint64_t pref_address;
117 uint32_t init_size;
118 uint32_t handover_offset;
119 } __packed;
120
121 struct screen_info {
122 uint8_t orig_x; /* 0x00 */
123 uint8_t orig_y; /* 0x01 */
124 uint16_t ext_mem_k; /* 0x02 */
125 uint16_t orig_video_page; /* 0x04 */
126 uint8_t orig_video_mode; /* 0x06 */
127 uint8_t orig_video_cols; /* 0x07 */
128 uint8_t flags; /* 0x08 */
129 uint8_t unused2; /* 0x09 */
130 uint16_t orig_video_ega_bx;/* 0x0a */
131 uint16_t unused3; /* 0x0c */
132 uint8_t orig_video_lines; /* 0x0e */
133 uint8_t orig_video_isVGA; /* 0x0f */
134 uint16_t orig_video_points;/* 0x10 */
135
136 /* VESA graphic mode -- linear frame buffer */
137 uint16_t lfb_width; /* 0x12 */
138 uint16_t lfb_height; /* 0x14 */
139 uint16_t lfb_depth; /* 0x16 */
140 uint32_t lfb_base; /* 0x18 */
141 uint32_t lfb_size; /* 0x1c */
142 uint16_t cl_magic, cl_offset; /* 0x20 */
143 uint16_t lfb_linelength; /* 0x24 */
144 uint8_t red_size; /* 0x26 */
145 uint8_t red_pos; /* 0x27 */
146 uint8_t green_size; /* 0x28 */
147 uint8_t green_pos; /* 0x29 */
148 uint8_t blue_size; /* 0x2a */
149 uint8_t blue_pos; /* 0x2b */
150 uint8_t rsvd_size; /* 0x2c */
151 uint8_t rsvd_pos; /* 0x2d */
152 uint16_t vesapm_seg; /* 0x2e */
153 uint16_t vesapm_off; /* 0x30 */
154 uint16_t pages; /* 0x32 */
155 uint16_t vesa_attributes; /* 0x34 */
156 uint32_t capabilities; /* 0x36 */
157 uint8_t _reserved[6]; /* 0x3a */
158 } __packed;
159
160 int syslinux_boot_linux(void *kernel_buf, size_t kernel_size,
161 struct initramfs *initramfs,
162 struct setup_data *setup_data,
163 char *cmdline);
164
165 /* Initramfs manipulation functions */
166
167 struct initramfs *initramfs_init(void);
168 int initramfs_add_data(struct initramfs *ihead, const void *data,
169 size_t data_len, size_t len, size_t align);
170 int initramfs_mknod(struct initramfs *ihead, const char *filename,
171 int do_mkdir,
172 uint16_t mode, size_t len, uint32_t major, uint32_t minor);
173 int initramfs_add_file(struct initramfs *ihead, const void *data,
174 size_t data_len, size_t len,
175 const char *filename, int do_mkdir, uint32_t mode);
176 int initramfs_load_file(struct initramfs *ihead, const char *src_filename,
177 const char *dst_filename, int do_mkdir, uint32_t mode);
178 int initramfs_add_trailer(struct initramfs *ihead);
179 int initramfs_load_archive(struct initramfs *ihead, const char *filename);
180
181 /* Get the combined size of the initramfs */
initramfs_size(struct initramfs * initramfs)182 static inline uint32_t initramfs_size(struct initramfs *initramfs)
183 {
184 struct initramfs *ip;
185 uint32_t size = 0;
186
187 if (!initramfs)
188 return 0;
189
190 for (ip = initramfs->next; ip->len; ip = ip->next) {
191 size = (size + ip->align - 1) & ~(ip->align - 1); /* Alignment */
192 size += ip->len;
193 }
194
195 return size;
196 }
197
198 /* Setup data manipulation functions */
199
200 struct setup_data *setup_data_init(void);
201 int setup_data_add(struct setup_data *head, uint32_t type,
202 const void *data, size_t data_len);
203 int setup_data_load(struct setup_data *head, uint32_t type,
204 const char *filename);
205
206 #endif /* _SYSLINUX_LINUX_H */
207