]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-firmware.c
9cb42946332e96f74dc3dd263243d1d80b73ed71
[thirdparty/systemd.git] / src / udev / 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@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 log_error("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 log_error("No memory available to load firmware file");
53 return false;
54 }
55
56 log_debug("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 = NULL;
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 log_error("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 < ELEMENTSOF(searchpath); i++) {
103 strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
104 fwfile = fopen(fwpath, "re");
105 if (fwfile != NULL)
106 break;
107
108 strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
109 fwfile = fopen(fwpath, "re");
110 if (fwfile != NULL)
111 break;
112 }
113
114 util_path_encode(firmware, fwencpath, sizeof(fwencpath));
115 strscpyl(misspath, sizeof(misspath), "/run/udev/firmware-missing/", fwencpath, NULL);
116 strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);
117
118 if (fwfile == NULL) {
119 int err;
120
121 /* This link indicates the missing firmware file and the associated device */
122 log_debug("did not find firmware file '%s'\n", firmware);
123 do {
124 err = mkdir_parents(misspath, 0755);
125 if (err != 0 && err != -ENOENT)
126 break;
127 err = symlink(udev_device_get_devpath(dev), misspath);
128 if (err != 0)
129 err = -errno;
130 } while (err == -ENOENT);
131 rc = EXIT_FAILURE;
132 /*
133 * Do not cancel the request in the initrd, the real root might have
134 * the firmware file and the 'coldplug' run in the real root will find
135 * this pending request and fulfill or cancel it.
136 * */
137 if (!in_initrd())
138 set_loading(udev, loadpath, "-1");
139 goto exit;
140 }
141
142 if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
143 if (!in_initrd())
144 set_loading(udev, loadpath, "-1");
145 rc = EXIT_FAILURE;
146 goto exit;
147 }
148
149 if (unlink(misspath) == 0)
150 util_delete_path(udev, misspath);
151
152 if (!set_loading(udev, loadpath, "1"))
153 goto exit;
154
155 strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL);
156 if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
157 log_error("error sending firmware '%s' to device\n", firmware);
158 set_loading(udev, loadpath, "-1");
159 rc = EXIT_FAILURE;
160 goto exit;
161 };
162
163 set_loading(udev, loadpath, "0");
164 exit:
165 if (fwfile)
166 fclose(fwfile);
167 return rc;
168 }
169
170 const struct udev_builtin udev_builtin_firmware = {
171 .name = "firmware",
172 .cmd = builtin_firmware,
173 .help = "kernel firmware loader",
174 .run_once = true,
175 };