File: embed_windows_drivers.cpp

package info (click to toggle)
libcpuid 0.8.1%2Brepack1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,300 kB
  • sloc: ansic: 11,555; python: 1,248; asm: 306; sh: 193; makefile: 110; cpp: 80
file content (88 lines) | stat: -rw-r--r-- 2,188 bytes parent folder | download | duplicates (2)
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
// A simple utility to read and embed the MSR drivers for X86

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <string>
using namespace std;

char* images[2];
int sizes[2];
const char* filenames[] = { "TmpRdr.sys", "TmpRdr64.sys" };
vector<string> source;

bool read_image(const char* drivers_root, const char* filename, char*& image, int& isize)
{
	char fn[512];
	sprintf(fn, "%s\\%s", drivers_root, filename);
	FILE* f = fopen(fn, "rb");
	if (!f) return false;
	fseek(f, 0, SEEK_END);
	long size = ftell(f);
	isize = (int) size;
	rewind(f);
	image = new char[size];
	fread(image, 1, size, f);
	fclose(f);
	return true;
}

bool read_source(const char* filename)
{
	source.clear();
	FILE* f = fopen(filename, "rt");
	if (!f) return false;
	char line[200];
	while (fgets(line, sizeof(line), f)) {
		int i = (int) strlen(line);
		if (i && line[i - 1] == '\n') line[--i] = 0;
		source.push_back(string(line));
	}
	fclose(f);
	return true;
}

void print_image(FILE* f, const char* arch, const char* image, int size)
{
	fprintf(f, "int cc_%sdriver_code_size = %d;\n", arch, size);
	fprintf(f, "uint8_t cc_%sdriver_code[%d] = {", arch, size);
	for (int i = 0; i < size; i++) {
		if (i % 18 == 0) fprintf(f, "\n\t");
		fprintf(f, "0x%02x,", (unsigned) (unsigned char) image[i]);
	}
	fprintf(f, "\n};\n");
}

int main(int argc, char** argv)
{
	if (argc < 3) {
		printf("%s DRIVER_ROOT_FOLDER SOURCE_FILE\n", argv[0]);
		return 1;
	}
	const char* drivers_root = argv[1];
	const char* sourcefile = argv[2];

	for (int i = 0; i < 2; i++)
		if (!read_image(drivers_root, filenames[i], images[i], sizes[i])) {
			printf("Cannot read image `%s' from `%s'!\n", filenames[i], drivers_root);
			return -1;
		}
	if (!read_source(sourcefile)) {
		printf("Cannot read source `%s'\n", sourcefile);
		return -2;
	}
	FILE* f = fopen(sourcefile, "wt");
	bool on = true;
	for (unsigned i = 0; i < source.size(); i++) {
		if (source[i] == "//} end")
			on = true;
		if (on) fprintf(f, "%s\n", source[i].c_str());
		if (source[i] == "//begin {") {
			on = false;
			print_image(f, "x86", images[0], sizes[0]);
			print_image(f, "x64", images[1], sizes[1]);
		}
	}
	return 0;
}