From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:50250) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RGE3F-0001NT-MQ for qemu-devel@nongnu.org; Tue, 18 Oct 2011 14:06:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RGE3E-0001CT-2k for qemu-devel@nongnu.org; Tue, 18 Oct 2011 14:06:09 -0400 Received: from mail-vw0-f45.google.com ([209.85.212.45]:57310) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RGE3D-0001CI-S3 for qemu-devel@nongnu.org; Tue, 18 Oct 2011 14:06:08 -0400 Received: by vws17 with SMTP id 17so692493vws.4 for ; Tue, 18 Oct 2011 11:06:07 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <1318878968-18090-3-git-send-email-jordan.l.justen@intel.com> References: <1318878968-18090-1-git-send-email-jordan.l.justen@intel.com> <1318878968-18090-3-git-send-email-jordan.l.justen@intel.com> From: Blue Swirl Date: Tue, 18 Oct 2011 18:05:47 +0000 Message-ID: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 3/4] loader: Add rom_add_file_buf for adding roms from a buffer List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jordan Justen Cc: qemu-devel@nongnu.org On Mon, Oct 17, 2011 at 7:16 PM, Jordan Justen wrote: > rom_add_file_buf is similar to rom_add_file, except the rom's > contents are provided in a buffer. > > rom_add_file is modified to call rom_add_file_buf after > reading the rom's contents from the file. > > Signed-off-by: Jordan Justen > --- > =C2=A0hw/loader.c | =C2=A0 71 +++++++++++++++++++++++++++++++++++++++----= --------------- > =C2=A0hw/loader.h | =C2=A0 =C2=A05 ++++ > =C2=A02 files changed, 53 insertions(+), 23 deletions(-) > > diff --git a/hw/loader.c b/hw/loader.c > index 5676c18..d1a4a98 100644 > --- a/hw/loader.c > +++ b/hw/loader.c > @@ -557,11 +557,11 @@ static void rom_insert(Rom *rom) > =C2=A0 =C2=A0 QTAILQ_INSERT_TAIL(&roms, rom, next); > =C2=A0} > > -int rom_add_file(const char *file, const char *fw_dir, > - =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 target_phys_add= r_t addr, int32_t bootindex) > +int rom_add_file_buf(const char *file, const void *data, size_t size, > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 c= onst char *fw_dir, > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 t= arget_phys_addr_t addr, int32_t bootindex) > =C2=A0{ > =C2=A0 =C2=A0 Rom *rom; > - =C2=A0 =C2=A0int rc, fd =3D -1; > =C2=A0 =C2=A0 char devpath[100]; > > =C2=A0 =C2=A0 rom =3D g_malloc0(sizeof(*rom)); > @@ -571,28 +571,16 @@ int rom_add_file(const char *file, const char *fw_d= ir, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 rom->path =3D g_strdup(file); > =C2=A0 =C2=A0 } > > - =C2=A0 =C2=A0fd =3D open(rom->path, O_RDONLY | O_BINARY); > - =C2=A0 =C2=A0if (fd =3D=3D -1) { > - =C2=A0 =C2=A0 =C2=A0 =C2=A0fprintf(stderr, "Could not open option rom '= %s': %s\n", > - =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0rom->path, strer= ror(errno)); > - =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err; > - =C2=A0 =C2=A0} > - > =C2=A0 =C2=A0 if (fw_dir) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 rom->fw_dir =C2=A0=3D g_strdup(fw_dir); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 rom->fw_file =3D g_strdup(file); > =C2=A0 =C2=A0 } > =C2=A0 =C2=A0 rom->addr =C2=A0 =C2=A0=3D addr; > - =C2=A0 =C2=A0rom->romsize =3D lseek(fd, 0, SEEK_END); > + =C2=A0 =C2=A0rom->romsize =3D size; > =C2=A0 =C2=A0 rom->data =C2=A0 =C2=A0=3D g_malloc0(rom->romsize); > - =C2=A0 =C2=A0lseek(fd, 0, SEEK_SET); > - =C2=A0 =C2=A0rc =3D read(fd, rom->data, rom->romsize); > - =C2=A0 =C2=A0if (rc !=3D rom->romsize) { > - =C2=A0 =C2=A0 =C2=A0 =C2=A0fprintf(stderr, "rom: file %-20s: read error= : rc=3D%d (expected %zd)\n", > - =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0rom->name, rc, r= om->romsize); > - =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err; > - =C2=A0 =C2=A0} > - =C2=A0 =C2=A0close(fd); > + > + =C2=A0 =C2=A0memcpy(rom->data, data, rom->romsize); This is not optimal, instead the data should be used directly. That way also mmap()ed, deduplicated ROM files are possible. > + > =C2=A0 =C2=A0 rom_insert(rom); > =C2=A0 =C2=A0 if (rom->fw_file && fw_cfg) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 const char *basename; > @@ -614,14 +602,51 @@ int rom_add_file(const char *file, const char *fw_d= ir, > > =C2=A0 =C2=A0 add_boot_device_path(bootindex, NULL, devpath); > =C2=A0 =C2=A0 return 0; > +} > + > +int rom_add_file(const char *file, const char *fw_dir, > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 target_phys_add= r_t addr, int32_t bootindex) > +{ > + =C2=A0 =C2=A0char *filename; > + =C2=A0 =C2=A0void *data =3D NULL; > + =C2=A0 =C2=A0size_t size; > + =C2=A0 =C2=A0int rc, fd =3D -1; > + > + =C2=A0 =C2=A0filename =3D qemu_find_file(QEMU_FILE_TYPE_BIOS, file); > + =C2=A0 =C2=A0if (filename =3D=3D NULL) { > + =C2=A0 =C2=A0 =C2=A0 =C2=A0filename =3D g_strdup(file); > + =C2=A0 =C2=A0} > + > + =C2=A0 =C2=A0fd =3D open(filename, O_RDONLY | O_BINARY); > + =C2=A0 =C2=A0if (fd =3D=3D -1) { > + =C2=A0 =C2=A0 =C2=A0 =C2=A0fprintf(stderr, "Could not open option rom '= %s': %s\n", > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0filename, strerr= or(errno)); > + =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err; > + =C2=A0 =C2=A0} > + > + =C2=A0 =C2=A0size =3D lseek(fd, 0, SEEK_END); > + =C2=A0 =C2=A0data =3D g_malloc0(size); > + =C2=A0 =C2=A0lseek(fd, 0, SEEK_SET); > + =C2=A0 =C2=A0rc =3D read(fd, data, size); It should be easy to replace this with mmap(), maybe later. > + =C2=A0 =C2=A0if (rc !=3D size) { > + =C2=A0 =C2=A0 =C2=A0 =C2=A0fprintf(stderr, "rom: file %-20s: read error= : rc=3D%d (expected %zd)\n", > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0filename, rc, si= ze); > + =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err; > + =C2=A0 =C2=A0} > + =C2=A0 =C2=A0close(fd); > + > + =C2=A0 =C2=A0rc =3D rom_add_file_buf(filename, data, size, fw_dir, addr= , bootindex); > + =C2=A0 =C2=A0if (rc !=3D 0) { > + =C2=A0 =C2=A0 =C2=A0 =C2=A0goto err; > + =C2=A0 =C2=A0} > + > + =C2=A0 =C2=A0g_free(data); > + =C2=A0 =C2=A0return 0; > > =C2=A0err: > =C2=A0 =C2=A0 if (fd !=3D -1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 close(fd); > - =C2=A0 =C2=A0g_free(rom->data); > - =C2=A0 =C2=A0g_free(rom->path); > - =C2=A0 =C2=A0g_free(rom->name); > - =C2=A0 =C2=A0g_free(rom); > + =C2=A0 =C2=A0g_free(data); > =C2=A0 =C2=A0 return -1; > =C2=A0} > > diff --git a/hw/loader.h b/hw/loader.h > index fc6bdff..9efe64a 100644 > --- a/hw/loader.h > +++ b/hw/loader.h > @@ -21,6 +21,9 @@ void pstrcpy_targphys(const char *name, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 const char *source); > > > +int rom_add_file_buf(const char *file, const void *data, size_t size, > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 c= onst char *fw_dir, > + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 t= arget_phys_addr_t addr, int32_t bootindex); > =C2=A0int rom_add_file(const char *file, const char *fw_dir, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0target_phys= _addr_t addr, int32_t bootindex); > =C2=A0int rom_add_blob(const char *name, const void *blob, size_t len, > @@ -31,6 +34,8 @@ int rom_copy(uint8_t *dest, target_phys_addr_t addr, si= ze_t size); > =C2=A0void *rom_ptr(target_phys_addr_t addr); > =C2=A0void do_info_roms(Monitor *mon); > > +#define rom_add_file_buf_fixed(_f, _d, _s, _a, _i) =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0\ > + =C2=A0 =C2=A0rom_add_file_buf(_f, _d, _s, NULL, _a, _i) > =C2=A0#define rom_add_file_fixed(_f, _a, _i) =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0\ > =C2=A0 =C2=A0 rom_add_file(_f, NULL, _a, _i) > =C2=A0#define rom_add_blob_fixed(_f, _b, _l, _a) =C2=A0 =C2=A0 =C2=A0\ > -- > 1.7.1 > > >