summaryrefslogtreecommitdiffstats
path: root/src/tests/tftptest.c
blob: 57f5150266b25bb2d24f282b4081b017bca45359 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdint.h>
#include <string.h>
#include <console.h>
#include <gpxe/udp.h>
#include <gpxe/tftp.h>
#include <gpxe/async.h>
#include <gpxe/uaccess.h>
#include <gpxe/buffer.h>
#include <gpxe/elf.h>
#include <bios.h>
#include "pxe.h"

int test_tftp ( struct net_device *netdev, struct sockaddr_tcpip *target,
		const char *filename ) {
	struct tftp_session tftp;
	struct buffer buffer;
	struct elf elf;
	uint16_t fbms;
	int rc;

	memset ( &buffer, 0, sizeof ( buffer ) );
	buffer.addr = real_to_user ( 0, 0x7c00 );
	get_real ( fbms, BDA_SEG, BDA_FBMS );
	buffer.len = ( fbms * 1024 - 0x7c00 );

	memset ( &tftp, 0, sizeof ( tftp ) );
	udp_connect ( &tftp.udp, target );
	tftp.filename = filename;
	tftp.buffer = &buffer;

	printf ( "Fetching \"%s\" via TFTP\n", filename );
	if ( ( rc = async_wait ( tftp_get ( &tftp ) ) ) != 0 )
		return rc;

	elf.image = buffer.addr;
	elf.len = buffer.len;
	if ( ( rc = elf_load ( &elf ) ) == 0 ) {
		printf ( "Got valid ELF image: execaddr at %lx\n",
			 elf.entry );
		return 0;
	}

	printf ( "Attempting PXE boot\n" );
	pxe_netdev = netdev;
	rc = pxe_boot();
	printf ( "PXE NBP returned with status %04x\n", rc );
	return 0;
}