2 * Linux Boot Option ROM for fw_cfg DMA
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Copyright (c) 2015-2016 Red Hat Inc.
19 * Marc MarĂ <marc.mari.barcelo@gmail.com>
20 * Richard W.M. Jones <rjones@redhat.com>
28 " .byte 3\n" /* desired size in 512 units; signrom.py adds padding */
29 " .byte 0xcb\n" /* far return without prefix */
36 " .byte (_pnph_len / 16)\n"
41 " .short _manufacturer\n"
49 " .equ _pnph_len, . - _pnph\n"
53 " .asciz \"Linux loader DMA\"\n"
61 #include "../../include/hw/nvram/fw_cfg_keys.h"
63 /* QEMU_CFG_DMA_CONTROL bits */
64 #define BIOS_CFG_DMA_CTL_ERROR 0x01
65 #define BIOS_CFG_DMA_CTL_READ 0x02
66 #define BIOS_CFG_DMA_CTL_SKIP 0x04
67 #define BIOS_CFG_DMA_CTL_SELECT 0x08
69 #define BIOS_CFG_DMA_ADDR_HIGH 0x514
70 #define BIOS_CFG_DMA_ADDR_LOW 0x518
72 #define uint64_t unsigned long long
73 #define uint32_t unsigned int
74 #define uint16_t unsigned short
76 #define barrier() asm("" : : : "memory")
78 typedef struct FWCfgDmaAccess
{
82 } __attribute__((packed
)) FWCfgDmaAccess
;
84 static inline void outl(uint32_t value
, uint16_t port
)
86 asm("outl %0, %w1" : : "a"(value
), "Nd"(port
));
89 static inline void set_es(void *addr
)
91 uint32_t seg
= (uint32_t)addr
>> 4;
92 asm("movl %0, %%es" : : "r"(seg
));
98 #define ADDR32 "addr32 "
101 static inline uint16_t readw_es(uint16_t offset
)
104 asm(ADDR32
"movw %%es:(%1), %0" : "=r"(val
) : "r"((uint32_t)offset
));
109 static inline uint32_t readl_es(uint16_t offset
)
112 asm(ADDR32
"movl %%es:(%1), %0" : "=r"(val
) : "r"((uint32_t)offset
));
117 static inline void writel_es(uint16_t offset
, uint32_t val
)
120 asm(ADDR32
"movl %0, %%es:(%1)" : : "r"(val
), "r"((uint32_t)offset
));
123 static inline uint32_t bswap32(uint32_t x
)
125 asm("bswapl %0" : "=r" (x
) : "0" (x
));
129 static inline uint64_t bswap64(uint64_t x
)
131 asm("bswapl %%eax; bswapl %%edx; xchg %%eax, %%edx" : "=A" (x
) : "0" (x
));
135 static inline uint64_t cpu_to_be64(uint64_t x
)
140 static inline uint32_t cpu_to_be32(uint32_t x
)
145 static inline uint32_t be32_to_cpu(uint32_t x
)
150 /* clang is happy to inline this function, and bloats the
153 static __attribute__((__noinline__
))
154 void bios_cfg_read_entry(void *buf
, uint16_t entry
, uint32_t len
)
156 FWCfgDmaAccess access
;
157 uint32_t control
= (entry
<< 16) | BIOS_CFG_DMA_CTL_SELECT
158 | BIOS_CFG_DMA_CTL_READ
;
160 access
.address
= cpu_to_be64((uint64_t)(uint32_t)buf
);
161 access
.length
= cpu_to_be32(len
);
162 access
.control
= cpu_to_be32(control
);
166 outl(cpu_to_be32((uint32_t)&access
), BIOS_CFG_DMA_ADDR_LOW
);
168 while (be32_to_cpu(access
.control
) & ~BIOS_CFG_DMA_CTL_ERROR
) {
173 /* Return top of memory using BIOS function E801. */
174 static uint32_t get_e801_addr(void)
176 uint16_t ax
, bx
, cx
, dx
;
180 : "=a"(ax
), "=b"(bx
), "=c"(cx
), "=d"(dx
)
181 : "a"(0xe801), "b"(0), "c"(0), "d"(0));
183 /* Not SeaBIOS, but in theory a BIOS could return CX=DX=0 in which
184 * case we need to use the result from AX & BX instead.
186 if (cx
== 0 && dx
== 0) {
192 /* DX = extended memory above 16M, in 64K units.
193 * Convert it to bytes and return.
195 ret
= ((uint32_t)dx
+ 256 /* 16M in 64K units */) << 16;
197 /* This is a fallback path for machines with <= 16MB of RAM,
198 * which probably would never be the case, but deal with it
201 * CX = extended memory between 1M and 16M, in kilobytes
202 * Convert it to bytes and return.
204 ret
= ((uint32_t)cx
+ 1024 /* 1M in K */) << 10;
210 /* Force the asm name without leading underscore, even on Win32. */
211 extern void load_kernel(void) asm("load_kernel");
213 void load_kernel(void)
220 uint32_t initrd_size
;
221 uint32_t kernel_size
;
222 uint32_t cmdline_size
;
223 uint32_t initrd_end_page
, max_allowed_page
;
224 uint32_t segment_addr
, stack_addr
;
226 bios_cfg_read_entry(&setup_addr
, FW_CFG_SETUP_ADDR
, 4);
227 bios_cfg_read_entry(&setup_size
, FW_CFG_SETUP_SIZE
, 4);
228 bios_cfg_read_entry(setup_addr
, FW_CFG_SETUP_DATA
, setup_size
);
232 /* For protocol < 0x203 we don't have initrd_max ... */
233 if (readw_es(0x206) < 0x203) {
234 /* ... so we assume initrd_max = 0x37ffffff. */
235 writel_es(0x22c, 0x37ffffff);
238 bios_cfg_read_entry(&initrd_addr
, FW_CFG_INITRD_ADDR
, 4);
239 bios_cfg_read_entry(&initrd_size
, FW_CFG_INITRD_SIZE
, 4);
241 initrd_end_page
= ((uint32_t)(initrd_addr
+ initrd_size
) & -4096);
242 max_allowed_page
= (readl_es(0x22c) & -4096);
244 if (initrd_end_page
!= 0 && max_allowed_page
!= 0 &&
245 initrd_end_page
!= max_allowed_page
) {
246 /* Initrd at the end of memory. Compute better initrd address
249 initrd_addr
= (void *)((get_e801_addr() - initrd_size
) & -4096);
250 writel_es(0x218, (uint32_t)initrd_addr
);
254 bios_cfg_read_entry(initrd_addr
, FW_CFG_INITRD_DATA
, initrd_size
);
256 bios_cfg_read_entry(&kernel_addr
, FW_CFG_KERNEL_ADDR
, 4);
257 bios_cfg_read_entry(&kernel_size
, FW_CFG_KERNEL_SIZE
, 4);
258 bios_cfg_read_entry(kernel_addr
, FW_CFG_KERNEL_DATA
, kernel_size
);
260 bios_cfg_read_entry(&cmdline_addr
, FW_CFG_CMDLINE_ADDR
, 4);
261 bios_cfg_read_entry(&cmdline_size
, FW_CFG_CMDLINE_SIZE
, 4);
262 bios_cfg_read_entry(cmdline_addr
, FW_CFG_CMDLINE_DATA
, cmdline_size
);
265 segment_addr
= ((uint32_t)setup_addr
>> 4);
266 stack_addr
= (uint32_t)(cmdline_addr
- setup_addr
- 16);
268 /* As we are changing critical registers, we cannot leave freedom to the
271 asm("movw %%ax, %%ds\n"
276 "movl %%ebx, %%esp\n"
278 "pushw %%ax\n" /* CS */
279 "pushw $0\n" /* IP */
280 /* Clear registers and jump to Linux */
287 : : "a"(segment_addr
), "b"(stack_addr
));