| 1 |
/* pxe.c - Driver to provide access to the pxe filesystem */ |
| 2 |
/* |
| 3 |
* GRUB -- GRand Unified Bootloader |
| 4 |
* Copyright (C) 2008,2009,2011 Free Software Foundation, Inc. |
| 5 |
* |
| 6 |
* GRUB is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation, either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* GRUB is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <grub/dl.h> |
| 21 |
#include <grub/net.h> |
| 22 |
#include <grub/mm.h> |
| 23 |
#include <grub/file.h> |
| 24 |
#include <grub/misc.h> |
| 25 |
#include <grub/env.h> |
| 26 |
#include <grub/i18n.h> |
| 27 |
#include <grub/loader.h> |
| 28 |
|
| 29 |
#include <grub/machine/pxe.h> |
| 30 |
#include <grub/machine/int.h> |
| 31 |
#include <grub/machine/memory.h> |
| 32 |
#include <grub/machine/kernel.h> |
| 33 |
|
| 34 |
GRUB_MOD_LICENSE ("GPLv3+"); |
| 35 |
|
| 36 |
#define SEGMENT(x) ((x) >> 4) |
| 37 |
#define OFFSET(x) ((x) & 0xF) |
| 38 |
#define SEGOFS(x) ((SEGMENT(x) << 16) + OFFSET(x)) |
| 39 |
#define LINEAR(x) (void *) ((((x) >> 16) << 4) + ((x) & 0xFFFF)) |
| 40 |
|
| 41 |
struct grub_pxe_undi_open |
| 42 |
{ |
| 43 |
grub_uint16_t status; |
| 44 |
grub_uint16_t open_flag; |
| 45 |
grub_uint16_t pkt_filter; |
| 46 |
grub_uint16_t mcast_count; |
| 47 |
grub_uint8_t mcast[8][6]; |
| 48 |
} GRUB_PACKED; |
| 49 |
|
| 50 |
struct grub_pxe_undi_info |
| 51 |
{ |
| 52 |
grub_uint16_t status; |
| 53 |
grub_uint16_t base_io; |
| 54 |
grub_uint16_t int_number; |
| 55 |
grub_uint16_t mtu; |
| 56 |
grub_uint16_t hwtype; |
| 57 |
grub_uint16_t hwaddrlen; |
| 58 |
grub_uint8_t current_addr[16]; |
| 59 |
grub_uint8_t permanent_addr[16]; |
| 60 |
grub_uint32_t romaddr; |
| 61 |
grub_uint16_t rxbufct; |
| 62 |
grub_uint16_t txbufct; |
| 63 |
} GRUB_PACKED; |
| 64 |
|
| 65 |
|
| 66 |
struct grub_pxe_undi_isr |
| 67 |
{ |
| 68 |
grub_uint16_t status; |
| 69 |
grub_uint16_t func_flag; |
| 70 |
grub_uint16_t buffer_len; |
| 71 |
grub_uint16_t frame_len; |
| 72 |
grub_uint16_t frame_hdr_len; |
| 73 |
grub_uint32_t buffer; |
| 74 |
grub_uint8_t prot_type; |
| 75 |
grub_uint8_t pkt_type; |
| 76 |
} GRUB_PACKED; |
| 77 |
|
| 78 |
enum |
| 79 |
{ |
| 80 |
GRUB_PXE_ISR_IN_START = 1, |
| 81 |
GRUB_PXE_ISR_IN_PROCESS, |
| 82 |
GRUB_PXE_ISR_IN_GET_NEXT |
| 83 |
}; |
| 84 |
|
| 85 |
enum |
| 86 |
{ |
| 87 |
GRUB_PXE_ISR_OUT_OURS = 0, |
| 88 |
GRUB_PXE_ISR_OUT_NOT_OURS = 1 |
| 89 |
}; |
| 90 |
|
| 91 |
enum |
| 92 |
{ |
| 93 |
GRUB_PXE_ISR_OUT_DONE = 0, |
| 94 |
GRUB_PXE_ISR_OUT_TRANSMIT = 2, |
| 95 |
GRUB_PXE_ISR_OUT_RECEIVE = 3, |
| 96 |
GRUB_PXE_ISR_OUT_BUSY = 4, |
| 97 |
}; |
| 98 |
|
| 99 |
struct grub_pxe_undi_transmit |
| 100 |
{ |
| 101 |
grub_uint16_t status; |
| 102 |
grub_uint8_t protocol; |
| 103 |
grub_uint8_t xmitflag; |
| 104 |
grub_uint32_t dest; |
| 105 |
grub_uint32_t tbd; |
| 106 |
grub_uint32_t reserved[2]; |
| 107 |
} GRUB_PACKED; |
| 108 |
|
| 109 |
struct grub_pxe_undi_tbd |
| 110 |
{ |
| 111 |
grub_uint16_t len; |
| 112 |
grub_uint32_t buf; |
| 113 |
grub_uint16_t blk_count; |
| 114 |
struct |
| 115 |
{ |
| 116 |
grub_uint8_t ptr_type; |
| 117 |
grub_uint8_t reserved; |
| 118 |
grub_uint16_t len; |
| 119 |
grub_uint32_t ptr; |
| 120 |
} blocks[8]; |
| 121 |
} GRUB_PACKED; |
| 122 |
|
| 123 |
struct grub_pxe_bangpxe *grub_pxe_pxenv; |
| 124 |
static grub_uint32_t pxe_rm_entry = 0; |
| 125 |
|
| 126 |
static struct grub_pxe_bangpxe * |
| 127 |
grub_pxe_scan (void) |
| 128 |
{ |
| 129 |
struct grub_bios_int_registers regs; |
| 130 |
struct grub_pxenv *pxenv; |
| 131 |
struct grub_pxe_bangpxe *bangpxe; |
| 132 |
|
| 133 |
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; |
| 134 |
|
| 135 |
regs.ebx = 0; |
| 136 |
regs.ecx = 0; |
| 137 |
regs.eax = 0x5650; |
| 138 |
regs.es = 0; |
| 139 |
|
| 140 |
grub_bios_interrupt (0x1a, ®s); |
| 141 |
|
| 142 |
if ((regs.eax & 0xffff) != 0x564e) |
| 143 |
return NULL; |
| 144 |
|
| 145 |
pxenv = (struct grub_pxenv *) ((regs.es << 4) + (regs.ebx & 0xffff)); |
| 146 |
if (grub_memcmp (pxenv->signature, GRUB_PXE_SIGNATURE, |
| 147 |
sizeof (pxenv->signature)) |
| 148 |
!= 0) |
| 149 |
return NULL; |
| 150 |
|
| 151 |
if (pxenv->version < 0x201) |
| 152 |
return NULL; |
| 153 |
|
| 154 |
bangpxe = (void *) ((((pxenv->pxe_ptr & 0xffff0000) >> 16) << 4) |
| 155 |
+ (pxenv->pxe_ptr & 0xffff)); |
| 156 |
|
| 157 |
if (!bangpxe) |
| 158 |
return NULL; |
| 159 |
|
| 160 |
if (grub_memcmp (bangpxe->signature, GRUB_PXE_BANGPXE_SIGNATURE, |
| 161 |
sizeof (bangpxe->signature)) != 0) |
| 162 |
return NULL; |
| 163 |
|
| 164 |
pxe_rm_entry = bangpxe->rm_entry; |
| 165 |
|
| 166 |
return bangpxe; |
| 167 |
} |
| 168 |
|
| 169 |
static struct grub_net_buff * |
| 170 |
grub_pxe_recv (struct grub_net_card *dev __attribute__ ((unused))) |
| 171 |
{ |
| 172 |
struct grub_pxe_undi_isr *isr; |
| 173 |
static int in_progress = 0; |
| 174 |
grub_uint8_t *ptr, *end; |
| 175 |
struct grub_net_buff *buf; |
| 176 |
|
| 177 |
isr = (void *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR); |
| 178 |
|
| 179 |
if (!in_progress) |
| 180 |
{ |
| 181 |
grub_memset (isr, 0, sizeof (*isr)); |
| 182 |
isr->func_flag = GRUB_PXE_ISR_IN_START; |
| 183 |
grub_pxe_call (GRUB_PXENV_UNDI_ISR, isr, pxe_rm_entry); |
| 184 |
/* Normally according to the specification we should also check |
| 185 |
that isr->func_flag != GRUB_PXE_ISR_OUT_OURS but unfortunately it |
| 186 |
breaks on intel cards. |
| 187 |
*/ |
| 188 |
if (isr->status) |
| 189 |
{ |
| 190 |
in_progress = 0; |
| 191 |
return NULL; |
| 192 |
} |
| 193 |
grub_memset (isr, 0, sizeof (*isr)); |
| 194 |
isr->func_flag = GRUB_PXE_ISR_IN_PROCESS; |
| 195 |
grub_pxe_call (GRUB_PXENV_UNDI_ISR, isr, pxe_rm_entry); |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
grub_memset (isr, 0, sizeof (*isr)); |
| 200 |
isr->func_flag = GRUB_PXE_ISR_IN_GET_NEXT; |
| 201 |
grub_pxe_call (GRUB_PXENV_UNDI_ISR, isr, pxe_rm_entry); |
| 202 |
} |
| 203 |
|
| 204 |
while (isr->func_flag != GRUB_PXE_ISR_OUT_RECEIVE) |
| 205 |
{ |
| 206 |
if (isr->status || isr->func_flag == GRUB_PXE_ISR_OUT_DONE) |
| 207 |
{ |
| 208 |
in_progress = 0; |
| 209 |
return NULL; |
| 210 |
} |
| 211 |
grub_memset (isr, 0, sizeof (*isr)); |
| 212 |
isr->func_flag = GRUB_PXE_ISR_IN_GET_NEXT; |
| 213 |
grub_pxe_call (GRUB_PXENV_UNDI_ISR, isr, pxe_rm_entry); |
| 214 |
} |
| 215 |
|
| 216 |
buf = grub_netbuff_alloc (isr->frame_len + 2); |
| 217 |
if (!buf) |
| 218 |
return NULL; |
| 219 |
/* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible |
| 220 |
by 4. So that IP header is aligned on 4 bytes. */ |
| 221 |
if (grub_netbuff_reserve (buf, 2)) |
| 222 |
{ |
| 223 |
grub_netbuff_free (buf); |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
ptr = buf->data; |
| 227 |
end = ptr + isr->frame_len; |
| 228 |
grub_netbuff_put (buf, isr->frame_len); |
| 229 |
grub_memcpy (ptr, LINEAR (isr->buffer), isr->buffer_len); |
| 230 |
ptr += isr->buffer_len; |
| 231 |
while (ptr < end) |
| 232 |
{ |
| 233 |
grub_memset (isr, 0, sizeof (*isr)); |
| 234 |
isr->func_flag = GRUB_PXE_ISR_IN_GET_NEXT; |
| 235 |
grub_pxe_call (GRUB_PXENV_UNDI_ISR, isr, pxe_rm_entry); |
| 236 |
if (isr->status || isr->func_flag != GRUB_PXE_ISR_OUT_RECEIVE) |
| 237 |
{ |
| 238 |
in_progress = 1; |
| 239 |
grub_netbuff_free (buf); |
| 240 |
return NULL; |
| 241 |
} |
| 242 |
|
| 243 |
grub_memcpy (ptr, LINEAR (isr->buffer), isr->buffer_len); |
| 244 |
ptr += isr->buffer_len; |
| 245 |
} |
| 246 |
in_progress = 1; |
| 247 |
|
| 248 |
return buf; |
| 249 |
} |
| 250 |
|
| 251 |
static grub_err_t |
| 252 |
grub_pxe_send (struct grub_net_card *dev __attribute__ ((unused)), |
| 253 |
struct grub_net_buff *pack) |
| 254 |
{ |
| 255 |
struct grub_pxe_undi_transmit *trans; |
| 256 |
struct grub_pxe_undi_tbd *tbd; |
| 257 |
char *buf; |
| 258 |
|
| 259 |
trans = (void *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR); |
| 260 |
grub_memset (trans, 0, sizeof (*trans)); |
| 261 |
tbd = (void *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR + 128); |
| 262 |
grub_memset (tbd, 0, sizeof (*tbd)); |
| 263 |
buf = (void *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR + 256); |
| 264 |
grub_memcpy (buf, pack->data, pack->tail - pack->data); |
| 265 |
|
| 266 |
trans->tbd = SEGOFS ((grub_addr_t) tbd); |
| 267 |
trans->protocol = 0; |
| 268 |
tbd->len = pack->tail - pack->data; |
| 269 |
tbd->buf = SEGOFS ((grub_addr_t) buf); |
| 270 |
|
| 271 |
grub_pxe_call (GRUB_PXENV_UNDI_TRANSMIT, trans, pxe_rm_entry); |
| 272 |
if (trans->status) |
| 273 |
return grub_error (GRUB_ERR_IO, N_("couldn't send network packet")); |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
static void |
| 278 |
grub_pxe_close (struct grub_net_card *dev __attribute__ ((unused))) |
| 279 |
{ |
| 280 |
if (pxe_rm_entry) |
| 281 |
grub_pxe_call (GRUB_PXENV_UNDI_CLOSE, |
| 282 |
(void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, |
| 283 |
pxe_rm_entry); |
| 284 |
} |
| 285 |
|
| 286 |
static grub_err_t |
| 287 |
grub_pxe_open (struct grub_net_card *dev __attribute__ ((unused))) |
| 288 |
{ |
| 289 |
struct grub_pxe_undi_open *ou; |
| 290 |
ou = (void *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR); |
| 291 |
grub_memset (ou, 0, sizeof (*ou)); |
| 292 |
ou->pkt_filter = 4; |
| 293 |
grub_pxe_call (GRUB_PXENV_UNDI_OPEN, ou, pxe_rm_entry); |
| 294 |
|
| 295 |
if (ou->status) |
| 296 |
return grub_error (GRUB_ERR_IO, "can't open UNDI"); |
| 297 |
return GRUB_ERR_NONE; |
| 298 |
} |
| 299 |
|
| 300 |
struct grub_net_card_driver grub_pxe_card_driver = |
| 301 |
{ |
| 302 |
.open = grub_pxe_open, |
| 303 |
.close = grub_pxe_close, |
| 304 |
.send = grub_pxe_send, |
| 305 |
.recv = grub_pxe_recv |
| 306 |
}; |
| 307 |
|
| 308 |
struct grub_net_card grub_pxe_card = |
| 309 |
{ |
| 310 |
.driver = &grub_pxe_card_driver, |
| 311 |
.name = "pxe" |
| 312 |
}; |
| 313 |
|
| 314 |
static grub_err_t |
| 315 |
grub_pxe_shutdown (int flags) |
| 316 |
{ |
| 317 |
if (flags & GRUB_LOADER_FLAG_PXE_NOT_UNLOAD) |
| 318 |
return GRUB_ERR_NONE; |
| 319 |
if (!pxe_rm_entry) |
| 320 |
return GRUB_ERR_NONE; |
| 321 |
|
| 322 |
grub_pxe_call (GRUB_PXENV_UNDI_CLOSE, |
| 323 |
(void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, |
| 324 |
pxe_rm_entry); |
| 325 |
grub_pxe_call (GRUB_PXENV_UNDI_SHUTDOWN, |
| 326 |
(void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, |
| 327 |
pxe_rm_entry); |
| 328 |
grub_pxe_call (GRUB_PXENV_UNLOAD_STACK, |
| 329 |
(void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, |
| 330 |
pxe_rm_entry); |
| 331 |
pxe_rm_entry = 0; |
| 332 |
grub_net_card_unregister (&grub_pxe_card); |
| 333 |
|
| 334 |
return GRUB_ERR_NONE; |
| 335 |
} |
| 336 |
|
| 337 |
/* Nothing we can do. */ |
| 338 |
static grub_err_t |
| 339 |
grub_pxe_restore (void) |
| 340 |
{ |
| 341 |
return GRUB_ERR_NONE; |
| 342 |
} |
| 343 |
|
| 344 |
void * |
| 345 |
grub_pxe_get_cached (grub_uint16_t type) |
| 346 |
{ |
| 347 |
struct grub_pxenv_get_cached_info ci; |
| 348 |
ci.packet_type = type; |
| 349 |
ci.buffer = 0; |
| 350 |
ci.buffer_size = 0; |
| 351 |
grub_pxe_call (GRUB_PXENV_GET_CACHED_INFO, &ci, pxe_rm_entry); |
| 352 |
if (ci.status) |
| 353 |
return 0; |
| 354 |
|
| 355 |
return LINEAR (ci.buffer); |
| 356 |
} |
| 357 |
|
| 358 |
static void |
| 359 |
grub_pc_net_config_real (char **device, char **path) |
| 360 |
{ |
| 361 |
struct grub_net_bootp_packet *bp; |
| 362 |
|
| 363 |
bp = grub_pxe_get_cached (GRUB_PXENV_PACKET_TYPE_DHCP_ACK); |
| 364 |
|
| 365 |
if (!bp) |
| 366 |
return; |
| 367 |
grub_net_configure_by_dhcp_ack ("pxe", &grub_pxe_card, 0, |
| 368 |
bp, GRUB_PXE_BOOTP_SIZE, |
| 369 |
1, device, path); |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
static struct grub_preboot *fini_hnd; |
| 374 |
|
| 375 |
GRUB_MOD_INIT(pxe) |
| 376 |
{ |
| 377 |
struct grub_pxe_bangpxe *pxenv; |
| 378 |
struct grub_pxe_undi_info *ui; |
| 379 |
unsigned i; |
| 380 |
|
| 381 |
pxenv = grub_pxe_scan (); |
| 382 |
if (! pxenv) |
| 383 |
return; |
| 384 |
|
| 385 |
ui = (void *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR); |
| 386 |
grub_memset (ui, 0, sizeof (*ui)); |
| 387 |
grub_pxe_call (GRUB_PXENV_UNDI_GET_INFORMATION, ui, pxe_rm_entry); |
| 388 |
|
| 389 |
grub_memcpy (grub_pxe_card.default_address.mac, ui->current_addr, |
| 390 |
sizeof (grub_pxe_card.default_address.mac)); |
| 391 |
for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++) |
| 392 |
if (grub_pxe_card.default_address.mac[i] != 0) |
| 393 |
break; |
| 394 |
if (i != sizeof (grub_pxe_card.default_address.mac)) |
| 395 |
{ |
| 396 |
for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++) |
| 397 |
if (grub_pxe_card.default_address.mac[i] != 0xff) |
| 398 |
break; |
| 399 |
} |
| 400 |
if (i == sizeof (grub_pxe_card.default_address.mac)) |
| 401 |
grub_memcpy (grub_pxe_card.default_address.mac, ui->permanent_addr, |
| 402 |
sizeof (grub_pxe_card.default_address.mac)); |
| 403 |
grub_pxe_card.mtu = ui->mtu; |
| 404 |
|
| 405 |
grub_pxe_card.default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET; |
| 406 |
|
| 407 |
grub_net_card_register (&grub_pxe_card); |
| 408 |
grub_pc_net_config = grub_pc_net_config_real; |
| 409 |
fini_hnd = grub_loader_register_preboot_hook (grub_pxe_shutdown, |
| 410 |
grub_pxe_restore, |
| 411 |
GRUB_LOADER_PREBOOT_HOOK_PRIO_DISK); |
| 412 |
} |
| 413 |
|
| 414 |
GRUB_MOD_FINI(pxe) |
| 415 |
{ |
| 416 |
grub_pc_net_config = 0; |
| 417 |
grub_pxe_shutdown (0); |
| 418 |
grub_loader_unregister_preboot_hook (fini_hnd); |
| 419 |
} |