1 /*
2  * Copyright 2014-2017 Frank Hunleth
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fwup_list.h"
18 #include <confuse.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include "cfgfile.h"
23 #include "util.h"
24 #include "simple_string.h"
25 
strsort(const void * a,const void * b)26 static int strsort(const void *a, const void *b)
27 {
28     return strcmp(*(const char **) a, *(const char **) b);
29 }
30 
list_tasks(cfg_t * cfg)31 static int list_tasks(cfg_t *cfg)
32 {
33     size_t i;
34     cfg_opt_t *opt = cfg_getopt(cfg, "task");
35     if (!opt)
36         ERR_RETURN("Firmware file missing task section");
37 
38     const char *tasks[opt->nvalues];
39     for (i = 0; i < opt->nvalues; i++) {
40         cfg_t *sec = cfg_opt_getnsec(opt, i);
41         tasks[i] = cfg_title(sec);
42     }
43     qsort(tasks, opt->nvalues, sizeof(const char *), strsort);
44 
45     struct simple_string s;
46     simple_string_init(&s);
47     for (i = 0; i < opt->nvalues; i++)
48         ssprintf(&s, "%s\n", tasks[i]);
49     fwup_output(FRAMING_TYPE_SUCCESS, 0, s.str);
50     free(s.str);
51 
52     return 0;
53 }
54 
55 /**
56  * @brief List out tasks in a firmware update file
57  * @param fw_filename the firmware update filename
58  * @param public_key an option public key if checking signatures
59  * @return 0 if successful
60  */
fwup_list(const char * fw_filename,unsigned char * const * public_keys)61 int fwup_list(const char *fw_filename, unsigned char * const *public_keys)
62 {
63     cfg_t *cfg = NULL;
64     int rc = 0;
65 
66     OK_OR_CLEANUP(cfgfile_parse_fw_meta_conf(fw_filename, &cfg, public_keys));
67 
68     OK_OR_CLEANUP(list_tasks(cfg));
69 
70 cleanup:
71     if (cfg)
72         cfgfile_free(cfg);
73     return rc;
74 }
75