udev-builtin-firmware.c revision 3f60bcb5e69846fe8a3156ca1c9a7e0813ac158a
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * firmware - Kernel firmware loader
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * This program is free software; you can redistribute it and/or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * modify it under the terms of the GNU General Public License as
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * published by the Free Software Foundation; either version 2 of the
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * License, or (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * This program is distributed in the hope that it will be useful, but
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * General Public License for more details:*
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool set_loading(struct udev *udev, char *loadpath, const char *state)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ldfile = fopen(loadpath, "we");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("error: can not open '%s'\n", loadpath);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fprintf(ldfile, "%s\n", state);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen FILE *fsource = NULL, *ftarget = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("No memory available to load firmware file");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_debug("writing '%s' (%zi) to '%s'\n", source, size, target);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fsource = fopen(source, "re");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ftarget = fopen(target, "we");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fread(buf, size, 1, fsource) != 1)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fwrite(buf, size, 1, ftarget) == 1)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct udev *udev = udev_device_get_udev(dev);
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen static const char *searchpath[] = { FIRMWARE_PATH };
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char loadpath[UTIL_PATH_SIZE];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char datapath[UTIL_PATH_SIZE];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen firmware = udev_device_get_property_value(dev, "FIRMWARE");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("firmware parameter missing\n\n");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* lookup firmware file */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen for (i = 0; i < ELEMENTSOF(searchpath); i++) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fwfile = fopen(fwpath, "re");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fwfile = fopen(fwpath, "re");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl log_debug("did not find firmware file '%s'\n", firmware);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl * Do not cancel the request in the initrd, the real root might have
b7e7184634d573fb73143210962acce205f37f61Michael Biebl * the firmware file and the 'coldplug' run in the real root will find
b7e7184634d573fb73143210962acce205f37f61Michael Biebl * this pending request and fulfill or cancel it.
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl log_error("error sending firmware '%s' to device\n", firmware);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen set_loading(udev, loadpath, "0");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenconst struct udev_builtin udev_builtin_firmware = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen .help = "kernel firmware loader",