chiark / gitweb /
firmware: convert shell script to C
[elogind.git] / extras / firmware / firmware.c
1 /*
2  * firmware - Load firmware device
3  *
4  * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
5  * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details:*
16  */
17
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <sys/utsname.h>
26 #include <sys/stat.h>
27 #include "libudev-private.h"
28
29 static bool set_loading(struct udev *udev, char *loadpath, const char *state)
30 {
31         FILE *ldfile;
32
33         ldfile = fopen(loadpath, "w");
34         if (ldfile == NULL) {
35                 err(udev, "error: can not open '%s'\n", loadpath);
36                 return false;
37         };
38         fprintf(ldfile, "%s\n", state);
39         fclose(ldfile);
40         return true;
41 }
42
43 static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
44 {
45         char *buf;
46         FILE *fsource, *ftarget;
47         bool ret = false;
48
49         buf = malloc(size);
50         if (buf == NULL) {
51                 err(udev,"No memory available to load firmware file");
52                 return false;
53         }
54
55         fsource = fopen(source, "r");
56         if (fsource == NULL)
57                 goto exit;
58         ftarget = fopen(target, "w");
59         if (ftarget == NULL)
60                 goto exit;
61         if (fread(buf, size, 1, fsource) != 1)
62                 goto exit;
63         if (fwrite(buf, size, 1, ftarget) == 1)
64                 ret = true;
65 exit:
66         fclose(ftarget);
67         fclose(fsource);
68         free(buf);
69         return ret;
70 }
71
72 int main(int argc, char **argv)
73 {
74         static const struct option options[] = {
75                 { "firmware", required_argument, NULL, 'f' },
76                 { "devpath", required_argument, NULL, 'p' },
77                 { "help", no_argument, NULL, 'h' },
78                 {}
79         };
80         static const char *searchpath[] = {
81                 "/lib/firmware/updates/",
82                 "/lib/firmware/"
83         };
84         char fwencpath[UTIL_PATH_SIZE];
85         char misspath[UTIL_PATH_SIZE];
86         char loadpath[UTIL_PATH_SIZE];
87         char datapath[UTIL_PATH_SIZE];
88         char fwpath[UTIL_PATH_SIZE];
89         char *devpath = NULL;
90         char *firmware = NULL;
91         FILE *fwfile;
92         struct utsname kernel;
93         struct stat statbuf;
94         struct udev *udev = NULL;
95         unsigned int i;
96         int rc = 0;
97
98         udev_log_init("firmware");
99
100         for (;;) {
101                 int option;
102
103                 option = getopt_long(argc, argv, "f:p:h", options, NULL);
104                 if (option == -1)
105                         break;
106
107                 switch (option) {
108                 case 'f':
109                         firmware = optarg;
110                         break;
111                 case 'p':
112                         devpath = optarg;
113                         break;
114                 case 'h':
115                         printf("Usage: firmware --firmware=<fwfile> --devpath=<path> [--help]\n\n");
116                 default:
117                         rc = 1;
118                         goto exit;
119                 }
120         }
121
122         if (devpath == NULL || firmware == NULL) {
123                 fprintf(stderr, "firmware or devpath parameter missing\n\n");
124                 rc = 1;
125                 goto exit;
126         }
127
128         udev = udev_new();
129         if (udev == NULL) {
130                 rc = 1;
131                 goto exit;
132         };
133
134         /* lookup firmware file */
135         uname(&kernel);
136         for (i = 0; i < ARRAY_SIZE(searchpath); i++) {
137                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
138                 dbg(udev, "trying %s\n", fwpath);
139                 fwfile = fopen(fwpath, "r");
140                 if (fwfile != NULL)
141                         break; 
142
143                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
144                 dbg(udev, "trying %s\n", fwpath);
145                 fwfile = fopen(fwpath, "r");
146                 if (fwfile != NULL)
147                         break;
148         }
149
150         util_path_encode(firmware, fwencpath, sizeof(fwencpath));
151         util_strscpyl(misspath, sizeof(misspath), udev_get_dev_path(udev), "/.udev/firmware-missing/", fwencpath, NULL);
152
153         if (fwfile == NULL) {
154                 int err;
155
156                 /* This link indicates the missing firmware file and the associated device */
157                 info(udev, "did not find firmware file '%s'\n", firmware);
158                 do {
159                         err = util_create_path(udev, misspath);
160                         if (err != 0 && err != -ENOENT)
161                                 break;
162                         udev_selinux_setfscreatecon(udev, misspath, S_IFLNK);
163                         err = symlink(devpath, misspath);
164                         if (err != 0)
165                                 err = -errno;
166                         udev_selinux_resetfscreatecon(udev);
167                 } while (err == -ENOENT);
168                 rc = 2;
169                 goto exit;
170         }
171
172         if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
173                 rc = 3;
174                 goto exit;
175         }
176         if (unlink(misspath) == 0)
177                 util_delete_path(udev, misspath);
178
179         util_strscpyl(loadpath, sizeof(loadpath), udev_get_sys_path(udev), devpath, "/loading", NULL);
180         set_loading(udev, loadpath, "1");
181
182         util_strscpyl(datapath, sizeof(datapath), udev_get_sys_path(udev), devpath, "/data", NULL);
183         if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
184                 err(udev, "error sending firmware '%s' to device\n", firmware);
185                 set_loading(udev, loadpath, "-1");
186                 rc = 4;
187                 goto exit;
188         };
189
190         set_loading(udev, loadpath, "0");
191 exit:
192         udev_unref(udev);
193         udev_log_close();
194         return rc;
195 }