summaryrefslogtreecommitdiffstats
path: root/loader2/fwloader.c
blob: 84c124e9668d19c32bf9f5e42c9441392a25fc1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 * fwloader.c -- a small firmware loader.
 *
 * Peter Jones (pjones@redhat.com)
 *
 * Copyright 2006-2007 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * General Public License, version 2.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define _GNU_SOURCE 1

#include <argz.h>
#include <envz.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <asm/types.h>
#include <linux/netlink.h>

#include "loader.h"
#include "fwloader.h"
#include "udelay.h"

struct fw_loader {
    int netlinkfd;
    sigset_t sigmask;
    char *fw_pathz;
    size_t fw_pathz_len;
    struct pollfd *fds;
};

int done = 0;

static inline int set_fd_coe(int fd, int enable)
{
    int rc;
    long flags = 0;

    rc = fcntl(fd, F_GETFD, &flags);
    if (rc < 0)
        return rc;

    if (enable)
        flags |= FD_CLOEXEC;
    else
        flags &= ~FD_CLOEXEC;

    rc = fcntl(fd, F_SETFD, flags);
    return rc;
}

static int open_uevent_socket(struct fw_loader *fwl)
{
    int fd, rc;
    struct sockaddr_nl sa;

    fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if (fd < 0)
        return -1;
    set_fd_coe(fd, 1);

    memset(&sa, '\0', sizeof (sa));
    sa.nl_family = AF_NETLINK;
    sa.nl_pid = getpid();
    sa.nl_groups = -1;

    if (bind(fd, (struct sockaddr *)&sa, sizeof (sa)) < 0) {
        close(fd);
        return -1;
    }

    fwl->netlinkfd = fd;

    fd = open("/proc/sys/kernel/hotplug", O_RDWR);
    if (fd >= 0) {
        rc = ftruncate(fd, 0);
        rc = write(fd, "\n", 1);
        close(fd);
    }

    fd = open("/sys/class/firmware/timeout", O_RDWR);
    if (fd >= 0) {
        rc = write(fd, "10", 2);
        close(fd);
    }

    return 0;
}

extern void loaderSegvHandler(int signum);

static void kill_hotplug_signal(int signum)
{
    signal(signum, kill_hotplug_signal);
#if 0
    fprintf(stderr, "got exit signal, quitting\n");
#endif
    done = 1;
}

static int daemonize(struct fw_loader *fwl)
{
    int fd;
    int rc;

#if 0
    int pid;
    pid = fork();
    if (pid != 0)
        return pid;
#endif

    signal(SIGTERM, kill_hotplug_signal);
    signal(SIGSEGV, loaderSegvHandler);
    signal(SIGTTOU, SIG_IGN);
    signal(SIGTTIN, SIG_IGN);
    signal(SIGTSTP, SIG_IGN);

    sigfillset(&fwl->sigmask);
    sigdelset(&fwl->sigmask, SIGTERM);
    sigdelset(&fwl->sigmask, SIGSEGV);
    sigemptyset(&fwl->sigmask);

    prctl(PR_SET_NAME, "hotplug", 0, 0, 0);
    rc = chdir("/");

    fd = open("/proc/self/oom_adj", O_RDWR);
    if (fd >= 0) {
        rc = write(fd, "-17", 3);
        close(fd);
    }

    for (fd = 0; fd < getdtablesize(); fd++) {
        if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
            continue;
        close(fd);
    }

    setsid();
#if 1
    fd = open("/dev/null", O_RDONLY);
#else
    fd = open("/dev/tty5", O_RDONLY);
#endif
    close(STDIN_FILENO);
    dup2(fd, STDIN_FILENO);
    set_fd_coe(STDIN_FILENO, 1);
    close(fd);
#if 1
    fd = open("/dev/null", O_WRONLY);
#else
    fd = open("/dev/tty5", O_WRONLY);
#endif
    close(STDOUT_FILENO);
    dup2(fd, STDOUT_FILENO);
    set_fd_coe(STDOUT_FILENO, 1);
    close(STDERR_FILENO);
    dup2(fd, STDERR_FILENO);
    set_fd_coe(STDERR_FILENO, 1);
    close(fd);

#if 0
    fprintf(stderr, "starting up (pid %d)\n", getpid());
#endif
    return 0;
}

struct uevent {
    char *msg;
    char *path;
    char *envz;
    size_t envz_len;
};

