grub/fs/i386/pc/pxe.c
proski eab58da22c 2009-07-16 Pavel Roskin <proski@gnu.org>
* kern/mm.c (grub_zalloc): New function.
	(grub_debug_zalloc): Likewise.
	* include/grub/mm.h: Declare grub_zalloc() and
	grub_debug_zalloc().
	* util/misc.c (grub_zalloc): New function.
	* bus/usb/uhci.c (grub_uhci_pci_iter): Use grub_zalloc()
	instead of grub_malloc(), remove unneeded initializations.
	* bus/usb/usbhub.c (grub_usb_hub_add_dev): Likewise.
	* commands/extcmd.c (grub_extcmd_dispatcher): Likewise.
	* commands/parttool.c (grub_cmd_parttool): Likewise.
	* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Likewise.
	* disk/raid5_recover.c (grub_raid5_recover): Likewise.
	* disk/raid6_recover.c (grub_raid6_recover): Likewise.
	* disk/usbms.c (grub_usbms_finddevs): Likewise.
	* efiemu/mm.c (grub_efiemu_request_memalign): Likewise.
	* efiemu/pnvram.c (grub_efiemu_pnvram): Likewise.
	(grub_cmd_efiemu_pnvram): Likewise.
	* fs/i386/pc/pxe.c (grub_pxefs_open): Likewise.
	* fs/iso9660.c (grub_iso9660_mount): Likewise.
	(grub_iso9660_iterate_dir): Likewise.
	* fs/jfs.c (grub_jfs_opendir): Likewise.
	* fs/ntfs.c (list_file): Likewise.
	(grub_ntfs_mount): Likewise.
	* kern/disk.c (grub_disk_open): Likewise.
	* kern/dl.c (grub_dl_load_core): Likewise.
	* kern/elf.c (grub_elf_file): Likewise.
	* kern/env.c (grub_env_context_open): Likewise.
	(grub_env_set): Likewise.
	(grub_env_set_data_slot): Likewise.
	* kern/file.c (grub_file_open): Likewise.
	* kern/fs.c (grub_fs_blocklist_open): Likewise.
	* loader/i386/multiboot.c (grub_module): Likewise.
	* loader/xnu.c (grub_xnu_create_key): Likewise.
	(grub_xnu_create_value): Likewise.
	* normal/main.c (grub_normal_add_menu_entry): Likewise.
	(read_config_file): Likewise.
	* normal/menu_entry.c (make_screen): Likewise.
	* partmap/sun.c (sun_partition_map_iterate): Likewise.
	* script/sh/lexer.c (grub_script_lexer_init): Likewise.
	* script/sh/script.c (grub_script_parse): Likewise.
	* video/bitmap.c (grub_video_bitmap_create): Likewise.
	* video/readers/jpeg.c (grub_video_reader_jpeg): Likewise.
	* video/readers/png.c (grub_png_output_byte): Likewise.
	(grub_video_reader_png): Likewise.
2009-07-16 22:14:09 +00:00

324 lines
7.4 KiB
C

