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"
62 * The includes of C headers must be after the asm block to avoid compiler
67 #include "optrom_fw_cfg.h"
69 static inline void set_es(void *addr)
71 uint32_t seg = (uint32_t)addr >> 4;
72 asm("movl %0, %%es" : : "r"(seg));
75 static inline uint16_t readw_es(uint16_t offset)
78 asm(ADDR32 "movw %%es:(%1), %0" : "=r"(val) : "r"((uint32_t)offset));
83 static inline uint32_t readl_es(uint16_t offset)
86 asm(ADDR32 "movl %%es:(%1), %0" : "=r"(val) : "r"((uint32_t)offset));
91 static inline void writel_es(uint16_t offset, uint32_t val)
94 asm(ADDR32 "movl %0, %%es:(%1)" : : "r"(val), "r"((uint32_t)offset));
97 /* Return top of memory using BIOS function E801. */
98 static uint32_t get_e801_addr(void)
100 uint16_t ax, bx, cx, dx;
104 : "=a"(ax), "=b"(bx), "=c"(cx), "=d"(dx)
105 : "a"(0xe801), "b"(0), "c"(0), "d"(0));
107 /* Not SeaBIOS, but in theory a BIOS could return CX=DX=0 in which
108 * case we need to use the result from AX & BX instead.
110 if (cx == 0 && dx == 0) {
116 /* DX = extended memory above 16M, in 64K units.
117 * Convert it to bytes and return.
119 ret = ((uint32_t)dx + 256 /* 16M in 64K units */) << 16;
121 /* This is a fallback path for machines with <= 16MB of RAM,
122 * which probably would never be the case, but deal with it
125 * CX = extended memory between 1M and 16M, in kilobytes
126 * Convert it to bytes and return.
128 ret = ((uint32_t)cx + 1024 /* 1M in K */) << 10;
134 /* Force the asm name without leading underscore, even on Win32. */
135 extern void load_kernel(void) asm("load_kernel");
137 void load_kernel(void)
144 uint32_t initrd_size;
145 uint32_t kernel_size;
146 uint32_t cmdline_size;
147 uint32_t initrd_end_page, max_allowed_page;
148 uint32_t segment_addr, stack_addr;
150 bios_cfg_read_entry_dma(&setup_addr, FW_CFG_SETUP_ADDR, 4);
151 bios_cfg_read_entry_dma(&setup_size, FW_CFG_SETUP_SIZE, 4);
152 bios_cfg_read_entry_dma(setup_addr, FW_CFG_SETUP_DATA, setup_size);
156 /* For protocol < 0x203 we don't have initrd_max ... */
157 if (readw_es(0x206) < 0x203) {
158 /* ... so we assume initrd_max = 0x37ffffff. */
159 writel_es(0x22c, 0x37ffffff);
162 bios_cfg_read_entry_dma(&initrd_addr, FW_CFG_INITRD_ADDR, 4);
163 bios_cfg_read_entry_dma(&initrd_size, FW_CFG_INITRD_SIZE, 4);
165 initrd_end_page = ((uint32_t)(initrd_addr + initrd_size) & -4096);
166 max_allowed_page = (readl_es(0x22c) & -4096);
168 if (initrd_end_page != 0 && max_allowed_page != 0 &&
169 initrd_end_page != max_allowed_page) {
170 /* Initrd at the end of memory. Compute better initrd address
173 initrd_addr = (void *)((get_e801_addr() - initrd_size) & -4096);
174 writel_es(0x218, (uint32_t)initrd_addr);
178 bios_cfg_read_entry_dma(initrd_addr, FW_CFG_INITRD_DATA, initrd_size);
180 bios_cfg_read_entry_dma(&kernel_addr, FW_CFG_KERNEL_ADDR, 4);
181 bios_cfg_read_entry_dma(&kernel_size, FW_CFG_KERNEL_SIZE, 4);
182 bios_cfg_read_entry_dma(kernel_addr, FW_CFG_KERNEL_DATA, kernel_size);
184 bios_cfg_read_entry_dma(&cmdline_addr, FW_CFG_CMDLINE_ADDR, 4);
185 bios_cfg_read_entry_dma(&cmdline_size, FW_CFG_CMDLINE_SIZE, 4);
186 bios_cfg_read_entry_dma(cmdline_addr, FW_CFG_CMDLINE_DATA, cmdline_size);
189 segment_addr = ((uint32_t)setup_addr >> 4);
190 stack_addr = (uint32_t)(cmdline_addr - setup_addr - 16);
192 /* As we are changing critical registers, we cannot leave freedom to the
195 asm("movw %%ax, %%ds\n"
200 "movl %%ebx, %%esp\n"
202 "pushw %%ax\n" /* CS */
203 "pushw $0\n" /* IP */
204 /* Clear registers and jump to Linux */
211 : : "a"(segment_addr), "b"(stack_addr));