static int get_netlink_msg(struct fw_loader *fwl, struct uevent *uevent)
{
    size_t len;
    ssize_t size;
    static char buffer[2560];
    char *pos;
    char *msg = NULL, *path = NULL, *envz = NULL;
    char *argv[] = { NULL };
    size_t envz_len;
    error_t errnum;

    size = recv(fwl->netlinkfd, &buffer, sizeof (buffer), 0);
    if (size < 0)
        return -1;

    if ((size_t)size > sizeof (buffer) - 1)
        size = sizeof (buffer) - 1;
    buffer[size] = '\0';

    len = strcspn(buffer, "@");
    if (!buffer[len])
        return -1;

    if ((errnum = argz_create(argv, &envz, &envz_len)) > 0)
        goto err;

    pos = buffer;
    msg = strndup(pos, len++);
    pos += len;
    path = strdup(pos);

    pos += strlen(pos) + 1;
    if (len < size + 1) {
        while (pos[0]) {
            char *value = strchr(pos, '=');
            if (value)
                *(value++) = '\0';

            if ((errnum = envz_add(&envz, &envz_len, pos, value)) > 0)
                goto err;
            pos += strlen(pos) + 1;
            if (*pos)
                pos += strlen(pos) + 1;
        }
    }

    uevent->msg = msg;
    uevent->path = path;
    uevent->envz = envz;
    uevent->envz_len = envz_len;
    return 0;
err:
    if (msg)
        free(msg);
    if (path)
        free(path);
    while(envz)
        argz_delete(&envz, &envz_len, envz);
    errno = errnum;
    return -1;
}

/* Set the 'loading' attribute for a firmware device.
 * 1 == currently loading
 * 0 == done loading
 * -1 == error
 */
static int
get_loading_fd(const char *device)
{
    int fd = -1;
    char *loading_path = NULL;

    if (asprintf(&loading_path, "%s/loading", device) < 0)
        return -1;
    fd = open(loading_path, O_RDWR | O_SYNC );
    free(loading_path);
    return fd;
}

static int
set_loading(int fd, int value)
{
    int rc = 0;

    if (value == -1)
        rc = write(fd, "-1", 3);
    else if (value == 0)
        rc = write(fd, "0", 2);
    else if (value == 1)
        rc = write(fd, "1", 2);
    fsync(fd);
    fdatasync(fd);

    return rc < 0 ? rc : 0;
}

static int
fd_map(int fd, char **buf, size_t *bufsize)
{
    struct stat stats;
    int en = 0;

    if (fstat(fd, &stats) < 0) {
        en = errno;
        close(fd);
        errno = en;
        return -1;
    }

    *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
    if (*buf == MAP_FAILED) {
        *buf = NULL;
        en = errno;
        close(fd);
        errno = en;
        return -1;
    }
    *bufsize = stats.st_size;
    return 0;
}

static int
file_map(const char *filename, char **buf, size_t *bufsize, int flags)
{
    int fd, en, rc = 0;

    if ((fd = open(filename, flags ? flags : O_RDONLY)) < 0)
        return -1;

    if (fd_map(fd, buf, bufsize) < 0)
        rc = -1;

    en = errno;
    close(fd);
    errno = en;

    return rc;
}

static void
file_unmap(void *buf, size_t bufsize)
{
    munmap(buf, bufsize);
}

static int
fetcher(char *inpath, int outfd)
{
    char *inbuf = NULL;
    size_t inlen;
    int count;
    int en = 0;
    int rc;

    errno = 0;
    if (access(inpath, F_OK))
        goto out;

    if (file_map(inpath, &inbuf, &inlen, O_RDONLY) < 0)
        goto out;

    lseek(outfd, 0, SEEK_SET);
    rc = ftruncate(outfd, 0);
    rc = ftruncate(outfd, inlen);

    count = 0;
    while (count < inlen) {
        ssize_t c;
        c = write(outfd, inbuf + count, inlen - count);
        if (c <= 0)
            goto out;
        count += c;
    }

out:
    en = errno;
    if (inbuf)
        file_unmap(inbuf, inlen);
    if (en) {
        errno = en;
        return -1;
    }
    return 0;
}


