carl9170 firmware: import 1.7.0
[carl9170fw.git] / tools / src / checksum.c
1 /*
2  * Copyright 2010, Christian Lamparter <chunkeey@googlemail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <error.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "carlfw.h"
28
29 #include "compiler.h"
30
31 static void checksum_help(void)
32 {
33         fprintf(stderr, "Usage:\n");
34         fprintf(stderr, "\tchecksum FW-FILE\n");
35
36         fprintf(stderr, "\nDescription:\n");
37         fprintf(stderr, "\tThis simple utility adds/updates various "
38                         "checksums.\n");
39
40         fprintf(stderr, "\nParameteres:\n");
41         fprintf(stderr, "\t 'FW-FILE'   = firmware name\n");
42         fprintf(stderr, "\n");
43 }
44
45 int main(int argc, char *args[])
46 {
47         struct carlfw *fw = NULL;
48         int err = 0;
49
50         if (argc != 2) {
51                 err = -EINVAL;
52                 goto out;
53         }
54
55         fw = carlfw_load(args[1]);
56         if (IS_ERR_OR_NULL(fw)) {
57                 fprintf(stderr, "Failed to open file \"%s\" (%d).\n",
58                         args[1], (int) PTR_ERR(fw));
59                 goto out;
60         }
61
62         /*
63          * No magic here, The checksum descriptor is added/update
64          * automatically in a subroutine of carlfw_store().
65          *
66          * This tools serves as a skeleton/example.
67          */
68         err = carlfw_store(fw);
69         if (err) {
70                 fprintf(stderr, "Failed to apply checksum (%d).\n", err);
71                 goto out;
72         }
73
74 out:
75         switch (err) {
76         case 0:
77                 fprintf(stdout, "checksum applied.\n");
78                 break;
79         case -EINVAL:
80                 checksum_help();
81                 break;
82         default:
83                 break;
84         }
85
86         carlfw_release(fw);
87         return err ? EXIT_FAILURE : EXIT_SUCCESS;
88 }