2 * firmware - Kernel firmware loader
4 * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
5 * Copyright (C) 2009-2011 Kay Sievers <kay.sievers@vrfy.org>
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.
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:*
25 #include <sys/utsname.h>
30 static bool set_loading(struct udev *udev, char *loadpath, const char *state)
34 ldfile = fopen(loadpath, "we");
36 log_error("error: can not open '%s'\n", loadpath);
39 fprintf(ldfile, "%s\n", state);
44 static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
47 FILE *fsource = NULL, *ftarget = NULL;
52 log_error("No memory available to load firmware file");
56 log_debug("writing '%s' (%zi) to '%s'\n", source, size, target);
58 fsource = fopen(source, "re");
61 ftarget = fopen(target, "we");
64 if (fread(buf, size, 1, fsource) != 1)
66 if (fwrite(buf, size, 1, ftarget) == 1)
77 static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test)
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];
88 struct utsname kernel;
91 int rc = EXIT_SUCCESS;
93 firmware = udev_device_get_property_value(dev, "FIRMWARE");
94 if (firmware == NULL) {
95 log_error("firmware parameter missing\n\n");
100 /* lookup firmware file */
102 for (i = 0; i < ELEMENTSOF(searchpath); i++) {
103 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
104 fwfile = fopen(fwpath, "re");
108 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
109 fwfile = fopen(fwpath, "re");
114 util_path_encode(firmware, fwencpath, sizeof(fwencpath));
115 util_strscpyl(misspath, sizeof(misspath), "/run/udev/firmware-missing/", fwencpath, NULL);
116 util_strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);
118 if (fwfile == NULL) {
121 /* This link indicates the missing firmware file and the associated device */
122 log_debug("did not find firmware file '%s'\n", firmware);
124 err = mkdir_parents(misspath, 0755);
125 if (err != 0 && err != -ENOENT)
127 err = symlink(udev_device_get_devpath(dev), misspath);
130 } while (err == -ENOENT);
132 set_loading(udev, loadpath, "-1");
136 if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
140 if (unlink(misspath) == 0)
141 util_delete_path(udev, misspath);
143 if (!set_loading(udev, loadpath, "1"))
146 util_strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL);
147 if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
148 log_error("error sending firmware '%s' to device\n", firmware);
149 set_loading(udev, loadpath, "-1");
154 set_loading(udev, loadpath, "0");
161 const struct udev_builtin udev_builtin_firmware = {
163 .cmd = builtin_firmware,
164 .help = "kernel firmware loader",