grub2: bring back build of aros-side grub2 tools
[AROS.git] / tools / adflib / Generic / adf_nativ.c
blobc77cd810818a3a4dd3b5ca8231caa0eb9cb14085
1 /*
2 * adf_nativ.c
4 * file
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <fcntl.h>
11 #include <unistd.h>
13 #include <sys/types.h>
15 #include "adf_str.h"
16 #include "adf_nativ.h"
17 #include "adf_err.h"
19 extern struct Env adfEnv;
22 * myInitDevice
24 * must fill 'dev->size'
26 static RETCODE myInitDevice(struct Device* dev, char* name,BOOL ro)
28 struct nativeDevice* nDev;
29 int fd;
30 off_t size;
32 fd = open(name, (ro ? O_RDONLY : O_RDWR));
33 if (fd < 0) {
34 (*adfEnv.eFct)("myInitDevice : open");
35 return RC_ERROR;
38 nDev = (struct nativeDevice*)malloc(sizeof(struct nativeDevice));
39 if (!nDev) {
40 (*adfEnv.eFct)("myInitDevice : malloc");
41 return RC_ERROR;
43 dev->nativeDev = nDev;
44 if (!ro)
45 /* check if device is writable, if not, force readOnly to TRUE */
46 dev->readOnly = FALSE;
47 else
48 /* mount device as read only */
49 dev->readOnly = TRUE;
51 nDev->fd = fdopen(fd, ro ? "rb" : "wb+");
52 size = lseek(fd, 0, SEEK_END);
54 dev->sectors = 61;
55 dev->heads = 126;
56 dev->cylinders = size / 512 / dev->sectors / dev->heads;
57 dev->size = dev->cylinders * dev->heads * dev->sectors * 512;
58 dev->isNativeDev = TRUE;
60 return RC_OK;
65 * myReadSector
68 static RETCODE myReadSector(struct Device *dev, long n, int size, unsigned char* buf)
70 struct nativeDevice *nDev = dev->nativeDev;
71 int fd = fileno(nDev->fd);
73 if (lseek(fd, (off_t)n * 512, SEEK_SET) != (off_t)-1)
74 if (read(fd, buf, size) == size)
75 return RC_OK;
77 return RC_ERROR;
82 * myWriteSector
85 static RETCODE myWriteSector(struct Device *dev, long n, int size, unsigned char* buf)
87 struct nativeDevice *nDev = dev->nativeDev;
88 int fd = fileno(nDev->fd);
90 if (lseek(fd, (off_t)n * 512, SEEK_SET) != (off_t)-1)
91 if (write(fd, buf, size) == size)
92 return RC_OK;
94 return RC_ERROR;
99 * myReleaseDevice
101 * free native device
103 static RETCODE myReleaseDevice(struct Device *dev)
105 struct nativeDevice* nDev;
107 nDev = (struct nativeDevice*)dev->nativeDev;
109 fclose(nDev->fd);
110 free(nDev);
112 return RC_OK;
117 * myIsDevNative
120 BOOL myIsDevNative(char *devName)
122 return (strncmp(devName,"/dev/",5)==0);
126 * adfInitNativeFct
129 void adfInitNativeFct()
131 struct nativeFunctions *nFct;
133 nFct = (struct nativeFunctions*)adfEnv.nativeFct;
135 nFct->adfInitDevice = myInitDevice ;
136 nFct->adfNativeReadSector = myReadSector ;
137 nFct->adfNativeWriteSector = myWriteSector ;
138 nFct->adfReleaseDevice = myReleaseDevice ;
139 nFct->adfIsDevNative = myIsDevNative;
142 /*##########################################################################*/