Set primary kernel in manifest and allow preloading.

The primary kernel was previously hard-coded as `vmlinuz` but the
manifest allows more flexibility. If the kernel file for any VM is not
specified, it is assumed that the image has been preloaded into memory.

The loading of the primary and secondaries are becoming increasingly
similar and will continue to do so.

Change-Id: I34f134d8a4d32e8ac92e142d3636902d52ad86ec
diff --git a/src/manifest.c b/src/manifest.c
index dc6ed5b..80382ee 100644
--- a/src/manifest.c
+++ b/src/manifest.c
@@ -53,17 +53,9 @@
 	return ptr;
 }
 
-static enum manifest_return_code read_string(const struct fdt_node *node,
-					     const char *property, char *out,
-					     rsize_t out_sz)
+static enum manifest_return_code extract_string(const char *data, uint32_t size,
+						char *out, rsize_t out_sz)
 {
-	const char *data;
-	uint32_t size;
-
-	if (!fdt_read_property(node, property, &data, &size)) {
-		return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
-	}
-
 	/*
 	 * Require that the value contains exactly one NULL character and that
 	 * it is the last byte.
@@ -81,6 +73,39 @@
 	return MANIFEST_SUCCESS;
 }
 
+static enum manifest_return_code read_string(const struct fdt_node *node,
+					     const char *property, char *out,
+					     rsize_t out_sz)
+{
+	const char *data;
+	uint32_t size;
+
+	if (!fdt_read_property(node, property, &data, &size)) {
+		return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
+	}
+
+	return extract_string(data, size, out, out_sz);
+}
+
+static enum manifest_return_code read_optional_string(
+	const struct fdt_node *node, const char *property, char *out,
+	rsize_t out_sz)
+{
+	const char *data;
+	uint32_t size;
+
+	if (!fdt_read_property(node, property, &data, &size)) {
+		if (out_sz < 1) {
+			return MANIFEST_ERROR_STRING_TOO_LONG;
+		}
+
+		*out = '\0';
+		return MANIFEST_SUCCESS;
+	}
+
+	return extract_string(data, size, out, out_sz);
+}
+
 static enum manifest_return_code read_uint64(const struct fdt_node *node,
 					     const char *property,
 					     uint64_t *out)
@@ -202,10 +227,9 @@
 {
 	TRY(read_string(node, "debug_name", vm->debug_name,
 			sizeof(vm->debug_name)));
+	TRY(read_optional_string(node, "kernel_filename", vm->kernel_filename,
+				 sizeof(vm->kernel_filename)));
 	if (vm_id != HF_PRIMARY_VM_ID) {
-		TRY(read_string(node, "kernel_filename",
-				vm->secondary.kernel_filename,
-				sizeof(vm->secondary.kernel_filename)));
 		TRY(read_uint64(node, "mem_size", &vm->secondary.mem_size));
 		TRY(read_uint16(node, "vcpu_count", &vm->secondary.vcpu_count));
 	}