2 /* #include <unistd.h> */
7 #include <linuxmt/config.h>
8 #include <linuxmt/mem.h>
10 char *failed
= "insmod: failed\n";
12 #define STDERR_FILENO 2
14 static char modbuf
[DATASIZE
< TEXTSIZE
? TEXTSIZE
: DATASIZE
];
22 unsigned int data
, text
, len
, doff
, coff
;
27 write(STDERR_FILENO
, "usage: insmod <module>\n",23);
30 /* Clean up the old module */
32 /* Open the mem device to access kernel space */
33 if ((md
= open("/dev/mem",O_WRONLY
)) == -1) {
34 write(STDERR_FILENO
, failed
, F_LEN
);
35 write(STDERR_FILENO
, "Could not open /dev/mem.\n",25);
38 /* Get the addresses in kernel space to copy the module to */
39 if (((ioctl(md
, MEM_GETMODTEXT
, &text
)) == -1) ||
40 ((ioctl(md
, MEM_GETMODDATA
, &data
)) == -1) ||
41 ((ioctl(md
, MEM_GETDS
, &doff
)) == -1) ||
42 ((ioctl(md
, MEM_GETCS
, &coff
)) == -1)) {
43 write(STDERR_FILENO
, failed
, F_LEN
);
44 write(STDERR_FILENO
, "Kernel does not support modules.\n",33);
47 /* Calculate /dev/mem seek points */
48 datap
= ((long)doff
<< 4) + (long)data
;
49 textp
= ((long)coff
<< 4) + (long)text
;
51 strcpy(fname
, argv
[1], 15);
53 if ((od
= open(fname
,O_RDONLY
)) == -1) {
54 write(STDERR_FILENO
, failed
, F_LEN
);
55 write(STDERR_FILENO
, "Could not open module ",22);
56 write(STDERR_FILENO
, argv
[1], strlen(argv
[1]));
59 if ((fstat(od
, &buf
)) == -1) {
60 write(STDERR_FILENO
, failed
, F_LEN
);
63 if ((len
= buf
.st_size
) > TEXTSIZE
) {
64 write(STDERR_FILENO
, failed
, F_LEN
);
65 write(STDERR_FILENO
, "Module too big.\n",16);
68 if ((lseek(md
, textp
, SEEK_SET
)) == -1) {
69 write(STDERR_FILENO
, failed
, F_LEN
);
72 /* Copy <module>.T into the kernel */
73 if ((read(od
, modbuf
, len
)) != len
) {
74 write(STDERR_FILENO
, failed
, F_LEN
);
77 if ((write(md
, modbuf
, len
)) != len
) {
78 write(STDERR_FILENO
, failed
, F_LEN
);
82 strcpy(fname
, argv
[1], 15);
84 if ((od
= open(fname
,O_RDONLY
)) == -1) {
85 write(STDERR_FILENO
, failed
, F_LEN
);
86 write(STDERR_FILENO
, "Could not open module ",22);
87 write(STDERR_FILENO
, argv
[1], strlen(argv
[1]));
90 if ((fstat(od
, &buf
)) == -1) {
91 write(STDERR_FILENO
, failed
, F_LEN
);
94 if ((len
= buf
.st_size
) > TEXTSIZE
) {
95 write(STDERR_FILENO
, failed
, F_LEN
);
96 write(STDERR_FILENO
, "Module too big.\n",16);
99 if ((lseek(md
, datap
, SEEK_SET
)) == -1) {
100 write(STDERR_FILENO
, failed
, F_LEN
);
103 /* Copy <module>.D into the kernel */
104 if ((read(od
, modbuf
, len
)) != len
) {
105 write(STDERR_FILENO
, failed
, F_LEN
);
108 if ((write(md
, modbuf
, len
)) != len
) {
109 write(STDERR_FILENO
, failed
, F_LEN
);
113 /* Initialise the module. */