/* pxe.c - Driver to provide access to the pxe filesystem */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008 Free Software Foundation, Inc.
*
* GRUB 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, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/fs.h>
#include <grub/mm.h>
#include <grub/disk.h>
#include <grub/file.h>
#include <grub/misc.h>
#include <grub/bufio.h>
#include <grub/machine/pxe.h>
#include <grub/machine/memory.h>
#define SEGMENT(x) ((x) >> 4)
#define OFFSET(x) ((x) & 0xF)
#define SEGOFS(x) ((SEGMENT(x) << 16) + OFFSET(x))
#define LINEAR(x) (void *) (((x >> 16) <<4) + (x & 0xFFFF))
struct grub_pxenv *grub_pxe_pxenv;
grub_uint32_t grub_pxe_your_ip;
grub_uint32_t grub_pxe_server_ip;
grub_uint32_t grub_pxe_gateway_ip;
int grub_pxe_blksize = GRUB_PXE_MIN_BLKSIZE;
static grub_file_t curr_file = 0;
struct grub_pxe_data
{
grub_uint32_t packet_number;
grub_uint32_t block_size;
char filename[0];
};
static int
grub_pxe_iterate (int (*hook) (const char *name))
{
if (hook ("pxe"))
return 1;
return 0;
}
static grub_err_t
grub_pxe_open (const char *name, grub_disk_t disk)
{
if (grub_strcmp (name, "pxe"))
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a pxe disk");
disk->total_sectors = 0;
disk->id = (unsigned long) "pxe";
disk->has_partitions = 0;
disk->data = 0;
return GRUB_ERR_NONE;
}
static void
grub_pxe_close (grub_disk_t disk __attribute((unused)))
{
}
static grub_err_t
grub_pxe_read (grub_disk_t disk __attribute((unused)),
grub_disk_addr_t sector __attribute((unused)),
grub_size_t size __attribute((unused)),
char *buf __attribute((unused)))
{
return GRUB_ERR_OUT_OF_RANGE;
}
static grub_err_t
grub_pxe_write (grub_disk_t disk __attribute((unused)),
grub_disk_addr_t sector __attribute((unused)),
grub_size_t size __attribute((unused)),
const char *buf __attribute((unused)))
{
return GRUB_ERR_OUT_OF_RANGE;
}
static struct grub_disk_dev grub_pxe_dev =
{
.name = "pxe",
.id = GRUB_DISK_DEVICE_PXE_ID,
.iterate = grub_pxe_iterate,
.open = grub_pxe_open,
.close = grub_pxe_close,
.read = grub_pxe_read,
.write = grub_pxe_write,
.next = 0
};
static grub_err_t
grub_pxefs_dir (grub_device_t device UNUSED, const char *path UNUSED,
int (*hook) (const char *filename,
const struct grub_dirhook_info *info) UNUSED)
{
return GRUB_ERR_NONE;
}
static grub_err_t
grub_pxefs_open (struct grub_file *file, const char *name)
{
union
{
struct grub_pxenv_tftp_get_fsize c1;
struct grub_pxenv_tftp_open c2;
} c;
struct grub_pxe_data *data;
grub_file_t file_int, bufio;
if (curr_file != 0)
{
grub_pxe_call (GRUB_PXENV_TFTP_CLOSE, &c.c2);
curr_file = 0;
}
c.c1.server_ip = grub_pxe_server_ip;
c.c1.gateway_ip = grub_pxe_gateway_ip;
grub_strcpy ((char *)&c.c1.filename[0], name);
grub_pxe_call (GRUB_PXENV_TFTP_GET_FSIZE, &c.c1);
if (c.c1.status)
return grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
file->size = c.c1.file_size;
c.c2.tftp_port = grub_cpu_to_be16 (GRUB_PXE_TFTP_PORT);
c.c2.packet_size = grub_pxe_blksize;
grub_pxe_call (GRUB_PXENV_TFTP_OPEN, &c.c2);
if (c.c2.status)
return grub_error (GRUB_ERR_BAD_FS, "open fails");
data = grub_zalloc (sizeof (struct grub_pxe_data) + grub_strlen (name) + 1);
if (! data)
return grub_errno;
data->block_size = grub_pxe_blksize;
grub_strcpy (data->filename, name);
file_int = grub_malloc (sizeof (*file_int));
if (! file_int)
{
grub_free (data);
return grub_errno;
}
file->data = data;
grub_memcpy (file_int, file, sizeof (struct grub_file));
curr_file = file_int;
bufio = grub_bufio_open (file_int, data->block_size);
if (! bufio)
{
grub_free (file_int);
grub_free (data);
return grub_errno;
}
grub_memcpy (file, bufio, sizeof (struct grub_file));
return GRUB_ERR_NONE;
}
static grub_ssize_t
grub_pxefs_read (grub_file_t file, char *buf, grub_size_t len)
{
struct grub_pxenv_tftp_read c;
struct grub_pxe_data *data;
grub_uint32_t pn, r;
data = file->data;
pn = grub_divmod64 (file->offset, data->block_size, &r);
if (r)
{
grub_error (GRUB_ERR_BAD_FS,
"read access must be aligned to packet size");
return -1;
}
if ((curr_file != file) || (data->packet_number > pn))
{
struct grub_pxenv_tftp_open o;
if (curr_file != 0)
grub_pxe_call (GRUB_PXENV_TFTP_CLOSE, &o);
o.server_ip = grub_pxe_server_ip;
o.gateway_ip = grub_pxe_gateway_ip;
grub_strcpy ((char *)&o.filename[0], data->filename);
o.tftp_port = grub_cpu_to_be16 (GRUB_PXE_TFTP_PORT);
o.packet_size = data->block_size;
grub_pxe_call (GRUB_PXENV_TFTP_OPEN, &o);
if (o.status)
{
grub_error (GRUB_ERR_BAD_FS, "open fails");
return -1;
}
data->packet_number = 0;
curr_file = file;
}
c.buffer = SEGOFS (GRUB_MEMORY_MACHINE_SCRATCH_ADDR);
while (pn >= data->packet_number)
{
c.buffer_size = grub_pxe_blksize;
grub_pxe_call (GRUB_PXENV_TFTP_READ, &c);
if (c.status)
{
grub_error (GRUB_ERR_BAD_FS, "read fails");
return -1;
}
data->packet_number++;
}
grub_memcpy (buf, (char *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, len);
return len;
}
static grub_err_t
grub_pxefs_close (grub_file_t file)
{
struct grub_pxenv_tftp_close c;
if (curr_file == file)
{
grub_pxe_call (GRUB_PXENV_TFTP_CLOSE, &c);
curr_file = 0;
}
grub_free (file->data);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_pxefs_label (grub_device_t device __attribute ((unused)),
char **label __attribute ((unused)))
{
*label = 0;
return GRUB_ERR_NONE;
}
static struct grub_fs grub_pxefs_fs =
{
.name = "pxefs",
.dir = grub_pxefs_dir,
.open = grub_pxefs_open,
.read = grub_pxefs_read,
.close = grub_pxefs_close,
.label = grub_pxefs_label,
.next = 0
};
static void
grub_pxe_detect (void)
{
struct grub_pxenv *pxenv;
struct grub_pxenv_get_cached_info ci;
struct grub_pxenv_boot_player *bp;
pxenv = grub_pxe_scan ();
if (! pxenv)
return;
ci.packet_type = GRUB_PXENV_PACKET_TYPE_DHCP_ACK;
ci.buffer = 0;
ci.buffer_size = 0;
grub_pxe_call (GRUB_PXENV_GET_CACHED_INFO, &ci);
if (ci.status)
return;
bp = LINEAR (ci.buffer);
grub_pxe_your_ip = bp->your_ip;
grub_pxe_server_ip = bp->server_ip;
grub_pxe_gateway_ip = bp->gateway_ip;
grub_pxe_pxenv = pxenv;
}
void
grub_pxe_unload (void)
{
if (grub_pxe_pxenv)
{
grub_fs_unregister (&grub_pxefs_fs);
grub_disk_dev_unregister (&grub_pxe_dev);
grub_pxe_pxenv = 0;
}
}
GRUB_MOD_INIT(pxe)
{
grub_pxe_detect ();
if (grub_pxe_pxenv)
{
grub_disk_dev_register (&grub_pxe_dev);
grub_fs_register (&grub_pxefs_fs);
}
}
GRUB_MOD_FINI(pxe)
{
grub_pxe_unload ();
}