From: Kasper Dupont <kasperd@fjtcb.17.jan.2016.kasperd.net>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH] Add support for using directory as initrd
Date: Mon, 8 Feb 2016 15:43:10 +0100 [thread overview]
Message-ID: <20160208144309.GA10770@colin.search.kasperd.net> (raw)
Signed-off-by: Kasper Dupont <kasperd@fjtcb.17.jan.2016.kasperd.net>
---
hw/core/loader.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++
hw/i386/pc.c | 6 +++--
include/hw/loader.h | 1 +
3 files changed, 76 insertions(+), 2 deletions(-)
diff --git a/hw/core/loader.c b/hw/core/loader.c
index 3a57415..6047486 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -54,6 +54,7 @@
#include "exec/address-spaces.h"
#include "hw/boards.h"
+#include <error.h>
#include <zlib.h>
static int roms_loaded;
@@ -164,6 +165,76 @@ void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
}
}
+/* return the size or -1 if error */
+int load_initrd(const char *filename, uint8_t **addr)
+{
+ pid_t child = 0;
+ int fd, size = 0, buffer_size = 4096;
+ fd = open(filename, O_RDONLY | O_BINARY);
+ if (fd < 0) {
+ return -1;
+ }
+
+ *addr = malloc(buffer_size);
+
+ while (*addr) {
+ int bytes_read = read(fd, (*addr) + size, buffer_size - size);
+
+ if (bytes_read == 0) {
+ close(fd);
+ if (child) {
+ waitpid(child, NULL, 0);
+ }
+ return size;
+ }
+
+ if (bytes_read > 0) {
+ size += bytes_read;
+ } else {
+ int pipefd[2];
+
+ if (errno != EISDIR) {
+ return -1;
+ }
+
+ assert(!child);
+
+ if (pipe(pipefd)) {
+ return -1;
+ }
+
+ child = fork();
+ if (child == -1) {
+ return -1;
+ }
+
+ if (!child) {
+ if (fchdir(fd)) {
+ error(1, errno, "fchdir failed");
+ }
+ if (dup2(pipefd[1], 1) == -1) {
+ error(1, errno, "dup2 failed");
+ }
+ execlp("/bin/sh", "/bin/sh", "-c",
+ "find . | cpio --quiet -R 0:0 -o -H newc",
+ NULL);
+ error(1, errno, "/bin/sh");
+ }
+
+ close(fd);
+ close(pipefd[1]);
+ fd = pipefd[0];
+ }
+
+ if (size == buffer_size) {
+ buffer_size <<= 1;
+ *addr = realloc(*addr, buffer_size);
+ }
+ }
+
+ return -1;
+}
+
/* A.OUT loader */
struct exec
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index b28bac4..d9e1596 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -953,12 +953,13 @@ static void load_linux(PCMachineState *pcms,
/* load initrd */
if (initrd_filename) {
+ uint8_t *initrd_load_buffer;
if (protocol < 0x200) {
fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
exit(1);
}
- initrd_size = get_image_size(initrd_filename);
+ initrd_size = load_initrd(initrd_filename, &initrd_load_buffer);
if (initrd_size < 0) {
fprintf(stderr, "qemu: error reading initrd %s: %s\n",
initrd_filename, strerror(errno));
@@ -968,7 +969,8 @@ static void load_linux(PCMachineState *pcms,
initrd_addr = (initrd_max-initrd_size) & ~4095;
initrd_data = g_malloc(initrd_size);
- load_image(initrd_filename, initrd_data);
+ memcpy(initrd_data, initrd_load_buffer, initrd_size);
+ free(initrd_load_buffer);
fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
diff --git a/include/hw/loader.h b/include/hw/loader.h
index f7b43ab..4fb4773 100644
--- a/include/hw/loader.h
+++ b/include/hw/loader.h
@@ -13,6 +13,7 @@
*/
int get_image_size(const char *filename);
int load_image(const char *filename, uint8_t *addr); /* deprecated */
+int load_initrd(const char *filename, uint8_t **addr);
ssize_t load_image_size(const char *filename, void *addr, size_t size);
int load_image_targphys(const char *filename, hwaddr,
uint64_t max_sz);
--
1.9.1
reply other threads:[~2016-02-08 14:43 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160208144309.GA10770@colin.search.kasperd.net \
--to=kasperd@fjtcb.17.jan.2016.kasperd.net \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).