]>
code.delx.au - refind/blob - gptsync/os_unix.c
3 * Unix OS glue for gptsync
5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #define STRINGIFY(s) #s
42 #define STRINGIFY2(s) STRINGIFY(s)
43 #define PROGNAME_S STRINGIFY2(PROGNAME)
53 void error(const char *msg
, ...)
59 vsnprintf(buf
, 4096, msg
, par
);
62 fprintf(stderr
, PROGNAME_S
": %s\n", buf
);
65 void errore(const char *msg
, ...)
71 vsnprintf(buf
, 4096, msg
, par
);
74 fprintf(stderr
, PROGNAME_S
": %s: %s\n", buf
, strerror(errno
));
78 // sector I/O functions
81 // Returns size of disk in blocks (currently bogus)
82 UINT64
disk_size(VOID
) {
83 return (UINT64
) 0xFFFFFFFF;
84 } // UINT64 disk_size()
86 UINTN
read_sector(UINT64 lba
, UINT8
*buffer
)
93 result_seek
= lseek(fd
, offset
, SEEK_SET
);
94 if (result_seek
!= offset
) {
95 errore("Seek to %llu failed", offset
);
99 result_read
= read(fd
, buffer
, 512);
100 if (result_read
< 0) {
101 errore("Data read failed at position %llu", offset
);
104 if (result_read
!= 512) {
105 errore("Data read fell short at position %llu", offset
);
111 UINTN
write_sector(UINT64 lba
, UINT8
*buffer
)
115 ssize_t result_write
;
118 result_seek
= lseek(fd
, offset
, SEEK_SET
);
119 if (result_seek
!= offset
) {
120 errore("Seek to %llu failed", offset
);
124 result_write
= write(fd
, buffer
, 512);
125 if (result_write
< 0) {
126 errore("Data write failed at position %llu", offset
);
129 if (result_write
!= 512) {
130 errore("Data write fell short at position %llu", offset
);
140 UINTN
input_boolean(CHARN
*prompt
, BOOLEAN
*bool_out
)
144 printf("%s", prompt
);
151 if (c
== 'y' || c
== 'Y') {
163 // EFI-style print function
166 void Print(wchar_t *format
, ...)
173 for (i
= 0; format
[i
]; i
++)
174 formatbuf
[i
] = (format
[i
] > 255) ? '?' : (char)(format
[i
] & 0xff);
177 va_start(par
, format
);
178 vsnprintf(buf
, 4096, formatbuf
, par
);
188 int main(int argc
, char *argv
[])
199 fprintf(stderr
, "Usage: " PROGNAME_S
" <device>\n");
204 // set input to unbuffered
206 setvbuf(stdin
, NULL
, _IONBF
, 0);
209 if (stat(filename
, &sb
) < 0) {
210 errore("Can't stat %.300s", filename
);
217 if (S_ISREG(sb
.st_mode
))
218 filesize
= sb
.st_size
;
219 else if (S_ISBLK(sb
.st_mode
))
221 else if (S_ISCHR(sb
.st_mode
))
223 else if (S_ISDIR(sb
.st_mode
))
224 reason
= "Is a directory";
225 else if (S_ISFIFO(sb
.st_mode
))
226 reason
= "Is a FIFO";
228 else if (S_ISSOCK(sb
.st_mode
))
229 reason
= "Is a socket";
232 reason
= "Is an unknown kind of special file";
234 if (reason
!= NULL
) {
235 error("%.300s: %s", filename
, reason
);
240 fd
= open(filename
, O_RDWR
);
241 if (fd
< 0 && errno
== EBUSY
) {
242 fd
= open(filename
, O_RDONLY
);
243 #ifndef NOREADONLYWARN
245 printf("Warning: %.300s opened read-only\n", filename
);
249 errore("Can't open %.300s", filename
);
253 // (try to) guard against TTY character devices
256 error("%.300s: Is a TTY device", filename
);
261 // run sync algorithm
266 if (close(fd
) != 0) {
267 errore("Error while closing %.300s", filename
);