udev-builtin-firmware.c revision 3f60bcb5e69846fe8a3156ca1c9a7e0813ac158a
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/*
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * firmware - Kernel firmware loader
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen *
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 *
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 *
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 Andersen */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <unistd.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <stdlib.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <string.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <stdio.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <getopt.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <errno.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <stdbool.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <sys/utsname.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include <sys/stat.h>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "udev.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool set_loading(struct udev *udev, char *loadpath, const char *state)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen{
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen FILE *ldfile;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ldfile = fopen(loadpath, "we");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ldfile == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("error: can not open '%s'\n", loadpath);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen };
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fprintf(ldfile, "%s\n", state);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fclose(ldfile);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return true;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen{
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char *buf;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen FILE *fsource = NULL, *ftarget = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen bool ret = false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen buf = malloc(size);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (buf == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("No memory available to load firmware file");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_debug("writing '%s' (%zi) to '%s'\n", source, size, target);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fsource = fopen(source, "re");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fsource == NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto exit;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ftarget = fopen(target, "we");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ftarget == NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto exit;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fread(buf, size, 1, fsource) != 1)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto exit;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fwrite(buf, size, 1, ftarget) == 1)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ret = true;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenexit:
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (ftarget != NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fclose(ftarget);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fsource != NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fclose(fsource);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen free(buf);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return ret;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen{
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 char fwpath[UTIL_PATH_SIZE];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen const char *firmware;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen FILE *fwfile = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct utsname kernel;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen struct stat statbuf;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen unsigned int i;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen int rc = EXIT_SUCCESS;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen firmware = udev_device_get_property_value(dev, "FIRMWARE");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (firmware == NULL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("firmware parameter missing\n\n");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen rc = EXIT_FAILURE;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto exit;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* lookup firmware file */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen uname(&kernel);
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 if (fwfile != NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen break;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fwfile = fopen(fwpath, "re");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fwfile != NULL)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen break;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen }
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (fwfile == NULL) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl log_debug("did not find firmware file '%s'\n", firmware);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl rc = EXIT_FAILURE;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl /*
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 * */
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt if (!in_initrd())
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt set_loading(udev, loadpath, "-1");
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt goto exit;
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt }
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (!in_initrd())
b7e7184634d573fb73143210962acce205f37f61Michael Biebl set_loading(udev, loadpath, "-1");
b7e7184634d573fb73143210962acce205f37f61Michael Biebl rc = EXIT_FAILURE;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl goto exit;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl }
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (!set_loading(udev, loadpath, "1"))
b7e7184634d573fb73143210962acce205f37f61Michael Biebl goto exit;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl
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);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl set_loading(udev, loadpath, "-1");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen rc = EXIT_FAILURE;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen goto exit;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen };
91e7bad45dced1cb2dfaac79337bb08d6e2b74a9Andreas Henriksson
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen set_loading(udev, loadpath, "0");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenexit:
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen if (fwfile)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen fclose(fwfile);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return rc;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt}
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenconst struct udev_builtin udev_builtin_firmware = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen .name = "firmware",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen .cmd = builtin_firmware,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen .help = "kernel firmware loader",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen .run_once = true,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen};
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen