-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathkernel.c
2842 lines (2259 loc) · 87.9 KB
/
kernel.c
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nvidia-installer: A tool for installing NVIDIA software packages on
* Unix and Linux systems.
*
* Copyright (C) 2003-2009 NVIDIA Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses>.
*/
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <limits.h>
#include <fts.h>
#include <syscall.h>
#include "nvidia-installer.h"
#include "kernel.h"
#include "user-interface.h"
#include "files.h"
#include "misc.h"
#include "precompiled.h"
#include "crc.h"
#include "conflicting-kernel-modules.h"
/* local prototypes */
static char *default_kernel_module_installation_path(Options *op);
static char *default_kernel_source_path(Options *op);
static char *find_module_substring(char *string, const char *substring);
static int check_for_loaded_kernel_module(Options *op, const char *);
static void check_for_warning_messages(Options *op);
static PrecompiledInfo *scan_dir(Options *op, Package *p,
const char *directory_name,
const char *proc_version_string,
char *const *search_filelist);
static char *build_distro_precompiled_kernel_interface_dir(Options *op);
static char *convert_include_path_to_source_path(const char *inc);
static int run_conftest(Options *op, const char *dir, const char *args,
char **result);
static int run_make(Options *op, Package *p, const char *dir,
const char *cli_options, const char *status,
const RunCommandOutputMatch *match);
static void load_kernel_module_quiet(Options *op, const char *module_name);
static void modprobe_remove_kernel_module_quiet(Options *op, const char *name);
static int kernel_configuration_conflict(Options *op, Package *p,
int target_system_checks);
/*
* Message text that is used by several error messages.
*/
static const char install_your_kernel_source[] =
"Please make sure you have installed the kernel source files for "
"your kernel and that they are properly configured; on Red Hat "
"Linux systems, for example, be sure you have the 'kernel-source' "
"or 'kernel-devel' RPM installed. If you know the correct kernel "
"source files are installed, you may specify the kernel source "
"path with the '--kernel-source-path' command line option.";
/*
* determine_kernel_module_installation_path() - get the installation
* path for the kernel module. The order is:
*
* - if op->kernel_module_installation_path is non-NULL, then it must
* have been initialized by the commandline parser, and therefore we
* should obey that (so just return).
*
* - get the default installation path
*
* - if in expert mode, ask the user, and use what they gave, if
* non-NULL
*/
int determine_kernel_module_installation_path(Options *op)
{
char *result;
int count = 0;
if (op->kernel_module_installation_path) return TRUE;
op->kernel_module_installation_path =
default_kernel_module_installation_path(op);
if (!op->kernel_module_installation_path) return FALSE;
if (op->expert) {
ask_for_kernel_install_path:
result = ui_get_input(op, op->kernel_module_installation_path,
"Kernel module installation path");
if (result && result[0]) {
free(op->kernel_module_installation_path);
op->kernel_module_installation_path = result;
if (!confirm_path(op, op->kernel_module_installation_path)) {
return FALSE;
}
} else {
if (result) free(result);
if (++count < NUM_TIMES_QUESTIONS_ASKED) {
ui_warn(op, "Invalid kernel module installation path.");
goto ask_for_kernel_install_path;
} else {
ui_error(op, "Unable to determine kernel module "
"installation path.");
return FALSE;
}
}
}
if (!mkdir_with_log(op, op->kernel_module_installation_path, 0755))
return FALSE;
ui_expert(op, "Kernel module installation path: %s",
op->kernel_module_installation_path);
return TRUE;
} /* determine_kernel_module_installation_path() */
/*
* run_conftest() - run conftest.sh with the given additional arguments; pass
* the result back to the caller. Returns TRUE on success, or FALSE on failure.
*/
static int run_conftest(Options *op, const char *dir, const char *args,
char **result)
{
char *kernel_source_path, *kernel_output_path;
const char *arch;
int ret;
if (result) {
*result = NULL;
}
arch = get_machine_arch(op);
if (!arch) {
return FALSE;
}
/* Some conftests don't require kernel source/output paths;
* if run_conftest() is run early enough, these may not be
* set yet, so use a placeholder string instead of NULL to
* prevent premature termination of nvstrcat() below. */
kernel_source_path = kernel_output_path = "DIRECTORY_PLACEHOLDER";
if (op->kernel_source_path) {
kernel_source_path = op->kernel_source_path;
}
if (op->kernel_output_path) {
kernel_output_path = op->kernel_output_path;
}
ret = run_command(op, result, FALSE, 0, TRUE,
"sh \"", dir, "/conftest.sh\" \"",
op->utils[CC], "\" \"",
arch, "\" \"",
kernel_source_path, "\" \"",
kernel_output_path, "\" ",
args, NULL);
return ret == 0;
} /* run_conftest() */
/*
* determine_kernel_source_path() - find the qualified path to the
* kernel source tree. This is called from install_from_cwd() if we
* need to compile the kernel interface files. Assigns
* op->kernel_source_path and returns TRUE if successful. Returns
* FALSE if no kernel source tree was found.
*/
int determine_kernel_source_path(Options *op, Package *p)
{
char *result;
char *version_h, *uapi_version_h;
int ret, count = 0;
/* determine the kernel source path */
op->kernel_source_path = default_kernel_source_path(op);
if (op->expert) {
ask_for_kernel_source_path:
result = ui_get_input(op, op->kernel_source_path,
"Kernel source path");
if (result && result[0]) {
if (!directory_exists(result)) {
ui_warn(op, "Kernel source path '%s' does not exist.",
result);
free(result);
if (++count < NUM_TIMES_QUESTIONS_ASKED) {
goto ask_for_kernel_source_path;
} else {
op->kernel_source_path = NULL;
}
} else {
op->kernel_source_path = result;
}
} else {
ui_warn(op, "Invalid kernel source path.");
if (result) free(result);
if (++count < NUM_TIMES_QUESTIONS_ASKED) {
goto ask_for_kernel_source_path;
} else {
op->kernel_source_path = NULL;
}
}
}
/* if we STILL don't have a kernel source path, give up */
if (!op->kernel_source_path) {
ui_error(op, "Unable to find the kernel source tree for the "
"currently running kernel. %s", install_your_kernel_source);
/*
* I suppose we could ask them here for the kernel source
* path, but we've already given users multiple methods of
* specifying their kernel source tree.
*/
return FALSE;
}
/* reject /usr as an invalid kernel source path */
if (!strcmp(op->kernel_source_path, "/usr") ||
!strcmp(op->kernel_source_path, "/usr/")) {
ui_error (op, "The kernel source path '%s' is invalid. %s",
op->kernel_source_path, install_your_kernel_source);
op->kernel_source_path = NULL;
return FALSE;
}
/* check that the kernel source path exists */
if (!directory_exists(op->kernel_source_path)) {
ui_error (op, "The kernel source path '%s' does not exist. %s",
op->kernel_source_path, install_your_kernel_source);
op->kernel_source_path = NULL;
return FALSE;
}
/* check that <path>/include/linux/kernel.h exists */
result = nvstrcat(op->kernel_source_path, "/include/linux/kernel.h", NULL);
if (access(result, F_OK) == -1) {
ui_error(op, "The kernel header file '%s' does not exist. "
"The most likely reason for this is that the kernel source "
"path '%s' is incorrect. %s", result,
op->kernel_source_path, install_your_kernel_source);
free(result);
return FALSE;
}
free(result);
if (!determine_kernel_output_path(op)) return FALSE;
/* check to make sure that either include/linux/version.h or
* include/generated/uapi/linux/version.h is present in the output dir */
#define VERSION_H_PATH "/include/linux/version.h"
#define UAPI_VERSION_H_PATH "/include/generated/uapi/linux/version.h"
version_h = nvstrcat(op->kernel_output_path, VERSION_H_PATH, NULL);
uapi_version_h = nvstrcat(op->kernel_output_path, UAPI_VERSION_H_PATH,
NULL);
ret = access(version_h, F_OK) == 0 || access(uapi_version_h, F_OK) == 0;
if (ret) {
/* OK, we seem to have a path to a configured kernel source tree */
ui_log(op, "Kernel source path: '%s'\n", op->kernel_source_path);
ui_log(op, "Kernel output path: '%s'\n", op->kernel_output_path);
} else {
ui_error(op, "Neither the '%s' nor the '%s' kernel header file exists. "
"The most likely reason for this is that the kernel "
"source files in '%s' have not been configured.",
version_h, uapi_version_h, op->kernel_output_path);
}
free(version_h);
free(uapi_version_h);
return ret;
} /* determine_kernel_source_path() */
/*
* determine_kernel_output_path() - determine the kernel output
* path; unless specified, the kernel output path is assumed to be
* the same as the kernel source path.
*/
int determine_kernel_output_path(Options *op)
{
char *str, *tmp;
/* check --kernel-output-path */
if (op->kernel_output_path) {
ui_log(op, "Using the kernel output path '%s' as specified by the "
"'--kernel-output-path' commandline option.",
op->kernel_output_path);
if (!directory_exists(op->kernel_output_path)) {
ui_error(op, "The kernel output path '%s' does not exist.",
op->kernel_output_path);
op->kernel_output_path = NULL;
return FALSE;
}
return TRUE;
}
/* check SYSOUT */
str = getenv("SYSOUT");
if (str) {
ui_log(op, "Using the kernel output path '%s', as specified by the "
"SYSOUT environment variable.", str);
op->kernel_output_path = str;
if (!directory_exists(op->kernel_output_path)) {
ui_error(op, "The kernel output path '%s' does not exist.",
op->kernel_output_path);
op->kernel_output_path = NULL;
return FALSE;
}
return TRUE;
}
/* check /lib/modules/`uname -r`/{source,build} */
tmp = get_kernel_name(op);
if (tmp) {
char *source_path, *build_source_path;
int len_source_path, len_build_source_path;
source_path = nvstrcat("/lib/modules/", tmp, "/source", NULL);
len_source_path = strlen(source_path);
build_source_path = nvstrcat("/lib/modules/", tmp, "/build/source", NULL);
len_build_source_path = strlen(build_source_path);
if ((!strncmp(op->kernel_source_path, source_path, len_source_path)) ||
(!strncmp(op->kernel_source_path, build_source_path, len_build_source_path))) {
nvfree(source_path);
nvfree(build_source_path);
str = nvstrcat("/lib/modules/", tmp, "/build", NULL);
if (directory_exists(str)) {
op->kernel_output_path = str;
return TRUE;
}
}
else {
nvfree(source_path);
nvfree(build_source_path);
}
nvfree(str);
}
op->kernel_output_path = op->kernel_source_path;
return TRUE;
}
/*
* attach_signature() - If we have a detached signature, verify the checksum of
* the linked module and append the signature.
*/
static int attach_signature(Options *op, Package *p,
const PrecompiledFileInfo *fileInfo,
const char *module_name) {
uint32 actual_crc;
char *module_path;
int ret = FALSE, command_ret;
const char *choices[2] = {
"Install unsigned kernel module",
"Abort installation"
};
ui_log(op, "Attaching module signature to linked kernel module.");
module_path = nvstrcat(p->kernel_module_build_directory, "/",
fileInfo->target_directory, "/", module_name, NULL);
command_ret = verify_crc(op, module_path, fileInfo->linked_module_crc,
&actual_crc);
if (command_ret) {
FILE *module_file;
module_file = fopen(module_path, "a+");
if (module_file && fileInfo->signature_size) {
command_ret = fwrite(fileInfo->signature, 1,
fileInfo->signature_size, module_file);
if (command_ret == fileInfo->signature_size) {
op->kernel_module_signed = ret = !ferror(module_file);
}
} else {
ret = (ui_multiple_choice(op, choices, 2, 1,
"A detached signature was included with "
"the precompiled interface, but opening "
"the linked kernel module and/or the "
"signature file failed.\n\nThe detached "
"signature will not be added; would you "
"still like to install the unsigned "
"kernel module?") == 0);
}
if (module_file) {
fclose(module_file);
}
} else {
ret = (ui_multiple_choice(op, choices, 2, 1,
"A detached signature was included with the "
"precompiled interface, but the checksum of "
"the linked kernel module (%d) did not match "
"the checksum of the the kernel module for "
"which the detached signature was generated "
"(%d).\n\nThis can happen if the linker on "
"the installation target system is not the "
"same as the linker on the system that built "
"the precompiled interface.\n\nThe detached "
"signature will not be added; would you "
"still like to install the unsigned kernel "
"module?", actual_crc,
fileInfo->linked_module_crc) == 0);
}
if (ret) {
if (op->kernel_module_signed) {
ui_log(op, "Signature attached successfully.");
} else {
ui_log(op, "Signature not attached.");
}
} else {
ui_error(op, "Failed to attach signature.");
}
nvfree(module_path);
return ret;
} /* attach_signature() */
/*
* unpack_kernel_modules() - unpack the precompiled file bundle, and link any
* prebuilt kernel interface against their respective binary-only core object
* files. This results in a complete kernel module, ready for installation.
*
* e.g.: ld -r -o nvidia.ko nv-linux.o nvidia/nv-kernel.o_binary
*
* If the precompiled file is a complete kernel module instead of an interface
* file, no additional action is needed after unpacking.
*/
int unpack_kernel_modules(Options *op, Package *p, const char *build_directory,
const PrecompiledFileInfo *fileInfo)
{
int ret;
uint32 attrmask;
if (fileInfo->type != PRECOMPILED_FILE_TYPE_INTERFACE &&
fileInfo->type != PRECOMPILED_FILE_TYPE_MODULE) {
ui_error(op, "The file does not appear to be a valid precompiled "
"kernel interface or module.");
return FALSE;
}
ret = precompiled_file_unpack(op, fileInfo, build_directory);
if (!ret) {
ui_error(op, "Failed to unpack the precompiled file.");
return FALSE;
} else if (fileInfo->type == PRECOMPILED_FILE_TYPE_MODULE) {
ui_log(op, "Kernel module unpacked successfully.");
return TRUE;
}
ret = run_command(op, NULL, TRUE, 0, TRUE,
"cd ", build_directory,
"; ", op->utils[LD], " ", LD_OPTIONS, " -o ",
fileInfo->linked_module_name, " ",
fileInfo->target_directory, "/", fileInfo->name, " ",
fileInfo->target_directory, "/", fileInfo->core_object_name,
NULL);
if (ret != 0) {
ui_error(op, "Unable to link kernel module.");
return FALSE;
}
ui_log(op, "Kernel module linked successfully.");
attrmask = PRECOMPILED_ATTR(DETACHED_SIGNATURE) |
PRECOMPILED_ATTR(LINKED_MODULE_CRC);
if ((fileInfo->attributes & attrmask) == attrmask) {
return attach_signature(op, p, fileInfo,
fileInfo->linked_module_name);
}
return TRUE;
}
/*
* Estimate the number of expected lines of output that will be produced by
* building the kernel modules. single_module may be set to restrict the
* estimate to the result of building the specified module only.
*/
static RunCommandOutputMatch *count_lines(Options *op, Package *p,
const char *dir,
const char *single_module)
{
RunCommandOutputMatch *ret = nvalloc(sizeof(*ret) * 5);
int conftest_count, object_count, module_count, count_success = FALSE;
char *data = NULL;
/*
* Build the make(1) command line. run_make() is explicitly avoided here:
* the output from count-lines.mk shouldn't be logged or displayed.
*/
if (run_command(op, &data, FALSE, NULL, TRUE,
"cd ", dir, "; ",
op->utils[MAKE], " -f count-lines.mk count "
"NV_EXCLUDE_KERNEL_MODULES=", p->excluded_kernel_modules,
single_module ? "" : NULL,
" NV_KERNEL_MODULES=", single_module,
NULL) == 0) {
if (sscanf(data, "conftests:%d objects:%d modules:%d",
&conftest_count, &object_count, &module_count) == 3) {
count_success = TRUE;
}
}
if (!count_success) {
/*
* Something went wrong, but since the counts are only used for cosmetic
* purposes, it is sufficient to log the error and silently fall back to
* approximate default values.
*/
ui_log(op, "Failed to estimate output lines: %s", data);
conftest_count = 250; object_count = 200; module_count = 5;
}
/*
* One line each for entering and leaving each of the Kbuild source and
* output directories
*/
ret[0].lines = 4;
ret[0].initial_match = "make[";
/*
* Each C source file that is compiled into an object file generates one
* line of output beginning with " CC [M] ". Additionally, for each module
* a $module_name.mod.o file is compiled, outputting a line that begins with
* " CC " on older kernels and " CC [M] " on newer ones.
*/
ret[1].lines = object_count + module_count;
ret[1].initial_match = " CC ";
/*
* Each module has has a $module_name.o object linked before the MODPOST
* stage, and a $module_name.ko object linked after. For both linking steps,
* a line beginning with " LD [M] " is printed.
*/
ret[2].lines = module_count * 2;
ret[2].initial_match = " LD [M] ";
/*
* Expect one line of output per conftest, and assume that all the conftests
* have already been run in the "rebuild a single module to isolate modules
* that failed to build" case.
*/
if (single_module == NULL) {
ret[3].lines = conftest_count;
ret[3].initial_match = " CONFTEST: ";
}
return ret;
}
static int check_file(Options *op, Package *p, const char *dir,
const char *modname)
{
int ret;
char *path;
path = nvstrcat(dir, "/", modname, ".ko", NULL);
ret = access(path, F_OK);
if (ret == -1) {
char *single_module_list = nvstrcat("NV_KERNEL_MODULES=\"", modname,
"\"", NULL);
char *rebuild_msg = nvstrcat("Checking to see whether the ", modname,
" kernel module was successfully built",
NULL);
RunCommandOutputMatch *match = count_lines(op, p, dir, modname);
/* Attempt to rebuild the individual module, in case the failure
* is module-specific and due to a different module */
run_make(op, p, dir, single_module_list, rebuild_msg, match);
nvfree(single_module_list);
nvfree(rebuild_msg);
nvfree(match);
/* Check the file again */
ret = access(path, F_OK);
}
nvfree(path);
if (ret == -1) {
ui_error(op, "The %s kernel module was not created.", modname);
}
return ret != -1;
}
/*
* Build the kernel modules that part of this package.
*/
int build_kernel_modules(Options *op, Package *p)
{
return build_kernel_interfaces(op, p, NULL);
}
/*
* Check to see if scripts/sign-file exists under 'dir' and is executable
*/
static char *test_sign_file(const char *dir)
{
char *path = nvstrcat(dir, "/scripts/sign-file", NULL);
struct stat st;
if (stat(path, &st) == 0 &&
(st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) != 0) {
return path;
}
nvfree(path);
return NULL;
}
/*
* sign_kernel_module() - sign a kernel module. The caller is responsible
* for ensuring that the kernel module is already built successfully and that
* op->module_signing_{secret,public}_key are set.
*/
int sign_kernel_module(Options *op, const char *build_directory,
const char *module_filename, int status) {
const RunCommandOutputMatch output_match[] = {
{ .lines = 1, .initial_match = NULL },
{ 0 }
};
int success;
/* Lazily set the default value for module_signing_script. */
if (!op->module_signing_script) {
op->module_signing_script = test_sign_file(op->kernel_output_path);
if (!op->module_signing_script) {
op->module_signing_script = test_sign_file(op->kernel_source_path);
}
}
if (!op->module_signing_script) {
ui_error(op, "nvidia-installer cannot sign %s without the `sign-file` "
"module signing program, and was unable to automatically "
"locate it. If you need to sign the NVIDIA kernel modules, "
"please try again and set the '--module-signing-script' "
"option on the installer's command line.", module_filename);
return FALSE;
}
if (status) {
ui_status_begin(op, "Signing kernel module:", "Signing");
}
if (!op->module_signing_hash) {
op->module_signing_hash = guess_module_signing_hash(op,
build_directory);
}
if (!op->module_signing_hash) {
ui_error(op, "The installer cannot sign %s without specifying a hash "
"algorithm on the command line to %s, and was also unable to "
"automatically detect the hash. If you need to sign the "
"NVIDIA kernel modules, please try again and set the "
"'--module-signing-hash' option on the installer's command "
"line.", module_filename, op->module_signing_script);
}
success = (run_command(op, NULL, TRUE, output_match, TRUE,
"\"", op->module_signing_script, "\" ",
op->module_signing_hash, " \"",
op->module_signing_secret_key, "\" \"",
op->module_signing_public_key, "\" \"",
build_directory, "/", module_filename, "\"", NULL) == 0);
if (status) {
ui_status_end(op, success ? "done." : "Failed to sign kernel module.");
} else {
ui_log(op, success ? "Signed kernel module." : "Module signing failed");
}
op->kernel_module_signed = success;
return success;
}
/*
* create_detached_signature() - Link a precompiled interface into a module,
* sign the resulting linked module, and store a CRC for the linked, unsigned
* module and the detached signature in the provided PrecompiledFileInfo record.
*/
static int create_detached_signature(Options *op, Package *p,
const char *build_dir,
PrecompiledFileInfo *fileInfo,
const char *module_filename)
{
int ret, command_ret;
struct stat st;
char *module_path = NULL, *error = NULL, *target_dir = NULL;
ui_status_begin(op, "Creating a detached signature for the linked "
"kernel module:", "Linking module");
ret = unpack_kernel_modules(op, p, build_dir, fileInfo);
if (!ret) {
ui_error(op, "Failed to link a kernel module for signing.");
goto done;
}
target_dir = nvstrcat(build_dir, "/", fileInfo->target_directory, NULL);
module_path = nvstrcat(target_dir, "/", module_filename, NULL);
command_ret = stat(module_path, &st);
if (command_ret != 0) {
ret = FALSE;
error = "Unable to determine size of linked module.";
goto done;
}
ui_status_update(op, .25, "Generating module checksum");
fileInfo->linked_module_crc = compute_crc(op, module_path);
fileInfo->attributes |= PRECOMPILED_ATTR(LINKED_MODULE_CRC);
ui_status_update(op, .50, "Signing linked module");
ret = sign_kernel_module(op, target_dir, module_filename, FALSE);
if (!ret) {
error = "Failed to sign the linked kernel module.";
goto done;
}
ui_status_update(op, .75, "Detaching module signature");
fileInfo->signature_size = byte_tail(module_path, st.st_size,
&(fileInfo->signature));
if (!(fileInfo->signature) || fileInfo->signature_size == 0) {
error = "Failed to detach the module signature";
goto done;
}
fileInfo->attributes |= PRECOMPILED_ATTR(DETACHED_SIGNATURE);
done:
if (ret) {
ui_status_end(op, "done.");
} else {
ui_status_end(op, "Error.");
if (error) {
ui_error(op, "%s", error);
}
}
nvfree(module_path);
nvfree(target_dir);
return ret;
}
/*
* pack_kernel_interface() - Store the input built interfaces in the
* PrecompiledFileInfo array.
*
* Returns true if the packing was successful, or false on error.
*/
static int pack_kernel_interface(Options *op, Package *p,
const char *build_dir,
PrecompiledFileInfo *fileInfo,
const char *kernel_interface,
const char *module_filename,
const char *core_file)
{
int command_ret;
char *file_path = nvstrcat(build_dir, "/", kernel_interface, NULL);
command_ret = precompiled_read_interface(fileInfo, file_path,
module_filename,
core_file, ".");
nvfree(file_path);
if (command_ret) {
if (op->module_signing_secret_key && op->module_signing_public_key) {
if (!create_detached_signature(op, p, build_dir, fileInfo,
module_filename)) {
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
static void handle_optional_module_failure(Options *op,
KernelModuleInfo module,
const char *action) {
if (module.is_optional) {
ui_error(op, "The %s kernel module failed to %s. This kernel module "
"is required for the proper operation of %s. If you do not "
"need to use %s, you can try to install this driver package "
"again with the '--%s' option.",
module.module_name, action, module.optional_module_dependee,
module.optional_module_dependee, module.disable_option);
}
}
static int pack_kernel_module(Options *op,
const char *build_dir,
PrecompiledFileInfo *fileInfo,
const char *module_filename)
{
int command_ret;
char *file_path = nvstrcat(build_dir, "/", module_filename, NULL);
if (op->module_signing_secret_key && op->module_signing_public_key) {
if (sign_kernel_module(op, build_dir, module_filename, FALSE)) {
fileInfo->attributes |= PRECOMPILED_ATTR(EMBEDDED_SIGNATURE);
} else {
ui_error(op, "Failed to sign precompiled kernel module %s!",
module_filename);
return FALSE;
}
}
command_ret = precompiled_read_module(fileInfo, file_path, "");
nvfree(file_path);
if (command_ret) {
return TRUE;
}
return FALSE;
}
/*
* build_kernel_interfaces() - build the kernel modules and interfaces, and
* store any precompiled files in a newly allocated PrecompiledFileInfo array.
* Return the number of packaged files, or 0 on error.
*
* If fileInfos is NULL, stop after building the kernel modules and do not
* build the interfaces.
*
* When building interfaces, copy everything into a temporary directory and
* work out of the tmpdir. For kernel module only builds, operate within
* p->kernel_module_build_directory, as later packaging steps will look for
* the built files there. If a tmpdir is created, it is removed before exit.
*/
int build_kernel_interfaces(Options *op, Package *p,
PrecompiledFileInfo ** fileInfos)
{
char *tmpdir = NULL, *builddir;
int ret, files_packaged = 0, i;
RunCommandOutputMatch *match;
struct {
const char *sanity_check_name;
const char *conftest_name;
} sanity_checks[] = {
{ "Compiler", "cc_sanity_check" },
{ "Dom0", "dom0_sanity_check" },
{ "Xen", "xen_sanity_check" },
{ "PREEMPT_RT", "preempt_rt_sanity_check" },
{ "vgpu_kvm", "vgpu_kvm_sanity_check" },
};
/* do not build if there is a kernel configuration conflict (and don't
* perform target system checks if we're only building interfaces) */
if (kernel_configuration_conflict(op, p, fileInfos == NULL)) {
return 0;
}
if (fileInfos) {
*fileInfos = NULL;
}
/* create a temporary directory if we will be packing interfaces */
if (fileInfos) {
tmpdir = make_tmpdir(op);
builddir = tmpdir;
if (!tmpdir) {
ui_error(op, "Unable to create a temporary build directory.");
goto done;
}
/* copy the kernel module sources to it */
ui_log(op, "Copying kernel module sources to temporary directory.");
if (!copy_directory_contents
(op, p->kernel_module_build_directory, tmpdir)) {
ui_error(op, "Unable to copy the kernel module sources to temporary "
"directory '%s'.", tmpdir);
goto done;
}
} else {
builddir = p->kernel_module_build_directory;
}
/*
* touch the contents of the build directory, to avoid make time
* skew error messages
*/
/* run sanity checks */
for (i = 0; i < ARRAY_LEN(sanity_checks); i++) {
if (!conftest_sanity_check(op, builddir,
sanity_checks[i].sanity_check_name,