uefi: netboot: Wire in gigaboot.

If gigaboot is the selected netboot protocol, use it to boot the machine.

Change-Id: I3c125e882960030449c0e996b5cc1b348f4e50dc
diff --git a/src/module/uefi/netboot.c b/src/module/uefi/netboot.c
index a897b6c..52f6580 100644
--- a/src/module/uefi/netboot.c
+++ b/src/module/uefi/netboot.c
@@ -27,9 +27,70 @@
 #include "drivers/timer/timer.h"
 #include "module/module.h"
 #include "module/uefi/fwdb.h"
+#include "net/gigaboot/gigaboot.h"
 #include "net/netboot/netboot.h"
 #include "net/netboot/params.h"
 
+
+static GigabootBuffer *resize_gigaboot_buffer(GigabootBuffer *buf, size_t size)
+{
+	EFI_SYSTEM_TABLE *st = uefi_system_table_ptr();
+	if (!st)
+		return NULL;
+	EFI_BOOT_SERVICES *bs = st->BootServices;
+
+	if (buf->size < size) {
+		EFI_PHYSICAL_ADDRESS data = (EFI_PHYSICAL_ADDRESS)(buf->data);
+		if (bs->FreePages(data, buf->size / 4096)) {
+			printf("Could not free previous gigaboot buffer.\n");
+			buf->size = 0;
+			return NULL;
+		}
+		buf->size = 0;
+
+		data = 0xffffffff;
+		if (bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,
+				      size / 4096, &data)) {
+			printf("Failed to allocate gigaboot buffer.\n");
+			return NULL;
+		}
+		buf->data = (void *)data;
+		buf->size = size;
+	}
+	return buf;
+}
+
+GigabootBuffer *gigaboot_get_buffer(const char *name, size_t size)
+{
+	static GigabootBuffer kernel = {
+		.data = (void *)(uintptr_t)CONFIG_KERNEL_START,
+		.size = CONFIG_KERNEL_SIZE,
+	};
+
+	static char cmdline_buf[4096];
+	static GigabootBuffer cmdline = {
+		.data = cmdline_buf,
+		.size = sizeof(cmdline_buf),
+	};
+
+	static GigabootBuffer ramdisk;
+
+	if (!strcmp(name, gigaboot_buffer_kernel))
+		return &kernel;
+	if (!strcmp(name, gigaboot_buffer_cmdline))
+		return &cmdline;
+	if (!strcmp(name, gigaboot_buffer_ramdisk)) {
+		if (ramdisk.size != 0 && size == 0)
+			return &ramdisk;
+
+		size_t buf_size =
+			size > 0 ? (size + 4095) & ~4095 : 512 * 1024 * 1024;
+
+		return resize_gigaboot_buffer(&ramdisk, buf_size);
+	}
+	return NULL;
+}
+
 void module_main(void)
 {
 	static char cmd_line[4096];
@@ -43,5 +104,8 @@
 		halt();
 	}
 
-	netboot(cmd_line, sizeof(cmd_line));
+	if (CONFIG_GIGABOOT)
+		gigaboot();
+	else
+		netboot(cmd_line, sizeof(cmd_line));
 }