Skip to content

Commit 45e8f05

Browse files
author
Nicholas Hunn
committed
misc: changed file loading so that all files are searched for in the include directory
1 parent ec8ac53 commit 45e8f05

11 files changed

Lines changed: 80 additions & 32 deletions

File tree

ks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ int main(int argc, char **argv)
103103
return 1;
104104
}
105105
filename = &optarg[colon - optarg + 1];
106-
ret = load_sahara_image(filename, &mappings[file_id]);
106+
ret = load_sahara_image(filename, &mappings[file_id], NULL);
107107
if (ret < 0)
108108
return 1;
109109

patch.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616
static struct list_head patches = LIST_INIT(patches);
1717
static bool patches_loaded;
1818

19-
int patch_load(const char *patch_file)
19+
int patch_load(const char *patch_file, const char *incdir)
2020
{
2121
struct patch *patch;
2222
xmlNode *node;
2323
xmlNode *root;
2424
xmlDoc *doc;
2525
int errors;
2626

27-
doc = xmlReadFile(patch_file, NULL, 0);
27+
char *patch_file_resolved = strdup(patch_file);
28+
29+
resolve_path(&patch_file_resolved, incdir);
30+
doc = xmlReadFile(patch_file_resolved, NULL, 0);
2831
if (!doc) {
2932
ux_err("failed to parse patch-type file \"%s\"\n", patch_file);
33+
free(patch_file_resolved);
3034
return -EINVAL;
3135
}
36+
free(patch_file_resolved);
3237

3338
root = xmlDocGetRootElement(doc);
3439
for (node = root->children; node ; node = node->next) {

patch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct patch {
1919
struct list_head node;
2020
};
2121

22-
int patch_load(const char *patch_file);
22+
int patch_load(const char *patch_file, const char *incdir);
2323
int patch_execute(struct qdl_device *qdl, int (*apply)(struct qdl_device *qdl, struct patch *patch));
2424
void free_patches(void);
2525

program.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,7 @@ static int load_program_tag(xmlNode *node, bool is_nand, bool allow_missing, con
185185
}
186186

187187
if (program->filename) {
188-
if (incdir) {
189-
snprintf(tmp, PATH_MAX, "%s/%s", incdir, program->filename);
190-
if (access(tmp, F_OK) != -1) {
191-
free((void *)program->filename);
192-
program->filename = strdup(tmp);
193-
}
194-
}
188+
resolve_path(&program->filename, incdir);
195189

196190
fd = open(program->filename, O_RDONLY | O_BINARY);
197191
if (fd < 0) {
@@ -234,12 +228,17 @@ int program_load(const char *program_file, bool is_nand, bool allow_missing, con
234228
xmlNode *root;
235229
xmlDoc *doc;
236230
int errors = 0;
231+
char *program_file_resolved = strdup(program_file);
232+
233+
resolve_path(&program_file_resolved, incdir);
237234

238-
doc = xmlReadFile(program_file, NULL, 0);
235+
doc = xmlReadFile(program_file_resolved, NULL, 0);
239236
if (!doc) {
240237
ux_err("failed to parse program-type file \"%s\"\n", program_file);
238+
free(program_file_resolved);
241239
return -EINVAL;
242240
}
241+
free(program_file_resolved);
243242

244243
root = xmlDocGetRootElement(doc);
245244
for (node = root->children; node ; node = node->next) {

program.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct program {
1111
unsigned int pages_per_block;
1212
unsigned int sector_size;
1313
unsigned int file_offset;
14-
const char *filename;
14+
char *filename;
1515
const char *label;
1616
unsigned int num_sectors;
1717
int partition;

qdl.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum {
4343

4444
bool qdl_debug;
4545

46-
static int detect_type(const char *verb)
46+
static int detect_type(const char *verb, const char *incdir)
4747
{
4848
xmlNode *root;
4949
xmlDoc *doc;
@@ -57,16 +57,22 @@ static int detect_type(const char *verb)
5757
if (!strcmp(verb, "erase"))
5858
return QDL_CMD_ERASE;
5959

60-
if (access(verb, F_OK)) {
60+
char *filename = strdup(verb);
61+
resolve_path(&filename, incdir);
62+
63+
if (access(filename, F_OK)) {
6164
ux_err("%s is not a verb and not a XML file\n", verb);
65+
free(filename);
6266
return -EINVAL;
6367
}
6468

65-
doc = xmlReadFile(verb, NULL, 0);
69+
doc = xmlReadFile(filename, NULL, 0);
6670
if (!doc) {
6771
ux_err("failed to parse XML file \"%s\"\n", verb);
72+
free(filename);
6873
return -EINVAL;
6974
}
75+
free(filename);
7076

7177
root = xmlDocGetRootElement(doc);
7278
if (!xmlStrcmp(root->name, (xmlChar *)"patches")) {
@@ -251,6 +257,7 @@ static int decode_programmer_archive(struct sahara_image *blob, struct sahara_im
251257
* decode_sahara_config() - Attempt to decode a Sahara config XML document
252258
* @blob: Loaded image to be decoded as Sahara config
253259
* @images: List of Sahara images, with @images[0] populated
260+
* @incdir: include directory to be searched for the files specified in the Sahara config
254261
*
255262
* The single blob provided in @images[0] might be a XML blob containing
256263
* a sahara_config document with definitions of the various Sahara images that
@@ -261,7 +268,7 @@ static int decode_programmer_archive(struct sahara_image *blob, struct sahara_im
261268
*
262269
* Returns: 0 if no archive was found, 1 if archive was decoded, -1 on error
263270
*/
264-
static int decode_sahara_config(struct sahara_image *blob, struct sahara_image *images)
271+
static int decode_sahara_config(struct sahara_image *blob, struct sahara_image *images, const char *incdir)
265272
{
266273
char image_path_full[PATH_MAX];
267274
const char *image_path;
@@ -342,7 +349,7 @@ static int decode_sahara_config(struct sahara_image *blob, struct sahara_image *
342349

343350
free((void *)image_path);
344351

345-
ret = load_sahara_image(image_path_full, &images[image_id]);
352+
ret = load_sahara_image(image_path_full, &images[image_id], incdir);
346353
if (ret < 0)
347354
goto err_free_doc;
348355
}
@@ -370,6 +377,7 @@ static int decode_sahara_config(struct sahara_image *blob, struct sahara_image *
370377
* decode_programmer() - decodes the programmer specifier
371378
* @s: programmer specifier, from the user
372379
* @images: array of images to populate
380+
* @incdir: include directory to be searched for the files specified in @s
373381
*
374382
* This parses the programmer specifier @s, which can either be a single
375383
* filename, or a comma-separated series of <id>:<filename> entries.
@@ -386,7 +394,7 @@ static int decode_sahara_config(struct sahara_image *blob, struct sahara_image *
386394
*
387395
* Returns: 0 on success, -1 otherwise.
388396
*/
389-
static int decode_programmer(char *s, struct sahara_image *images)
397+
static int decode_programmer(char *s, struct sahara_image *images, const char *incdir)
390398
{
391399
struct sahara_image archive;
392400
char *filename;
@@ -411,20 +419,20 @@ static int decode_programmer(char *s, struct sahara_image *images)
411419
}
412420

413421
filename = &tail[1];
414-
ret = load_sahara_image(filename, &images[id]);
422+
ret = load_sahara_image(filename, &images[id], incdir);
415423
if (ret < 0)
416424
return -1;
417425
}
418426
} else {
419-
ret = load_sahara_image(s, &archive);
427+
ret = load_sahara_image(s, &archive, incdir);
420428
if (ret < 0)
421429
return -1;
422430

423431
ret = decode_programmer_archive(&archive, images);
424432
if (ret < 0 || ret == 1)
425433
return ret;
426434

427-
ret = decode_sahara_config(&archive, images);
435+
ret = decode_sahara_config(&archive, images, incdir);
428436
if (ret < 0 || ret == 1)
429437
return ret;
430438

@@ -695,18 +703,18 @@ static int qdl_flash(int argc, char **argv)
695703
if (qdl_debug)
696704
print_version();
697705

698-
ret = decode_programmer(argv[optind++], sahara_images);
706+
ret = decode_programmer(argv[optind++], sahara_images, incdir);
699707
if (ret < 0)
700708
goto out_cleanup;
701709

702710
do {
703-
type = detect_type(argv[optind]);
711+
type = detect_type(argv[optind], incdir);
704712
if (type < 0 || type == QDL_FILE_UNKNOWN)
705713
errx(1, "failed to detect file type of %s\n", argv[optind]);
706714

707715
switch (type) {
708716
case QDL_FILE_PATCH:
709-
ret = patch_load(argv[optind]);
717+
ret = patch_load(argv[optind], incdir);
710718
if (ret < 0)
711719
errx(1, "patch_load %s failed", argv[optind]);
712720
break;
@@ -728,7 +736,7 @@ static int qdl_flash(int argc, char **argv)
728736
if (storage_type != QDL_STORAGE_UFS)
729737
errx(1, "attempting to load provisioning config when storage isn't \"ufs\"");
730738

731-
ret = ufs_load(argv[optind], qdl_finalize_provisioning);
739+
ret = ufs_load(argv[optind], qdl_finalize_provisioning, incdir);
732740
if (ret < 0)
733741
errx(1, "ufs_load %s failed", argv[optind]);
734742
break;

qdl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,13 @@ int firehose_read_buf(struct qdl_device *qdl, struct read_op *read_op, void *out
112112
int sahara_run(struct qdl_device *qdl, const struct sahara_image *images,
113113
const char *ramdump_path,
114114
const char *ramdump_filter);
115-
int load_sahara_image(const char *filename, struct sahara_image *image);
115+
int load_sahara_image(const char *filename, struct sahara_image *image, const char *incdir);
116116
void sahara_images_free(struct sahara_image *images, size_t count);
117117
void print_hex_dump(const char *prefix, const void *buf, size_t len);
118118
unsigned int attr_as_unsigned(xmlNode *node, const char *attr, int *errors);
119119
const char *attr_as_string(xmlNode *node, const char *attr, int *errors);
120120
bool attr_as_bool(xmlNode *node, const char *attr, int *errors);
121+
void resolve_path(char **filename, const char *incdir);
121122

122123
void ux_init(void);
123124
void ux_err(const char *fmt, ...);

read.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ int read_op_load(const char *read_op_file, const char *incdir)
2727
xmlDoc *doc;
2828
int errors;
2929
char tmp[PATH_MAX];
30+
char *read_op_file_resolved = strdup(read_op_file);
3031

31-
doc = xmlReadFile(read_op_file, NULL, 0);
32+
resolve_path(&read_op_file_resolved, incdir);
33+
34+
doc = xmlReadFile(read_op_file_resolved, NULL, 0);
3235
if (!doc) {
3336
ux_err("failed to parse read-type file \"%s\"\n", read_op_file);
37+
free(read_op_file_resolved);
3438
return -EINVAL;
3539
}
40+
free(read_op_file_resolved);
3641

3742
root = xmlDocGetRootElement(doc);
3843
for (node = root->children; node ; node = node->next) {

ufs.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct ufs_epilogue *ufs_parse_epilogue(xmlNode *node)
116116
return result;
117117
}
118118

119-
int ufs_load(const char *ufs_file, bool finalize_provisioning)
119+
int ufs_load(const char *ufs_file, bool finalize_provisioning, const char *incdir)
120120
{
121121
xmlNode *node;
122122
xmlNode *root;
@@ -130,12 +130,17 @@ int ufs_load(const char *ufs_file, bool finalize_provisioning)
130130
ufs_file);
131131
return -EEXIST;
132132
}
133+
char *ufs_file_resolved = strdup(ufs_file);
134+
135+
resolve_path(&ufs_file_resolved, incdir);
133136

134137
doc = xmlReadFile(ufs_file, NULL, 0);
135138
if (!doc) {
136139
ux_err("failed to parse ufs-type file \"%s\"\n", ufs_file);
140+
free(ufs_file_resolved);
137141
return -EINVAL;
138142
}
143+
free(ufs_file_resolved);
139144

140145
root = xmlDocGetRootElement(doc);
141146

ufs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct ufs_epilogue {
4747
bool commit;
4848
};
4949

50-
int ufs_load(const char *ufs_file, bool finalize_provisioning);
50+
int ufs_load(const char *ufs_file, bool finalize_provisioning, const char *incdir);
5151
int ufs_provisioning_execute(struct qdl_device *qdl,
5252
int (*apply_ufs_common)(struct qdl_device *qdl, struct ufs_common *ufs),
5353
int (*apply_ufs_body)(struct qdl_device *qdl, struct ufs_body *ufs),

0 commit comments

Comments
 (0)