From d54e27e0c3fb00608855d5ff87c5592e6c62db39 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 3 Feb 2010 08:30:06 +0800 Subject: [PATCH 1/1] More additions --- .../Applications/MultibootCheck_src/Makefile | 8 ++ .../MultibootCheck_src/MultibootCheck.c | 86 ++++++++++++ Usermode/Applications/cpuid_src/Makefile | 8 ++ Usermode/Applications/cpuid_src/main.c | 124 ++++++++++++++++++ 4 files changed, 226 insertions(+) create mode 100644 Usermode/Applications/MultibootCheck_src/Makefile create mode 100644 Usermode/Applications/MultibootCheck_src/MultibootCheck.c create mode 100644 Usermode/Applications/cpuid_src/Makefile create mode 100644 Usermode/Applications/cpuid_src/main.c diff --git a/Usermode/Applications/MultibootCheck_src/Makefile b/Usermode/Applications/MultibootCheck_src/Makefile new file mode 100644 index 00000000..291376e6 --- /dev/null +++ b/Usermode/Applications/MultibootCheck_src/Makefile @@ -0,0 +1,8 @@ +# Project: cat + +-include ../Makefile.cfg + +OBJ = MultibootCheck.o +BIN = ../MultibootCheck + +-include ../Makefile.tpl diff --git a/Usermode/Applications/MultibootCheck_src/MultibootCheck.c b/Usermode/Applications/MultibootCheck_src/MultibootCheck.c new file mode 100644 index 00000000..ebb5b68b --- /dev/null +++ b/Usermode/Applications/MultibootCheck_src/MultibootCheck.c @@ -0,0 +1,86 @@ +/* + */ + +#include +#include + +// === CONSTANTS === +#define SCAN_SPACE 8192 +#define MAGIC 0x1BADB002 + +// === TYPES === +typedef struct { + unsigned long Magic; + unsigned long Flags; + unsigned long Checksum; +} tMBootImg; + +// === PROTOTYPES === +void CheckMultiboot(char *file); + +// === CODE === +/** + */ +int main(int argc, char *argv[]) +{ + if(argc != 2) + { + fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, " Path of file to validate\n"); + fprintf(stderr, "\n"); + } + + CheckMultiboot(argv[1]); + + return 0; +} + +/** + */ +void CheckMultiboot(char *file) +{ + FILE *fp = fopen(file, "rb"); + int len, ofs; + char buf[SCAN_SPACE]; + tMBootImg *img; + + // Error Check + if(fp == NULL) { + fprintf(stderr, "Unable to open '%s' for reading\n", file); + exit(EXIT_FAILURE); + } + + // Get file length + fseek(fp, 0, SEEK_END); + len = ftell(fp); + fseek(fp, 0, SEEK_SET); + + // Clip + if(len > SCAN_SPACE) len = SCAN_SPACE; + + // Read + fread(buf, len, 1, fp); + + // Scan + for(ofs = 0; ofs < len-sizeof(tMBootImg); ofs += 4) + { + img = (void*)&buf[ofs]; + // Check magic value + if(img->Magic != MAGIC) continue; + // Validate checksum + if(img->Magic + img->Flags + img->Checksum != 0) { + printf("Checksum fail at 0x%x\n", ofs); + continue; + } + // Check undefined feature flags + if(img->Flags & 0xFFF8) { + printf("Header at 0x%x uses undefined features (0x%lx)\n", ofs, img->Flags); + return ; + } + // Print success + printf("Found Multiboot header at 0x%x\n", ofs); + return ; + } + + printf("No multiboot header found\n"); +} diff --git a/Usermode/Applications/cpuid_src/Makefile b/Usermode/Applications/cpuid_src/Makefile new file mode 100644 index 00000000..66739a88 --- /dev/null +++ b/Usermode/Applications/cpuid_src/Makefile @@ -0,0 +1,8 @@ +# Project: cpuid + +-include ../Makefile.cfg + +OBJ = main.o +BIN = ../cpuid + +-include ../Makefile.tpl diff --git a/Usermode/Applications/cpuid_src/main.c b/Usermode/Applications/cpuid_src/main.c new file mode 100644 index 00000000..98dcbf4b --- /dev/null +++ b/Usermode/Applications/cpuid_src/main.c @@ -0,0 +1,124 @@ +/* + * Acess2 - CPUID Parser + */ +#include +#include +#include + +// === PROTOTYPES === + int main(int argc, char *argv[]); +void cpuid(uint32_t Num, uint32_t *EAX, uint32_t *EBX, uint32_t *EDX, uint32_t *ECX); + +// === CODE === +int main(int argc, char *argv[]) +{ + char sVendorId[13]; + int iMaxBasic = 0; + uint32_t eax, ebx, edx, ecx; + + // -- Get Vendor ID and maximum ID + cpuid(0, + (uint32_t*)&iMaxBasic, (uint32_t*)&sVendorId[0], + (uint32_t*)&sVendorId[4], (uint32_t*)&sVendorId[8]); + sVendorId[12] = '\0'; + printf("Vendor: %s\n", sVendorId); + printf("Maximum CPUID: %i\n", iMaxBasic); + + // -- Get Processor Information + if( iMaxBasic == 0 ) return 0; + /* +# 3:0 - Stepping +# 7:4 - Model +# 11:8 - Family +# 13:12 - Processor Type +# 19:16 - Extended Model +# 27:20 - Extended Famil + */ + cpuid(1, &eax, &ebx, &edx, &ecx); + printf("Model: Family %i, Model %i, Stepping %i\n", (eax>>8)&7, (eax>>4)&7, eax&7); + printf("Type: %i\n", (eax>>12)&7); + printf("EDX Features: "); + if(edx & 1 << 0) printf("FPU "); + if(edx & 1 << 1) printf("VME "); + if(edx & 1 << 2) printf("DE "); + if(edx & 1 << 3) printf("PSE "); + if(edx & 1 << 4) printf("TSC "); + if(edx & 1 << 5) printf("MSR "); + if(edx & 1 << 6) printf("PAE "); + if(edx & 1 << 7) printf("MCE "); + if(edx & 1 << 8) printf("CX8 "); + if(edx & 1 << 9) printf("APIC "); + // -- 1 << 10 + if(edx & 1 << 11) printf("SEP "); + if(edx & 1 << 12) printf("MTRR "); + if(edx & 1 << 13) printf("PGE "); + if(edx & 1 << 14) printf("MCA "); + if(edx & 1 << 15) printf("CMOV "); + if(edx & 1 << 16) printf("PAT "); + if(edx & 1 << 17) printf("PSE36 "); + if(edx & 1 << 18) printf("PSN "); + if(edx & 1 << 19) printf("CLF "); + // -- 1 << 20 + if(edx & 1 << 21) printf("DTES "); + if(edx & 1 << 22) printf("ACPI "); + if(edx & 1 << 23) printf("MMX "); + if(edx & 1 << 25) printf("SSE "); + if(edx & 1 << 26) printf("SSE2 "); + if(edx & 1 << 27) printf("SS "); + if(edx & 1 << 28) printf("HTT "); + if(edx & 1 << 29) printf("TM1 "); + if(edx & 1 << 30) printf("IA64 "); + if(edx & 1 << 31) printf("PBE "); + printf("\n"); + + printf("ECX Features: "); + if(ecx & 1 << 0) printf("SSE3 "); + if(ecx & 1 << 1) printf("PCLMUL "); + if(ecx & 1 << 4) printf("DTES64 "); + if(ecx & 1 << 5) printf("VMX "); + if(ecx & 1 << 6) printf("SMX "); + if(ecx & 1 << 7) printf("EST "); + if(ecx & 1 << 8) printf("TM2 "); + if(ecx & 1 << 9) printf("SSSE3 "); + if(ecx & 1 << 10) printf("CID "); + // -- 1 << 11 + if(ecx & 1 << 12) printf("FMA "); + if(ecx & 1 << 13) printf("CX16 "); + if(ecx & 1 << 14) printf("ETPRD "); + if(ecx & 1 << 15) printf("PDCM "); + // -- 1 << 16 + // -- 1 << 17 + if(ecx & 1 << 18) printf("DCA "); + if(ecx & 1 << 19) printf("SSE4.1"); + if(ecx & 1 << 20) printf("SSE4.2"); + if(ecx & 1 << 21) printf("x2APIC "); + if(ecx & 1 << 22) printf("MOVBE "); + if(ecx & 1 << 23) printf("POPCNT "); + // -- 1 << 24 + // -- 1 << 25 + if(ecx & 1 << 26) printf("XSAVE "); + if(ecx & 1 << 27) printf("OSXSAVE "); + if(ecx & 1 << 28) printf("AVX "); + printf("\n"); + + return 0; +} + +/** + * \brief Call the CPUID opcode + */ +void cpuid(uint32_t Num, uint32_t *EAX, uint32_t *EBX, uint32_t *EDX, uint32_t *ECX) +{ + uint32_t eax, ebx, edx, ecx; + + __asm__ __volatile__ ( + "cpuid" + : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) + : "a"(Num) + ); + + if(EAX) *EAX = eax; + if(EBX) *EBX = ebx; + if(EDX) *EDX = edx; + if(ECX) *ECX = ecx; +} -- 2.20.1