OSDN Git Service

Modify its project name.
[nxt-jsp/etrobo-atk.git] / nxtOSEK / lejos_nxj / src / nxtvm / platform / unix / load.c
1 /**
2  * load.c
3  * Loads binary into VM memory.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include "language.h"
13 #include "magic.h"
14 #include "trace.h"
15 #include "types.h"
16
17 #ifndef O_BINARY
18 #define O_BINARY 0
19 #endif
20
21 void abort_tool (char *msg, char *arg)
22 {
23   printf (msg, arg);
24   exit (1);
25 }
26
27 void readBinary (char *fileName)
28 {
29   int pDesc;
30   int pLength;
31   int pTotal;
32   int pNumRead;
33   byte *pBinary;
34
35   pDesc = open (fileName, O_RDONLY | O_BINARY);
36   if (pDesc == -1)
37     abort_tool ("Unable to open %s\n", fileName);
38   pLength = lseek (pDesc, 0, SEEK_END);
39   lseek (pDesc, 0, SEEK_SET);
40   pBinary = (void *) malloc (pLength);
41   pTotal = 0;
42   while (pTotal < pLength)
43   {
44     pNumRead = read (pDesc, pBinary + pTotal, pLength - pTotal);
45     if (pNumRead == -1)
46     {
47       printf ("Unexpected EOF\n");
48       exit (1);
49     }
50     pTotal += pNumRead;
51   }
52   #if DEBUG_STARTUP
53   printf ("Installing binary %d\n", (int) pBinary);
54   #endif
55   install_binary (pBinary);
56
57   #if DEBUG_STARTUP
58   printf ("Checking validity of magic number\n");
59   #endif
60   if (get_master_record()->magicNumber != MAGIC)
61   {
62     printf ("Fatal: bad magic number: 0x%X. Linked for RCX?"
63             "\n", get_master_record()->magicNumber);
64     exit(1); 
65   }
66 }
67
68
69
70
71
72
73