Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | From 9a3a4e16b36e762291cd1574ec71d7954f8b5363 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Mon, 14 Dec 2015 05:33:32 +0000 |
| 4 | Subject: [PATCH 17/36] Revert "udev: remove userspace firmware loading |
| 5 | support" |
| 6 | |
| 7 | This reverts commit be2ea723b1d023b3d385d3b791ee4607cbfb20ca. |
| 8 | Userspace firmware loading support is needed for Linux < 3.7. |
| 9 | |
| 10 | Upstream-Status: Inappropriate [OE specific] |
| 11 | |
| 12 | Signed-off-by: Jonathan Liu <net147@gmail.com> |
| 13 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 14 | --- |
| 15 | Makefile.am | 12 +++ |
| 16 | README | 6 +- |
| 17 | TODO | 1 + |
| 18 | configure.ac | 22 ++++++ |
| 19 | src/udev/udev-builtin-firmware.c | 154 +++++++++++++++++++++++++++++++++++++++ |
| 20 | src/udev/udev-builtin.c | 3 + |
| 21 | src/udev/udev.h | 6 ++ |
| 22 | src/udev/udevd.c | 13 ++++ |
| 23 | 8 files changed, 214 insertions(+), 3 deletions(-) |
| 24 | create mode 100644 src/udev/udev-builtin-firmware.c |
| 25 | |
| 26 | diff --git a/Makefile.am b/Makefile.am |
| 27 | index a99e8eb..b5c6ba7 100644 |
| 28 | --- a/Makefile.am |
| 29 | +++ b/Makefile.am |
| 30 | @@ -3573,6 +3573,18 @@ libudev_core_la_LIBADD = \ |
| 31 | $(BLKID_LIBS) \ |
| 32 | $(KMOD_LIBS) |
| 33 | |
| 34 | +libudev_core_la_CPPFLAGS = \ |
| 35 | + $(AM_CPPFLAGS) \ |
| 36 | + -DFIRMWARE_PATH="$(FIRMWARE_PATH)" |
| 37 | + |
| 38 | +if ENABLE_FIRMWARE |
| 39 | +libudev_core_la_SOURCES += \ |
| 40 | + src/udev/udev-builtin-firmware.c |
| 41 | + |
| 42 | +dist_udevrules_DATA += \ |
| 43 | + rules/50-firmware.rules |
| 44 | +endif |
| 45 | + |
| 46 | if HAVE_KMOD |
| 47 | libudev_core_la_SOURCES += \ |
| 48 | src/udev/udev-builtin-kmod.c |
| 49 | diff --git a/README b/README |
| 50 | index 41fb07a..10230ac 100644 |
| 51 | --- a/README |
| 52 | +++ b/README |
| 53 | @@ -50,14 +50,14 @@ REQUIREMENTS: |
| 54 | CONFIG_PROC_FS |
| 55 | CONFIG_FHANDLE (libudev, mount and bind mount handling) |
| 56 | |
| 57 | - udev will fail to work with the legacy sysfs layout: |
| 58 | + Udev will fail to work with the legacy layout: |
| 59 | CONFIG_SYSFS_DEPRECATED=n |
| 60 | |
| 61 | Legacy hotplug slows down the system and confuses udev: |
| 62 | CONFIG_UEVENT_HELPER_PATH="" |
| 63 | |
| 64 | - Userspace firmware loading is not supported and should |
| 65 | - be disabled in the kernel: |
| 66 | + Userspace firmware loading is deprecated, will go away, and |
| 67 | + sometimes causes problems: |
| 68 | CONFIG_FW_LOADER_USER_HELPER=n |
| 69 | |
| 70 | Some udev rules and virtualization detection relies on it: |
| 71 | diff --git a/TODO b/TODO |
| 72 | index 92cc8cc..25f6849 100644 |
| 73 | --- a/TODO |
| 74 | +++ b/TODO |
| 75 | @@ -628,6 +628,7 @@ Features: |
| 76 | * rename "userspace" to "core-os" |
| 77 | |
| 78 | * udev: |
| 79 | + - remove src/udev/udev-builtin-firmware.c (CONFIG_FW_LOADER_USER_HELPER=n) |
| 80 | - move to LGPL |
| 81 | - kill scsi_id |
| 82 | - add trigger --subsystem-match=usb/usb_device device |
| 83 | diff --git a/configure.ac b/configure.ac |
| 84 | index 16c83bb..36d20b5 100644 |
| 85 | --- a/configure.ac |
| 86 | +++ b/configure.ac |
| 87 | @@ -1353,6 +1353,25 @@ AC_ARG_ENABLE(hwdb, [AC_HELP_STRING([--disable-hwdb], [disable hardware database |
| 88 | AM_CONDITIONAL(ENABLE_HWDB, [test x$enable_hwdb = xyes]) |
| 89 | |
| 90 | # ------------------------------------------------------------------------------ |
| 91 | +AC_ARG_WITH(firmware-path, |
| 92 | + AS_HELP_STRING([--with-firmware-path=DIR[[[:DIR[...]]]]], |
| 93 | + [Firmware search path (default="")]), |
| 94 | + [], [with_firmware_path=""]) |
| 95 | +OLD_IFS=$IFS |
| 96 | +IFS=: |
| 97 | +for i in $with_firmware_path; do |
| 98 | + if test "x${FIRMWARE_PATH}" = "x"; then |
| 99 | + FIRMWARE_PATH="\\\"${i}/\\\"" |
| 100 | + else |
| 101 | + FIRMWARE_PATH="${FIRMWARE_PATH}, \\\"${i}/\\\"" |
| 102 | + fi |
| 103 | +done |
| 104 | +IFS=$OLD_IFS |
| 105 | +AC_SUBST(FIRMWARE_PATH) |
| 106 | +AS_IF([test "x${FIRMWARE_PATH}" != "x"], [ AC_DEFINE(HAVE_FIRMWARE, 1, [Define if FIRMWARE is available]) ]) |
| 107 | +AM_CONDITIONAL(ENABLE_FIRMWARE, [test "x${FIRMWARE_PATH}" != "x"]) |
| 108 | + |
| 109 | +# ------------------------------------------------------------------------------ |
| 110 | have_manpages=no |
| 111 | AC_ARG_ENABLE(manpages, AS_HELP_STRING([--disable-manpages], [disable manpages])) |
| 112 | AC_PATH_PROG([XSLTPROC], [xsltproc]) |
| 113 | @@ -1653,6 +1672,9 @@ AC_MSG_RESULT([ |
| 114 | SysV init scripts: ${SYSTEM_SYSVINIT_PATH} |
| 115 | SysV rc?.d directories: ${SYSTEM_SYSVRCND_PATH} |
| 116 | Build Python: ${PYTHON} |
| 117 | + Installation Python: ${PYTHON_BINARY} |
| 118 | + sphinx binary: ${SPHINX_BUILD} |
| 119 | + firmware path: ${FIRMWARE_PATH} |
| 120 | PAM modules dir: ${with_pamlibdir} |
| 121 | PAM configuration dir: ${with_pamconfdir} |
| 122 | D-Bus policy dir: ${with_dbuspolicydir} |
| 123 | diff --git a/src/udev/udev-builtin-firmware.c b/src/udev/udev-builtin-firmware.c |
| 124 | new file mode 100644 |
| 125 | index 0000000..bd8c2fb |
| 126 | --- /dev/null |
| 127 | +++ b/src/udev/udev-builtin-firmware.c |
| 128 | @@ -0,0 +1,154 @@ |
| 129 | +/* |
| 130 | + * firmware - Kernel firmware loader |
| 131 | + * |
| 132 | + * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com> |
| 133 | + * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org> |
| 134 | + * |
| 135 | + * This program is free software; you can redistribute it and/or |
| 136 | + * modify it under the terms of the GNU General Public License as |
| 137 | + * published by the Free Software Foundation; either version 2 of the |
| 138 | + * License, or (at your option) any later version. |
| 139 | + * |
| 140 | + * This program is distributed in the hope that it will be useful, but |
| 141 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 142 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 143 | + * General Public License for more details:* |
| 144 | + */ |
| 145 | + |
| 146 | +#include <unistd.h> |
| 147 | +#include <stdlib.h> |
| 148 | +#include <string.h> |
| 149 | +#include <stdio.h> |
| 150 | +#include <getopt.h> |
| 151 | +#include <errno.h> |
| 152 | +#include <stdbool.h> |
| 153 | +#include <sys/utsname.h> |
| 154 | +#include <sys/stat.h> |
| 155 | + |
| 156 | +#include "udev.h" |
| 157 | + |
| 158 | +static bool set_loading(struct udev *udev, char *loadpath, const char *state) { |
| 159 | + FILE *ldfile; |
| 160 | + |
| 161 | + ldfile = fopen(loadpath, "we"); |
| 162 | + if (ldfile == NULL) { |
| 163 | + log_error("error: can not open '%s'", loadpath); |
| 164 | + return false; |
| 165 | + }; |
| 166 | + fprintf(ldfile, "%s\n", state); |
| 167 | + fclose(ldfile); |
| 168 | + return true; |
| 169 | +} |
| 170 | + |
| 171 | +static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size) { |
| 172 | + char *buf; |
| 173 | + FILE *fsource = NULL, *ftarget = NULL; |
| 174 | + bool ret = false; |
| 175 | + |
| 176 | + buf = malloc(size); |
| 177 | + if (buf == NULL) { |
| 178 | + log_error("No memory available to load firmware file"); |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + log_debug("writing '%s' (%zi) to '%s'", source, size, target); |
| 183 | + |
| 184 | + fsource = fopen(source, "re"); |
| 185 | + if (fsource == NULL) |
| 186 | + goto exit; |
| 187 | + ftarget = fopen(target, "we"); |
| 188 | + if (ftarget == NULL) |
| 189 | + goto exit; |
| 190 | + if (fread(buf, size, 1, fsource) != 1) |
| 191 | + goto exit; |
| 192 | + if (fwrite(buf, size, 1, ftarget) == 1) |
| 193 | + ret = true; |
| 194 | +exit: |
| 195 | + if (ftarget != NULL) |
| 196 | + fclose(ftarget); |
| 197 | + if (fsource != NULL) |
| 198 | + fclose(fsource); |
| 199 | + free(buf); |
| 200 | + return ret; |
| 201 | +} |
| 202 | + |
| 203 | +static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test) { |
| 204 | + struct udev *udev = udev_device_get_udev(dev); |
| 205 | + static const char *searchpath[] = { FIRMWARE_PATH }; |
| 206 | + char loadpath[UTIL_PATH_SIZE]; |
| 207 | + char datapath[UTIL_PATH_SIZE]; |
| 208 | + char fwpath[UTIL_PATH_SIZE]; |
| 209 | + const char *firmware; |
| 210 | + FILE *fwfile = NULL; |
| 211 | + struct utsname kernel; |
| 212 | + struct stat statbuf; |
| 213 | + unsigned int i; |
| 214 | + int rc = EXIT_SUCCESS; |
| 215 | + |
| 216 | + firmware = udev_device_get_property_value(dev, "FIRMWARE"); |
| 217 | + if (firmware == NULL) { |
| 218 | + log_error("firmware parameter missing"); |
| 219 | + rc = EXIT_FAILURE; |
| 220 | + goto exit; |
| 221 | + } |
| 222 | + |
| 223 | + /* lookup firmware file */ |
| 224 | + uname(&kernel); |
| 225 | + for (i = 0; i < ELEMENTSOF(searchpath); i++) { |
| 226 | + strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL); |
| 227 | + fwfile = fopen(fwpath, "re"); |
| 228 | + if (fwfile != NULL) |
| 229 | + break; |
| 230 | + |
| 231 | + strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL); |
| 232 | + fwfile = fopen(fwpath, "re"); |
| 233 | + if (fwfile != NULL) |
| 234 | + break; |
| 235 | + } |
| 236 | + |
| 237 | + strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL); |
| 238 | + |
| 239 | + if (fwfile == NULL) { |
| 240 | + log_debug("did not find firmware file '%s'", firmware); |
| 241 | + rc = EXIT_FAILURE; |
| 242 | + /* |
| 243 | + * Do not cancel the request in the initrd, the real root might have |
| 244 | + * the firmware file and the 'coldplug' run in the real root will find |
| 245 | + * this pending request and fulfill or cancel it. |
| 246 | + * */ |
| 247 | + if (!in_initrd()) |
| 248 | + set_loading(udev, loadpath, "-1"); |
| 249 | + goto exit; |
| 250 | + } |
| 251 | + |
| 252 | + if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) { |
| 253 | + if (!in_initrd()) |
| 254 | + set_loading(udev, loadpath, "-1"); |
| 255 | + rc = EXIT_FAILURE; |
| 256 | + goto exit; |
| 257 | + } |
| 258 | + |
| 259 | + if (!set_loading(udev, loadpath, "1")) |
| 260 | + goto exit; |
| 261 | + |
| 262 | + strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL); |
| 263 | + if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) { |
| 264 | + log_error("error sending firmware '%s' to device", firmware); |
| 265 | + set_loading(udev, loadpath, "-1"); |
| 266 | + rc = EXIT_FAILURE; |
| 267 | + goto exit; |
| 268 | + }; |
| 269 | + |
| 270 | + set_loading(udev, loadpath, "0"); |
| 271 | +exit: |
| 272 | + if (fwfile) |
| 273 | + fclose(fwfile); |
| 274 | + return rc; |
| 275 | +} |
| 276 | + |
| 277 | +const struct udev_builtin udev_builtin_firmware = { |
| 278 | + .name = "firmware", |
| 279 | + .cmd = builtin_firmware, |
| 280 | + .help = "kernel firmware loader", |
| 281 | + .run_once = true, |
| 282 | +}; |
| 283 | diff --git a/src/udev/udev-builtin.c b/src/udev/udev-builtin.c |
| 284 | index e6b36f1..cd9947e 100644 |
| 285 | --- a/src/udev/udev-builtin.c |
| 286 | +++ b/src/udev/udev-builtin.c |
| 287 | @@ -31,6 +31,9 @@ static const struct udev_builtin *builtins[] = { |
| 288 | [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid, |
| 289 | #endif |
| 290 | [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs, |
| 291 | +#ifdef HAVE_FIRMWARE |
| 292 | + [UDEV_BUILTIN_FIRMWARE] = &udev_builtin_firmware, |
| 293 | +#endif |
| 294 | [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb, |
| 295 | [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id, |
| 296 | [UDEV_BUILTIN_KEYBOARD] = &udev_builtin_keyboard, |
| 297 | diff --git a/src/udev/udev.h b/src/udev/udev.h |
| 298 | index 1f9c812..fa35a88 100644 |
| 299 | --- a/src/udev/udev.h |
| 300 | +++ b/src/udev/udev.h |
| 301 | @@ -147,6 +147,9 @@ enum udev_builtin_cmd { |
| 302 | UDEV_BUILTIN_BLKID, |
| 303 | #endif |
| 304 | UDEV_BUILTIN_BTRFS, |
| 305 | +#ifdef HAVE_FIRMWARE |
| 306 | + UDEV_BUILTIN_FIRMWARE, |
| 307 | +#endif |
| 308 | UDEV_BUILTIN_HWDB, |
| 309 | UDEV_BUILTIN_INPUT_ID, |
| 310 | UDEV_BUILTIN_KEYBOARD, |
| 311 | @@ -175,6 +178,9 @@ struct udev_builtin { |
| 312 | extern const struct udev_builtin udev_builtin_blkid; |
| 313 | #endif |
| 314 | extern const struct udev_builtin udev_builtin_btrfs; |
| 315 | +#ifdef HAVE_FIRMWARE |
| 316 | +extern const struct udev_builtin udev_builtin_firmware; |
| 317 | +#endif |
| 318 | extern const struct udev_builtin udev_builtin_hwdb; |
| 319 | extern const struct udev_builtin udev_builtin_input_id; |
| 320 | extern const struct udev_builtin udev_builtin_keyboard; |
| 321 | diff --git a/src/udev/udevd.c b/src/udev/udevd.c |
| 322 | index bb92f16..84024ac 100644 |
| 323 | --- a/src/udev/udevd.c |
| 324 | +++ b/src/udev/udevd.c |
| 325 | @@ -125,6 +125,9 @@ struct event { |
| 326 | bool is_block; |
| 327 | sd_event_source *timeout_warning; |
| 328 | sd_event_source *timeout; |
| 329 | +#ifdef HAVE_FIRMWARE |
| 330 | + bool nodelay; |
| 331 | +#endif |
| 332 | }; |
| 333 | |
| 334 | static inline struct event *node_to_event(struct udev_list_node *node) { |
| 335 | @@ -613,6 +616,10 @@ static int event_queue_insert(Manager *manager, struct udev_device *dev) { |
| 336 | event->devnum = udev_device_get_devnum(dev); |
| 337 | event->is_block = streq("block", udev_device_get_subsystem(dev)); |
| 338 | event->ifindex = udev_device_get_ifindex(dev); |
| 339 | +#ifdef HAVE_FIRMWARE |
| 340 | + if (streq(udev_device_get_subsystem(dev), "firmware")) |
| 341 | + event->nodelay = true; |
| 342 | +#endif |
| 343 | |
| 344 | log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev), |
| 345 | udev_device_get_action(dev), udev_device_get_subsystem(dev)); |
| 346 | @@ -698,6 +705,12 @@ static bool is_devpath_busy(Manager *manager, struct event *event) { |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | +#ifdef HAVE_FIRMWARE |
| 351 | + /* allow to bypass the dependency tracking */ |
| 352 | + if (event->nodelay) |
| 353 | + continue; |
| 354 | +#endif |
| 355 | + |
| 356 | /* parent device event found */ |
| 357 | if (event->devpath[common] == '/') { |
| 358 | event->delaying_seqnum = loop_event->seqnum; |
| 359 | -- |
| 360 | 1.8.3.1 |
| 361 | |