static int
_load_firmware(struct fw_loader *fwl, int fw_fd, char *sysdir, int timeout)
{
    int rc = 0;
    char *fw_buf = NULL, *data = NULL;
    size_t fw_len = 0;
    int dfd = -1, lfd = -1;
    int loading = -2;
    size_t count;

    if ((lfd = get_loading_fd(sysdir)) < 0)
        return lfd;

    timeout *= 1000000;
    while (access(sysdir, F_OK) && timeout) {
        udelay(100);
        timeout -= 100;
    }
    if (!timeout)
        return -ENOENT;

    set_loading(lfd, 1);
    loading = -1;

    if (fd_map(fw_fd, &fw_buf, &fw_len) < 0) {
        rc = -errno;
        goto out;
    }

    if (asprintf(&data, "%s/data", sysdir) < 0) {
        rc = -errno;
        goto out;
    }
    if ((dfd = open(data, O_RDWR)) < 0) {
        rc = -errno;
        goto out;
    }
    count = 0;
    while (count < fw_len) {
        ssize_t c;
        if ((c = write(dfd, fw_buf + count, fw_len - count)) <= 0)
            goto out;
        count += c;
    }
    loading = 0;

out:
    if (dfd >= 0)
        close(dfd);
    if (fw_buf)
        file_unmap(fw_buf, fw_len);
    if (loading != -2)
        set_loading(lfd, loading);
    if (lfd >= 0)
        close(lfd);
    if (data)
        free(data);

    return rc;
}

static void load_firmware(struct fw_loader *fwl, struct uevent *uevent)
{
    char *physdevbus = NULL, *physdevdriver = NULL, *physdevpath = NULL,
         *devpath = NULL, *firmware = NULL, *timeout;
    char *dp = NULL;
    char *fw_file = NULL, *sys_file = NULL;
    char *entry;
    int timeout_secs;
    char *tempfile;
    int fd = -1;

    if (!(tempfile = tempnam("/tmp/", "fw-")))
        return;

    if ((fd = open(tempfile, O_RDWR | O_EXCL | O_CREAT)) < 0) {
        free(tempfile);
        return;
    }

    unlink(tempfile);
    free(tempfile);

    devpath = envz_get(uevent->envz, uevent->envz_len, "DEVPATH");
    firmware = envz_get(uevent->envz, uevent->envz_len, "FIRMWARE");
    physdevbus = envz_get(uevent->envz, uevent->envz_len, "PHYSDEVBUS");
    physdevdriver = envz_get(uevent->envz, uevent->envz_len, "PHYSDEVDRIVER");
    physdevpath = envz_get(uevent->envz, uevent->envz_len, "PHYSDEVPATH");
    timeout = envz_get(uevent->envz, uevent->envz_len, "TIMEOUT");
    
    if (!devpath || !firmware || !physdevbus || !physdevdriver || !physdevpath)
        return;

    timeout_secs = strtol(timeout, NULL, 10);

    /* find the file */
    for (entry = fwl->fw_pathz; entry;
            entry = argz_next(fwl->fw_pathz, fwl->fw_pathz_len, entry)) {
        if (asprintf(&fw_file, "%s/%s", entry, firmware) < 0)
            return;

#if 0
        fprintf(stderr, "Trying to find %s at %s\n", firmware, fw_file);
#endif

        if (fetcher(fw_file, fd) >= 0)
            break;

        free(fw_file);
        fw_file = NULL;
        if (errno == ENOENT || errno == EPERM)
            continue;
        break;
    }
    if (!fw_file)
        goto out;

    /* try the new way first */
    /* PJFIX this is messy */
    dp = strdup(devpath + 2 + strcspn(devpath+1, "/"));
    if (!dp)
        goto out;
    dp[strcspn(dp, "/")] = ':';

    if (asprintf(&sys_file, "/sys%s/%s/", physdevpath, dp) < 0) {
        free(dp);
        goto out;
    }
    free(dp);
    if (_load_firmware(fwl, fd, sys_file, timeout_secs) >= 0)
        goto out;

    /* ok, try the old way */
    free(sys_file);
    sys_file = NULL;

    if (asprintf(&sys_file, "/sys%s/", devpath) < 0) {
        goto out;
    }
    _load_firmware(fwl, fd, sys_file, timeout_secs);
out:
    if (fw_file)
        free(fw_file);
    if (sys_file)
        free(sys_file);
    if (fd != -1)
        close(fd);
}

static void handle_single_uevent(struct fw_loader *fwl, struct uevent *uevent)
{
    char *action = NULL;
    char *subsystem = NULL;

    action = envz_get(uevent->envz, uevent->envz_len, "ACTION");
    subsystem = envz_get(uevent->envz, uevent->envz_len, "SUBSYSTEM");

#if 0
    if (subsystem)
        fprintf(stderr, "subsystem %s ", subsystem);
    if (action)
        fprintf(stderr, "got action %s", action);
    if (subsystem || action)
        fprintf(stderr, "\n");
#endif

    if (!strcmp(action, "add") && !strcmp(subsystem, "firmware"))
        load_firmware(fwl, uevent);
}

static void handle_events(struct fw_loader *fwl)
{
    int rc;
    struct uevent uevent;
    if (fwl->fds == NULL)
        fwl->fds = calloc(1, sizeof (struct pollfd));

    do {
        do {
            if (done)
                exit(0);
            fwl->fds[0].events = POLLIN | POLLPRI;
            fwl->fds[0].revents = 0;
            fwl->fds[0].fd = fwl->netlinkfd;

            errno = 0;
            rc = poll(fwl->fds, 1, -1);

            if (done)
                exit(0);
        } while (rc < 1 || (rc < 0 && errno == EINTR));

        memset(&uevent, '\0', sizeof (uevent));
        if (get_netlink_msg(fwl, &uevent) < 0)
            continue;

        handle_single_uevent(fwl, &uevent);
    } while (1);

    if (fwl->fds) {
        free(fwl->fds);
        fwl->fds = NULL;
    }
}

void set_fw_search_path(struct loaderData_s *loaderData, char *path)
{
    char *old = loaderData->fw_search_pathz, *new = NULL;
    size_t old_len = loaderData->fw_search_pathz_len;

    loaderData->fw_search_pathz = NULL;
    loaderData->fw_search_pathz_len = -1;
    if (!path) {
        if (old)
            free(old);
        return;
    }

    if ((new = strdup(path)) == NULL)
        goto out;

    loaderData->fw_search_pathz = NULL;
    loaderData->fw_search_pathz_len = 0;
    if (argz_create_sep(new, ':', &loaderData->fw_search_pathz,
                &loaderData->fw_search_pathz_len) != 0)
        goto out;

    if (old)
        free(old);

    return;
out:
    if (new)
        free(new);
    loaderData->fw_search_pathz = old;
    loaderData->fw_search_pathz_len = old_len;

    return;
}

void add_fw_search_dir(struct loaderData_s *loaderData, char *dir)
{
    argz_add(&loaderData->fw_search_pathz, &loaderData->fw_search_pathz_len,
            dir);
}

void do_fw_loader(struct loaderData_s *loaderData)
{
    struct fw_loader fwl;
    int rc;

    memset(&fwl, '\0', sizeof (fwl));
    fwl.netlinkfd = -1;

    fwl.fw_pathz = loaderData->fw_search_pathz;
    fwl.fw_pathz_len = loaderData->fw_search_pathz_len;

    rc = daemonize(&fwl);
    if (rc < 0) {
#if 0
        fprintf(stderr, "daemonize() failed with %d: %m\n", rc);
#endif
        exit(1);
    }

    if (open_uevent_socket(&fwl) < 0) {
#if 0
        fprintf(stderr, "open_uevent_socket() failed: %m\n");
#endif
        exit(1);
    }

    handle_events(&fwl);

    exit(1);
}


void start_fw_loader(struct loaderData_s *loaderData) {
    pid_t loader;

    loader = fork();
    if (loader > 0)
        loaderData->fw_loader_pid = loader;
    if (loader != 0)
        return;
    
    do_fw_loader(loaderData);
}

void stop_fw_loader(struct loaderData_s *loaderData) {
    int x = 0, rc;
    siginfo_t siginfo;
    if (loaderData->fw_loader_pid > 0)
        kill(loaderData->fw_loader_pid, SIGTERM);
    while (x <= 100) {
        if (x > 90)
            kill(loaderData->fw_loader_pid, SIGKILL);
        memset(&siginfo, '\0', sizeof (siginfo));
        rc = waitid(P_PID, loaderData->fw_loader_pid, &siginfo, WNOHANG|WEXITED);
        if (rc < 0 && errno == ECHILD)
            return;
        else if (rc == 0 && siginfo.si_pid != 0)
            return;
        else if (rc == 0)
            x++;
        usleep(10000);
    }
    return;
}


/*
 * vim:ts=8:sw=4:sts=4:et
 */