chiark / gitweb /
selinux: firmware - do not label files in runtime dir
[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         if (ftarget != NULL)
67                 fclose(ftarget);
68         if (fsource != NULL)
69                 fclose(fsource);
70         free(buf);
71         return ret;
72 }
73
74 int main(int argc, char **argv)
75 {
76         static const struct option options[] = {
77                 { "firmware", required_argument, NULL, 'f' },
78                 { "devpath", required_argument, NULL, 'p' },
79                 { "help", no_argument, NULL, 'h' },
80                 {}
81         };
82         static const char *searchpath[] = { FIRMWARE_PATH };
83         char fwencpath[UTIL_PATH_SIZE];
84         char misspath[UTIL_PATH_SIZE];
85         char loadpath[UTIL_PATH_SIZE];
86         char datapath[UTIL_PATH_SIZE];
87         char fwpath[UTIL_PATH_SIZE];
88         char *devpath = NULL;
89         char *firmware = NULL;
90         FILE *fwfile;
91         struct utsname kernel;
92         struct stat statbuf;
93         struct udev *udev = NULL;
94         unsigned int i;
95         int rc = 0;
96
97         udev_log_init("firmware");
98
99         for (;;) {
100                 int option;
101
102                 option = getopt_long(argc, argv, "f:p:h", options, NULL);
103                 if (option == -1)
104                         break;
105
106                 switch (option) {
107                 case 'f':
108                         firmware = optarg;
109                         break;
110                 case 'p':
111                         devpath = optarg;
112                         break;
113                 case 'h':
114                         printf("Usage: firmware --firmware=<fwfile> --devpath=<path> [--help]\n\n");
115                 default:
116                         rc = 1;
117                         goto exit;
118                 }
119         }
120
121         if (devpath == NULL || firmware == NULL) {
122                 fprintf(stderr, "firmware or devpath parameter missing\n\n");
123                 rc = 1;
124                 goto exit;
125         }
126
127         udev = udev_new();
128         if (udev == NULL) {
129                 rc = 1;
130                 goto exit;
131         };
132
133         /* lookup firmware file */
134         uname(&kernel);
135         for (i = 0; i < ARRAY_SIZE(searchpath); i++) {
136                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
137                 dbg(udev, "trying %s\n", fwpath);
138                 fwfile = fopen(fwpath, "r");
139                 if (fwfile != NULL)
140                         break; 
141
142                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
143                 dbg(udev, "trying %s\n", fwpath);
144                 fwfile = fopen(fwpath, "r");
145                 if (fwfile != NULL)
146                         break;
147         }
148
149         util_path_encode(firmware, fwencpath, sizeof(fwencpath));
150         util_strscpyl(misspath, sizeof(misspath), udev_get_run_path(udev), "/firmware-missing/", fwencpath, NULL);
151         util_strscpyl(loadpath, sizeof(loadpath), udev_get_sys_path(udev), devpath, "/loading", 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                         err = symlink(devpath, misspath);
163                         if (err != 0)
164                                 err = -errno;
165                 } while (err == -ENOENT);
166                 rc = 2;
167                 set_loading(udev, loadpath, "-1");
168                 goto exit;
169         }
170
171         if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
172                 rc = 3;
173                 goto exit;
174         }
175         if (unlink(misspath) == 0)
176                 util_delete_path(udev, misspath);
177
178         if (!set_loading(udev, loadpath, "1"))
179                 goto exit;
180
181         util_strscpyl(datapath, sizeof(datapath), udev_get_sys_path(udev), devpath, "/data", NULL);
182         if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
183                 err(udev, "error sending firmware '%s' to device\n", firmware);
184                 set_loading(udev, loadpath, "-1");
185                 rc = 4;
186                 goto exit;
187         };
188
189         set_loading(udev, loadpath, "0");
190 exit:
191         udev_unref(udev);
192         udev_log_close();
193         return rc;
194 }