summaryrefslogtreecommitdiff
path: root/precompiled.c
blob: 877f04a7c39f08b51f726e240c84a0a4a81221d5 (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
/*
 * nvidia-installer: A tool for installing NVIDIA software packages on
 * Unix and Linux systems.
 *
 * Copyright (C) 2003 NVIDIA Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * 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.
 *      59 Temple Place - Suite 330
 *      Boston, MA 02111-1307, USA
 *
 *
 * precompiled.c - this source file contains functions for dealing
 * with precompiled kernel interfaces.
 *
 * XXX portions of these functions are lifted from mkprecompiled (it
 * was much easier to duplicate them than to finesse the code to be
 * shared between the installer and mkprecompiled).
 */


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

#include "nvidia-installer.h"
#include "user-interface.h"
#include "precompiled.h"
#include "misc.h"
#include "crc.h"

#define PRECOMPILED_CONSTANT_LENGTH (8 + 4 + 4 + 4 + 4)

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

static 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() */



/*
 * read_proc_version() - return the string contained in /proc/version
 *
 * XXX could use getmntent() to determine where the proc filesystem is
 * mounted.
 */

char *read_proc_version(Options *op)
{
    int fd, ret, len, version_len;
    char *version, *c = NULL;
    char *proc_verson_filename;
    
    len = strlen(op->proc_mount_point) + 9; /* strlen("/version") + 1 */
    proc_verson_filename = (char *) nvalloc(len);
    snprintf(proc_verson_filename, len, "%s/version", op->proc_mount_point);

    if ((fd = open(proc_verson_filename, O_RDONLY)) == -1) {
        ui_warn(op, "Unable to open the file '%s' (%s).",
                proc_verson_filename, strerror(errno));
        return NULL;
    }

    /*
     * it would be more convenient if we could just mmap(2)
     * /proc/version, but proc files do not support mmap(2), so read
     * the file instead
     */
    
    len = version_len = 0;
    version = NULL;

    while (1) {
        if (version_len == len) {
            version_len += NV_LINE_LEN;
            version = nvrealloc(version, version_len);
            c = version + len;
        }
        ret = read(fd, c, version_len - len);
        if (ret == -1) {
            ui_warn(op, "Error reading %s (%s).",
                    proc_verson_filename, 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';

    free(proc_verson_filename);

    return version;

} /* read_proc_version() */



/*
 * precompiled_unpack() - unpack the specified package.  It's not
 * really an error if we can't open the file or if it's not the right
 * format, so just throw an expert-only log message.
 */

PrecompiledInfo *precompiled_unpack(Options *op,
                                    const char *filename,
                                    const char *output_filename,
                                    const char *real_proc_version_string,
                                    const char *package_version)
{
    int dst_fd, fd, offset, len = 0;
    char *buf, *dst;
    uint32 crc, val, size;
    char *version, *description, *proc_version_string;
    struct stat stat_buf;
    PrecompiledInfo *info = NULL;

    fd = dst_fd = size = 0;
    buf = dst = description = proc_version_string = NULL;
    
    /* open the file to be unpacked */
    
    if ((fd = open(filename, O_RDONLY)) == -1) {
        ui_expert(op, "Unable to open precompiled kernel interface file "
                  "'%s' (%s)", filename, strerror(errno));
        goto done;
    }
    
    /* get the file length */
    
    if (fstat(fd, &stat_buf) == -1) {
        ui_expert(op, "Unable to determine '%s' file length (%s).",
                  filename, strerror(errno));
        goto done;
    }
    size = stat_buf.st_size;

    /* check for a minimum length */

    if (size < PRECOMPILED_CONSTANT_LENGTH) {
        ui_expert(op, "File '%s' appears to be too short.", filename);
        goto done;
    }
    
    /* mmap(2) the input file */

    buf = mmap(0, size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
    if (buf == (void *) -1) {
        ui_expert(op, "Unable to mmap file %s (%s).",
                  filename, strerror(errno));
        goto done;
    }
    offset = 0;

    /* check for the header */

    if (strncmp(buf + offset, "NVIDIA  ", 8) != 0) {
        ui_expert(op, "File '%s': unrecognized file format.", filename);
        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 + PRECOMPILED_CONSTANT_LENGTH) > size) {
        ui_expert(op, "Invalid file '%s' (bad version string length %d).",
                  filename, val);
        goto done;
    }
    if (val > 0) {
        version = nvalloc(val+1);
        memcpy(version, buf + offset, val);
        version[val] = '\0';
    } else {
        version = NULL;
    }
    offset += val;

    /* check if this precompiled kernel interface is the right driver
       version */

    if (strcmp(version, package_version) != 0) {
        goto done;
    }


    /* read the description */

    val = decode_uint32(buf + offset);
    offset += 4;
    if ((val + PRECOMPILED_CONSTANT_LENGTH) > size) {
        ui_expert(op, "Invalid file '%s' (bad description string length %d).",
                  filename, val);
        goto done;
    }
    if (val > 0) {
        description = nvalloc(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 + PRECOMPILED_CONSTANT_LENGTH) > size) {
        ui_expert(op, "Invalid file '%s' (bad version string length %d).",
                  filename, val);
        goto done;
    }
    proc_version_string = nvalloc(val+1);
    memcpy(proc_version_string, buf + offset, val);
    offset += val;
    proc_version_string[val] = '\0';

    /* check if the running kernel matches */

    if (strcmp(real_proc_version_string, proc_version_string) != 0) {
        goto done;
    }
    
    ui_log(op, "A precompiled kernel interface for kernel '%s' has been "
           "found here: %s.", description, filename);
    
    /* extract kernel interface module */
    
    len = size - offset;

    if ((dst_fd = open(output_filename, O_CREAT | O_RDWR | O_TRUNC,
                       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
        ui_error(op, "Unable to open output file '%s' (%s).", output_filename,
                 strerror(errno));
        goto done;
    }
    
    /* set the output file length */

    if ((lseek(dst_fd, len - 1, SEEK_SET) == -1) ||
        (write(dst_fd, "", 1) == -1)) {
        ui_error(op, "Unable to set output file '%s' length %d (%s).\n",
                 output_filename, len, strerror(errno));
        goto done;
    }

    /* mmap the dst */

    dst = mmap(0, len, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, dst_fd, 0);
    if (dst == (void *) -1) {
        ui_error(op, "Unable to mmap output file %s (%s).\n",
                 output_filename, strerror(errno));
        goto done;
    }
    
    /* copy */
    
    memcpy(dst, buf + offset, len);
    
    /*
     * now that we can no longer fail, allocate and initialize the
     * PrecompiledInfo structure
     */

    info = (PrecompiledInfo *) nvalloc(sizeof(PrecompiledInfo));
    info->crc = crc;
    info->version = version;
    info->proc_version_string = proc_version_string;
    info->description = description;

    /*
     * XXX so that the proc version and description strings aren't
     * freed below
     */
    
    proc_version_string = description = NULL;

 done:
    
    /* cleanup whatever needs cleaning up */

    if (dst) munmap(dst, len);
    if (buf) munmap(buf, size);
    if (fd > 0) close(fd);
    if (dst_fd > 0) close(dst_fd);
    if (description) free(description);
    if (proc_version_string) free(proc_version_string);

    return info;
    
} /* mkprecompiled_unpack() */