chiark / gitweb /
extras: cdrom_id - create only /dev/cdrom
[elogind.git] / src / udev-builtin-firmware.c
1 /*
2  * firmware - Kernel firmware loader
3  *
4  * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
5  * Copyright (C) 2009-2011 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
28 #include "udev.h"
29
30 static bool set_loading(struct udev *udev, char *loadpath, const char *state)
31 {
32         FILE *ldfile;
33
34         ldfile = fopen(loadpath, "we");
35         if (ldfile == NULL) {
36                 err(udev, "error: can not open '%s'\n", loadpath);
37                 return false;
38         };
39         fprintf(ldfile, "%s\n", state);
40         fclose(ldfile);
41         return true;
42 }
43
44 static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
45 {
46         char *buf;
47         FILE *fsource = NULL, *ftarget = NULL;
48         bool ret = false;
49
50         buf = malloc(size);
51         if (buf == NULL) {
52                 err(udev,"No memory available to load firmware file");
53                 return false;
54         }
55
56         info(udev, "writing '%s' (%zi) to '%s'\n", source, size, target);
57
58         fsource = fopen(source, "re");
59         if (fsource == NULL)
60                 goto exit;
61         ftarget = fopen(target, "we");
62         if (ftarget == NULL)
63                 goto exit;
64         if (fread(buf, size, 1, fsource) != 1)
65                 goto exit;
66         if (fwrite(buf, size, 1, ftarget) == 1)
67                 ret = true;
68 exit:
69         if (ftarget != NULL)
70                 fclose(ftarget);
71         if (fsource != NULL)
72                 fclose(fsource);
73         free(buf);
74         return ret;
75 }
76
77 static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test)
78 {
79         struct udev *udev = udev_device_get_udev(dev);
80         static const char *searchpath[] = { FIRMWARE_PATH };
81         char fwencpath[UTIL_PATH_SIZE];
82         char misspath[UTIL_PATH_SIZE];
83         char loadpath[UTIL_PATH_SIZE];
84         char datapath[UTIL_PATH_SIZE];
85         char fwpath[UTIL_PATH_SIZE];
86         const char *firmware;
87         FILE *fwfile;
88         struct utsname kernel;
89         struct stat statbuf;
90         unsigned int i;
91         int rc = EXIT_SUCCESS;
92
93         firmware = udev_device_get_property_value(dev, "FIRMWARE");
94         if (firmware == NULL) {
95                 err(udev, "firmware parameter missing\n\n");
96                 rc = EXIT_FAILURE;
97                 goto exit;
98         }
99
100         /* lookup firmware file */
101         uname(&kernel);
102         for (i = 0; i < ARRAY_SIZE(searchpath); i++) {
103                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
104                 dbg(udev, "trying %s\n", fwpath);
105                 fwfile = fopen(fwpath, "re");
106                 if (fwfile != NULL)
107                         break;
108
109                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
110                 dbg(udev, "trying %s\n", fwpath);
111                 fwfile = fopen(fwpath, "re");
112                 if (fwfile != NULL)
113                         break;
114         }
115
116         util_path_encode(firmware, fwencpath, sizeof(fwencpath));
117         util_strscpyl(misspath, sizeof(misspath), udev_get_run_path(udev), "/firmware-missing/", fwencpath, NULL);
118         util_strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);
119
120         if (fwfile == NULL) {
121                 int err;
122
123                 /* This link indicates the missing firmware file and the associated device */
124                 info(udev, "did not find firmware file '%s'\n", firmware);
125                 do {
126                         err = util_create_path(udev, misspath);
127                         if (err != 0 && err != -ENOENT)
128                                 break;
129                         err = symlink(udev_device_get_devpath(dev), misspath);
130                         if (err != 0)
131                                 err = -errno;
132                 } while (err == -ENOENT);
133                 rc = EXIT_FAILURE;
134                 set_loading(udev, loadpath, "-1");
135                 goto exit;
136         }
137
138         if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
139                 rc = EXIT_FAILURE;
140                 goto exit;
141         }
142         if (unlink(misspath) == 0)
143                 util_delete_path(udev, misspath);
144
145         if (!set_loading(udev, loadpath, "1"))
146                 goto exit;
147
148         util_strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL);
149         if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
150                 err(udev, "error sending firmware '%s' to device\n", firmware);
151                 set_loading(udev, loadpath, "-1");
152                 rc = EXIT_FAILURE;
153                 goto exit;
154         };
155
156         set_loading(udev, loadpath, "0");
157 exit:
158         if (fwfile)
159                 fclose(fwfile);
160         return rc;
161 }
162
163 const struct udev_builtin udev_builtin_firmware = {
164         .name = "firmware",
165         .cmd = builtin_firmware,
166         .help = "kernel firmware loader",
167         .run_once = true,
168 };