mirror of https://gitlab.com/qemu-project/dtc.git
dtc: Make dt_from_blob() open its own input file, like the other input formats
Currently, main() has a variable for the input file. It used to be that main() would open the input based on command line arguments before passing it to the dt_from_*() function. However, only dt_from_blob() uses this. dt_from_source() opens its own file, and dt_from_fs() interprets the argument as as a directory and does its own opendir() call. Furthermore, main() opened the file with dtc_open_file() but closed it with a direct call to fclose(). Therefore, to improve the interface consistency between the dt_from_*() functions, make dt_from_blob() open and close its own files like the other dt_from_*() functions. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
parent
c26015443a
commit
a742aade6a
16
dtc.c
16
dtc.c
|
@ -118,7 +118,6 @@ int main(int argc, char *argv[])
|
||||||
int force = 0, check = 0;
|
int force = 0, check = 0;
|
||||||
const char *arg;
|
const char *arg;
|
||||||
int opt;
|
int opt;
|
||||||
struct dtc_file *inf = NULL;
|
|
||||||
FILE *outf = NULL;
|
FILE *outf = NULL;
|
||||||
int outversion = DEFAULT_FDT_VERSION;
|
int outversion = DEFAULT_FDT_VERSION;
|
||||||
int boot_cpuid_phys = 0xfeedbeef;
|
int boot_cpuid_phys = 0xfeedbeef;
|
||||||
|
@ -186,19 +185,14 @@ int main(int argc, char *argv[])
|
||||||
fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
|
fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
|
||||||
inform, outform, arg);
|
inform, outform, arg);
|
||||||
|
|
||||||
if (streq(inform, "dts")) {
|
if (streq(inform, "dts"))
|
||||||
bi = dt_from_source(arg);
|
bi = dt_from_source(arg);
|
||||||
} else if (streq(inform, "fs")) {
|
else if (streq(inform, "fs"))
|
||||||
bi = dt_from_fs(arg);
|
bi = dt_from_fs(arg);
|
||||||
} else if(streq(inform, "dtb")) {
|
else if(streq(inform, "dtb"))
|
||||||
inf = dtc_open_file(arg, NULL);
|
bi = dt_from_blob(arg);
|
||||||
bi = dt_from_blob(inf->file);
|
else
|
||||||
} else {
|
|
||||||
die("Unknown input format \"%s\"\n", inform);
|
die("Unknown input format \"%s\"\n", inform);
|
||||||
}
|
|
||||||
|
|
||||||
if (inf && inf->file != stdin)
|
|
||||||
fclose(inf->file);
|
|
||||||
|
|
||||||
fill_fullpaths(bi->dt, "");
|
fill_fullpaths(bi->dt, "");
|
||||||
process_checks(force, bi);
|
process_checks(force, bi);
|
||||||
|
|
2
dtc.h
2
dtc.h
|
@ -248,7 +248,7 @@ void dt_to_blob(FILE *f, struct boot_info *bi, int version,
|
||||||
void dt_to_asm(FILE *f, struct boot_info *bi, int version,
|
void dt_to_asm(FILE *f, struct boot_info *bi, int version,
|
||||||
int boot_cpuid_phys);
|
int boot_cpuid_phys);
|
||||||
|
|
||||||
struct boot_info *dt_from_blob(FILE *f);
|
struct boot_info *dt_from_blob(const char *fname);
|
||||||
|
|
||||||
/* Tree source */
|
/* Tree source */
|
||||||
|
|
||||||
|
|
26
flattree.c
26
flattree.c
|
@ -19,6 +19,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "dtc.h"
|
#include "dtc.h"
|
||||||
|
#include "srcpos.h"
|
||||||
|
|
||||||
#define FTF_FULLPATH 0x1
|
#define FTF_FULLPATH 0x1
|
||||||
#define FTF_VARALIGN 0x2
|
#define FTF_VARALIGN 0x2
|
||||||
|
@ -780,8 +781,9 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct boot_info *dt_from_blob(FILE *f)
|
struct boot_info *dt_from_blob(const char *fname)
|
||||||
{
|
{
|
||||||
|
struct dtc_file *dtcf;
|
||||||
u32 magic, totalsize, version, size_dt;
|
u32 magic, totalsize, version, size_dt;
|
||||||
u32 off_dt, off_str, off_mem_rsvmap;
|
u32 off_dt, off_str, off_mem_rsvmap;
|
||||||
int rc;
|
int rc;
|
||||||
|
@ -796,12 +798,14 @@ struct boot_info *dt_from_blob(FILE *f)
|
||||||
u32 val;
|
u32 val;
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
|
|
||||||
rc = fread(&magic, sizeof(magic), 1, f);
|
dtcf = dtc_open_file(fname, NULL);
|
||||||
if (ferror(f))
|
|
||||||
|
rc = fread(&magic, sizeof(magic), 1, dtcf->file);
|
||||||
|
if (ferror(dtcf->file))
|
||||||
die("Error reading DT blob magic number: %s\n",
|
die("Error reading DT blob magic number: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
if (rc < 1) {
|
if (rc < 1) {
|
||||||
if (feof(f))
|
if (feof(dtcf->file))
|
||||||
die("EOF reading DT blob magic number\n");
|
die("EOF reading DT blob magic number\n");
|
||||||
else
|
else
|
||||||
die("Mysterious short read reading magic number\n");
|
die("Mysterious short read reading magic number\n");
|
||||||
|
@ -811,11 +815,11 @@ struct boot_info *dt_from_blob(FILE *f)
|
||||||
if (magic != FDT_MAGIC)
|
if (magic != FDT_MAGIC)
|
||||||
die("Blob has incorrect magic number\n");
|
die("Blob has incorrect magic number\n");
|
||||||
|
|
||||||
rc = fread(&totalsize, sizeof(totalsize), 1, f);
|
rc = fread(&totalsize, sizeof(totalsize), 1, dtcf->file);
|
||||||
if (ferror(f))
|
if (ferror(dtcf->file))
|
||||||
die("Error reading DT blob size: %s\n", strerror(errno));
|
die("Error reading DT blob size: %s\n", strerror(errno));
|
||||||
if (rc < 1) {
|
if (rc < 1) {
|
||||||
if (feof(f))
|
if (feof(dtcf->file))
|
||||||
die("EOF reading DT blob size\n");
|
die("EOF reading DT blob size\n");
|
||||||
else
|
else
|
||||||
die("Mysterious short read reading blob size\n");
|
die("Mysterious short read reading blob size\n");
|
||||||
|
@ -835,12 +839,12 @@ struct boot_info *dt_from_blob(FILE *f)
|
||||||
p = blob + sizeof(magic) + sizeof(totalsize);
|
p = blob + sizeof(magic) + sizeof(totalsize);
|
||||||
|
|
||||||
while (sizeleft) {
|
while (sizeleft) {
|
||||||
if (feof(f))
|
if (feof(dtcf->file))
|
||||||
die("EOF before reading %d bytes of DT blob\n",
|
die("EOF before reading %d bytes of DT blob\n",
|
||||||
totalsize);
|
totalsize);
|
||||||
|
|
||||||
rc = fread(p, 1, sizeleft, f);
|
rc = fread(p, 1, sizeleft, dtcf->file);
|
||||||
if (ferror(f))
|
if (ferror(dtcf->file))
|
||||||
die("Error reading DT blob: %s\n",
|
die("Error reading DT blob: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
|
||||||
|
@ -902,5 +906,7 @@ struct boot_info *dt_from_blob(FILE *f)
|
||||||
|
|
||||||
free(blob);
|
free(blob);
|
||||||
|
|
||||||
|
dtc_close_file(dtcf);
|
||||||
|
|
||||||
return build_boot_info(reservelist, tree);
|
return build_boot_info(reservelist, tree);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue