summaryrefslogtreecommitdiff
path: root/mkprecompiled.c
blob: e1f23d5e3fa18ffea8ed6ea0aad2a578db31510c (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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
 * gcc mkprecompiled.c -o mkprecompiled -Wall -g
 *
 * mkprecompiled - this program packages up a precompiled kernel
 * module interface with a list of unresolved symbols in the kernel
 * module.
 *
 * normally, this would be done much more simply with a perl or shell
 * script, but I've implemented it in C because we don't want the
 * installer to rely upon any system utilities that it doesn't
 * absolutely need.
 *
 * commandline options:
 *
 * -i, --interface=<filename>
 * -o, --output=<filename>
 * -u, --unpack=<filename>
 * -d, --description=<kernel description>
 *
 * There is nothing specific to the NVIDIA graphics driver in this
 * program, so it should be usable for the nforce drivers, for
 * example.
 *
 * The format of a precompiled kernel interface package is:
 *
 * the first 8 bytes are: "NVIDIA  "
 *
 * the next 4 bytes (unsigned) are: CRC of the kernel interface module
 * 
 * the next 4 bytes (unsigned) are: the length of the version string (v)
 *
 * the next v bytes are the version string
 *
 * the next 4 bytes (unsigned) are: the length of the description (n)
 *
 * the next n bytes are the description
 *
 * the next 4 bytes (unsigned) are: the length of the proc version string (m)
 *
 * the next m bytes are the proc version string
 *
 * the rest of the file is the kernel interface module
 */

#define BINNAME "mkprecompiled"
#define NV_LINE_LEN 256
#define NV_VERSION_LEN 4096
#define PROC_VERSION "/proc/version"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <ctype.h>
#include <stdarg.h>

#define _GNU_SOURCE /* XXX not portable */
#include <getopt.h>

#define CONSTANT_LENGTH (8 + 4 + 4 + 4 + 4)

typedef unsigned int uint32;
typedef unsigned char uint8;

/*
 * Options structure
 */

typedef struct {
    char *interface;
    char *output;
    char *unpack;
    char *description;
    char *proc_version_string;
    char *version;
    uint8  info;
    uint8  match;
} Options;


#include "crc.h"


/*
 * nv_alloc() - malloc wrapper that checks for errors, and zeros out
 * the memory; if an error occurs, an error is printed to stderr and
 * exit() is called -- this function will only return on success.
 */

void *nv_alloc (size_t size)
{
    void *m = malloc (size);

    if (!m) {
        fprintf (stderr, "%s: memory allocation failure\n", BINNAME);
        exit (1);
    }
    memset (m, 0, size);
    return (m);

} /* nv_alloc() */


/*
 * XXX hack to resolve symbols used by crc.c
 */

void *nvalloc(size_t size)
{
    return nv_alloc(size);
}

void ui_warn(Options *op, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
}



/*
 * nv_open() - open(2) wrapper; prints an error message if open(2)
 * fails and calls exit().  This function only returns on success.
 */

int nv_open(const char *pathname, int flags, mode_t mode)
{
    int fd;
    fd = open(pathname, flags, mode);
    if (fd == -1) {
        fprintf(stderr, "Failure opening %s (%s).\n",
                pathname, strerror(errno));
        exit(1);
    }
    return fd;

} /* nv_name() */



/*
 * nv_get_file_length() - stat(2) wrapper; prints an error message if
 * the system call fails and calls exit().  This function only returns
 * on success.
 */

int nv_get_file_length(const char *filename)
{
    struct stat stat_buf;
    int ret;
    
    ret = stat(filename, &stat_buf);
    if (ret == -1) {
        fprintf(stderr, "Unable to determine '%s' file length (%s).\n",
                filename, strerror(errno));
        exit(1);
    }
    return stat_buf.st_size;

} /* nv_get_file_length() */



/*
 * nv_set_file_length() - wrapper for lseek() and write(); prints an
 * error message if the system calls fail and calls exit().  This
 * function only returns on success.
 */

void nv_set_file_length(const char *filename, int fd, int len)
{
    if ((lseek(fd, len - 1, SEEK_SET) == -1) ||
        (write(fd, "", 1) == -1)) {
        fprintf(stderr, "Unable to set file '%s' length %d (%s).\n",
                filename, fd, strerror(errno));
        exit(1);
    }
} /* nv_set_file_length() */



/*
 * nv_mmap() - mmap(2) wrapper; prints an error message if mmap(2)
 * fails and calls exit().  This function only returns on success.
 */

void *nv_mmap(const char *filename, size_t len, int prot, int flags, int fd)
{
    void *ret;

    ret = mmap(0, len, prot, flags, fd, 0);
    if (ret == (void *) -1) {
        fprintf(stderr, "Unable to mmap file %s (%s).\n",
                filename, strerror(errno));
        exit(1);
    }
    return ret;
    
} /* nv_mmap() */



/*
 * print_help()
 */

void print_help(void)
{
    printf("\n%s [options] \n\n", BINNAME);

    printf("-i, --interface=<interface name>\n");
    printf("  Name of kernel interface file.\n\n");
    
    printf("-o, --output=<output name>\n");
    printf("  Name of output file.\n\n");

    printf("-u, --unpack=<filename>\n");
    printf("  Name of file to be unpacked.\n\n");

    printf("-d, --description=<kernel description>\n");
    printf("  Kernel description.\n\n");

    printf("-v, --proc-version=<string>\n");
    printf("  /proc/version string for target kernel.\n\n");

    printf("--version=<version string>\n\n");
    
    printf("--info\n");
    printf("  Print the description and version number of the file\n");
    printf("  specified by the unpack option.\n\n");

    printf("-m, --match\n");
    printf("  Check if the precompiled package matches the running\n");
    printf("  kernel.\n\n");

    printf("This program can be used to either pack a precompiled kernel\n");
    printf("module interface, or unpack it.\n\n");

} /* print_help() */



/*
 * parse_commandline() - parse the commandline arguments. do some
 * trivial validation, and return an initialized malloc'ed Options
 * structure.
 */

Options *parse_commandline(int argc, char *argv[])
{
    Options *op;
    int c, option_index = 0;

#define INFO_OPTION          4
    
    static struct option long_options[] = {
        { "interface",    1, 0, 'i'                  },
        { "output",       1, 0, 'o'                  },
        { "unpack",       1, 0, 'u'                  },
        { "description",  1, 0, 'd'                  },
        { "help",         0, 0, 'h'                  },
        { "proc-version", 1, 0, 'v'                  },
        { "version",      1, 0, 'V'                  },
        { "info",         0, 0, INFO_OPTION          },
        { "match",        0, 0, 'm'                  },
        { 0,              0, 0, 0                    }
    };
    
    op = (Options *) nv_alloc(sizeof(Options));
    
    while (1) {
        c = getopt_long (argc, argv, "i:b:o:u:d:hv:m",
                         long_options, &option_index);
        if (c == -1)
            break;

        switch(c) {
        case 'i': op->interface = optarg; break;
        case 'o': op->output = optarg; break;
        case 'u': op->unpack = optarg; break;
        case 'd': op->description = optarg; break;
        case 'h': print_help(); exit(0); break;
        case 'v': op->proc_version_string = optarg; break;
        case 'V': op->version = optarg; break;
        case INFO_OPTION:
            op->info = 1; break;
        case 'm':
            op->match = 1; break;
        default:
            fprintf (stderr, "Invalid commandline, please run `%s --help` "
                     "for usage information.\n", argv[0]);
            exit (0);
        }
    }

    if (optind < argc) {
        fprintf (stderr, "Unrecognized arguments: ");
        while (optind < argc)
            fprintf (stderr, "%s ", argv[optind++]);
        fprintf (stderr, "\n");
        fprintf (stderr, "Invalid commandline, please run `%s --help` for "
                 "usage information.\n", argv[0]);
        exit (0);
    }
    
    /* validate options */
    
    if (!op->unpack && !(op->interface && op->proc_version_string)) {
        
        fprintf (stderr, "Incorrect options specified; please run "
                 "`%s --help` for usage information.\n", argv[0]);
        exit(1);
    }
    
    if (!op->info && !op->match && !op->output) {
        fprintf(stderr, "Output file not specified.\n");
        exit(1);
    }

    if (!op->version) {
        fprintf(stderr, "Driver version string not specified.\n");
        exit(1);
    }

    return op;

} /* parse_commandline() */



char *read_proc_version(void)
{
    int fd, ret, len, version_len;
    char *version, *c = NULL;
    
    fd = nv_open(PROC_VERSION, O_RDONLY, 0);

    /*
     * it would be more convenient if we could just mmap(2)
     * /proc/version, but that's not supported, so just read in the
     * whole file
     */
    
    len = version_len = 0;
    version = NULL;

    while (1) {
        if (version_len == len) {
            version_len += NV_VERSION_LEN;
            version = realloc(version, version_len);
            c = version + len;
        }
        ret = read(fd, c, version_len - len);
        if (ret == -1) {
            fprintf(stderr, "Error reading %s (%s).\n",
                    PROC_VERSION, strerror(errno));
            free(version);
            return NULL;
        }
        if (ret == 0) {
            *c = '\0';
            break;
        }
        len += ret;
        c += ret;
    }

    /* replace a newline with a null-terminator */

    c = version;
    while ((*c != '\0') && (*c != '\n')) c++;
    *c = '\0';

    return version;

} /* read_proc_version() */



/*
 * check_match() - read /proc/version, and do a strcmp with str.
 * Returns 1 if the strings match, 0 if they don't match.
 */

int check_match(char *str)
{
    int ret = 0;
    char *version = read_proc_version();
    
    if (strcmp(version, str) == 0) {
        ret = 1;
        printf("kernel interface matches.\n");
    } else {
        ret = 0;
        printf("kernel interface doesn't match.\n");
    }

    free(version);
        
    return ret;
    
} /* check_match() */



/*
 * encode_uint32() - given a uint32, and a 4 byte data buffer, write
 * the integer to the data buffer.
 */

void encode_uint32(uint32 val, uint8 data[4])
{
    data[0] = ((val >> 0)  & 0xff);
    data[1] = ((val >> 8)  & 0xff);
    data[2] = ((val >> 16) & 0xff);
    data[3] = ((val >> 24) & 0xff);

} /* encode_uint32() */



/*
 * decode_uint32() - given an index into a buffer, read the next 4
 * bytes, and build a uint32.
 */

uint32 decode_uint32(char *buf)
{
    uint32 ret = 0;

    ret += (((uint32) buf[3]) & 0xff);
    ret <<= 8;

    ret += (((uint32) buf[2]) & 0xff);
    ret <<= 8;

    ret += (((uint32) buf[1]) & 0xff);
    ret <<= 8;

    ret += (((uint32) buf[0]) & 0xff);
    ret <<= 0;

    return ret;

} /* decode_uint32() */



/*
 * pack() - pack the specified precompiled kernel interface file,
 * prepended with a header, the CRC the driver version, a description
 * string, and the proc version string.
 */ 

int pack(Options *op)
{
    int fd, offset, src_fd;
    uint8 *out, *src, data[4];
    uint32 crc;
    int version_len, description_len, proc_version_len;
    int interface_len, total_len;
    
    /*
     * get the lengths of the description, the proc version string,
     * and the interface file.
     */

    version_len = strlen(op->version);
    description_len = strlen(op->description);
    proc_version_len = strlen(op->proc_version_string);
    interface_len = nv_get_file_length(op->interface);
    
    total_len = CONSTANT_LENGTH +
        version_len + description_len + proc_version_len + interface_len;
    
    /* compute the crc of the kernel interface */

    crc = compute_crc(NULL, op->interface);

    /* open the output file for writing */
    
    fd = nv_open(op->output, O_CREAT|O_RDWR|O_TRUNC,
                 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    
    /* set the output file length */

    nv_set_file_length(op->output, fd, total_len);
    
    /* map the input file */

    out = nv_mmap(op->output, total_len, PROT_READ|PROT_WRITE,
                  MAP_FILE|MAP_SHARED, fd);
    offset = 0;

    /* write the header */
    
    memcpy(&(out[0]), "NVIDIA  ", 8);
    offset += 8;

    /* write the crc */

    encode_uint32(crc, data);
    memcpy(&(out[offset]), data, 4);
    offset += 4;

    /* write the version */

    encode_uint32(version_len, data);
    memcpy(&(out[offset]), data, 4);
    offset += 4;
    
    if (version_len) {
        memcpy(&(out[offset]), op->version, version_len);
        offset += version_len;
    }

    /* write the description */

    encode_uint32(description_len, data);
    memcpy(&(out[offset]), data, 4);
    offset += 4;

    if (description_len) {
        memcpy(&(out[offset]), op->description, description_len);
        offset += description_len;
    }

    /* write the proc version string */
    
    encode_uint32(proc_version_len, data);
    memcpy(&(out[offset]), data, 4);
    offset += 4;
    
    memcpy(&(out[offset]), op->proc_version_string, proc_version_len);
    offset += proc_version_len;

    /* open the precompiled kernel module interface for reading */

    src_fd = nv_open(op->interface, O_RDONLY, 0);
    
    /* mmap() the kernel module interface */

    src = nv_mmap(op->interface, interface_len, PROT_READ,
                  MAP_FILE|MAP_SHARED, src_fd);
    
    memcpy(out + offset, src, interface_len);

    /* unmap src and dst */

    munmap(src, interface_len);
    munmap(out, total_len);
    
    close(src_fd);
    close(fd);

    return 0;

} /* pack() */



/*
 * unpack() - unpack the specified package
 */

int unpack(Options *op)
{
    int dst_fd, fd, ret, offset, len = 0;
    char *buf, *dst;
    uint32 crc, val, size;
    char *version, *description, *proc_version_string;

    fd = dst_fd = 0;
    buf = dst = NULL;
    ret = 1;
    
    /* open the file to be unpacked */
    
    fd = nv_open(op->unpack, O_RDONLY, 0);
    
    /* get the file length */
    
    size = nv_get_file_length(op->unpack);

    /* check for a minimum length */

    if (size < CONSTANT_LENGTH) {
        fprintf(stderr, "File '%s' appears to be too short.\n", op->unpack);
        goto done;
    }
    
    /* mmap(2) the input file */

    buf = nv_mmap(op->unpack, size, PROT_READ, MAP_FILE|MAP_SHARED, fd);
    offset = 0;

    /* check for the header */

    if (strncmp(buf, "NVIDIA  ", 8) != 0) {
        fprintf(stderr, "File '%s': unrecognized file format.\n", op->unpack);
        goto done;
    }
    offset += 8;

    /* read the crc */

    crc = decode_uint32(buf + offset);
    offset += 4;
    
    /* read the version */

    val = decode_uint32(buf + offset);
    offset += 4;

    if (val > 0) {
        version = nv_alloc(val+1);
        memcpy(version, buf + offset, val);
        version[val] = '\0';
    } else {
        version = NULL;
    }
    offset += val;

    /* read the description */

    val = decode_uint32(buf + offset);
    offset += 4;
    if ((val + CONSTANT_LENGTH) > size) {
        fprintf(stderr, "Invalid file.\n");
        goto done;
    }
    if (val > 0) {
        description = nv_alloc(val+1);
        memcpy(description, buf + offset, val);
        description[val] = '\0';
    } else {
        description = NULL;
    }
    offset += val;
    
    /* read the proc version string */

    val = decode_uint32(buf + offset);
    offset += 4;
    if ((val + CONSTANT_LENGTH) > size) {
        fprintf(stderr, "Invalid file.\n");
        goto done;
    }
    proc_version_string = nv_alloc(val+1);
    memcpy(proc_version_string, buf + offset, val);
    offset += val;
    proc_version_string[val] = '\0';

    /*
     * if info was requested, print the description, driver version,
     * crc, proc version, and exit
     */
    
    if (op->info) {
        printf("description: %s\n", description);
        printf("version: %s\n", version);
        printf("crc: %u\n", crc);
        printf("proc version: %s\n", proc_version_string);
        return 0;
    }
    
    /* check if the running kernel matches */

    if (op->match) {
        return check_match(proc_version_string);
    }
    
    /* extract kernel interface module */
    
    len = size - offset;

    dst_fd = nv_open(op->output, O_CREAT | O_RDWR | O_TRUNC,
                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    
    /* set the output file length */

    nv_set_file_length(op->output, dst_fd, len);
    
    /* mmap the dst */

    dst = nv_mmap(op->output, len, PROT_READ|PROT_WRITE,
                  MAP_FILE|MAP_SHARED, dst_fd);
    
    /* copy */
    
    memcpy(dst, buf + offset, len);
    
    ret = 0;

 done:
    if (dst) munmap(dst, len);
    if (buf) munmap(buf, size);
    if (fd > 0) close(fd);
    if (dst_fd > 0) close(dst_fd);
    return ret;
    
} /* unpack() */



/*
 * program entry point
 */

int main(int argc, char *argv[])
{
    Options *op;
    int ret;

    op = parse_commandline(argc, argv);

    if (op->unpack) {
        ret = unpack(op);
    } else { /* pack */
        ret = pack(op);
    }
 
    return ret;

} /* main() */