x86_64 build now compiles :)
[tpg/acess2.git] / Usermode / Applications / MultibootCheck_src / MultibootCheck.c
1 /*
2  */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7
8 // === CONSTANTS ===
9 #define SCAN_SPACE      8192
10 #define MAGIC   0x1BADB002
11
12 // === TYPES ===
13 typedef struct {
14         uint32_t        Magic;
15         uint32_t        Flags;
16         uint32_t        Checksum;
17 } __attribute__((packed)) tMBootImg;
18
19 // === PROTOTYPES ===
20 void    CheckMultiboot(char *file);
21
22 // === CODE ===
23 /**
24  */
25 int main(int argc, char *argv[])
26 {
27         if(argc != 2)
28         {
29                 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
30                 fprintf(stderr, " <file>        Path of file to validate\n");
31                 fprintf(stderr, "\n");
32         }
33         
34         CheckMultiboot(argv[1]);
35         
36         return 0;
37 }
38
39 /**
40  */
41 void CheckMultiboot(char *file)
42 {
43         FILE    *fp = fopen(file, "rb");
44          int    len, ofs;
45         char    buf[SCAN_SPACE];
46         tMBootImg       *img;
47         
48         // Error Check
49         if(fp == NULL) {
50                 fprintf(stderr, "Unable to open '%s' for reading\n", file);
51                 exit(EXIT_FAILURE);
52         }
53         
54         // Get file length
55         fseek(fp, 0, SEEK_END);
56         len = ftell(fp);
57         fseek(fp, 0, SEEK_SET);
58         
59         // Clip
60         if(len > SCAN_SPACE)    len = SCAN_SPACE;
61         
62         // Read
63         fread(buf, len, 1, fp);
64         
65         // Scan
66         for(ofs = 0; ofs < len-sizeof(tMBootImg); ofs += 4)
67         {
68                 img = (void*)&buf[ofs];
69                 // Check magic value
70                 if(img->Magic != MAGIC) continue;
71                 // Validate checksum
72                 if(img->Magic + img->Flags + img->Checksum != 0) {
73                         printf("Checksum fail at 0x%x\n", ofs);
74                         continue;
75                 }
76                 // Check undefined feature flags
77                 if(img->Flags & 0xFFF8) {
78                         printf("Header at 0x%x uses undefined features (0x%lx)\n", ofs, img->Flags);
79                         return ;
80                 }
81                 // Print success
82                 printf("Found Multiboot header at 0x%x\n", ofs);
83                 return ;
84         }
85         
86         printf("No multiboot header found\n");
87 }

UCC git Repository :: git.ucc.asn.au