25 |
#include <sys/stat.h> |
#include <sys/stat.h> |
26 |
#include <sys/times.h> |
#include <sys/times.h> |
27 |
#include <malloc.h> |
#include <malloc.h> |
28 |
|
#include <unistd.h> |
29 |
|
|
30 |
#include <grub/util/misc.h> |
#include <grub/util/misc.h> |
31 |
#include <grub/mm.h> |
#include <grub/mm.h> |
108 |
} |
} |
109 |
|
|
110 |
size_t |
size_t |
111 |
|
grub_util_get_fp_size (FILE *fp) |
112 |
|
{ |
113 |
|
struct stat st; |
114 |
|
|
115 |
|
if (fflush (fp) == EOF) |
116 |
|
grub_util_error ("fflush failed"); |
117 |
|
|
118 |
|
if (fstat (fileno (fp), &st) == -1) |
119 |
|
grub_util_error ("fstat failed"); |
120 |
|
|
121 |
|
return st.st_size; |
122 |
|
} |
123 |
|
|
124 |
|
size_t |
125 |
grub_util_get_image_size (const char *path) |
grub_util_get_image_size (const char *path) |
126 |
{ |
{ |
127 |
struct stat st; |
struct stat st; |
134 |
return st.st_size; |
return st.st_size; |
135 |
} |
} |
136 |
|
|
137 |
|
void |
138 |
|
grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp) |
139 |
|
{ |
140 |
|
if (fseek (fp, offset, SEEK_SET) == -1) |
141 |
|
grub_util_error ("fseek failed"); |
142 |
|
|
143 |
|
if (fread (img, 1, size, fp) != size) |
144 |
|
grub_util_error ("read failed"); |
145 |
|
} |
146 |
|
|
147 |
char * |
char * |
148 |
grub_util_read_image (const char *path) |
grub_util_read_image (const char *path) |
149 |
{ |
{ |
159 |
fp = fopen (path, "rb"); |
fp = fopen (path, "rb"); |
160 |
if (! fp) |
if (! fp) |
161 |
grub_util_error ("cannot open %s", path); |
grub_util_error ("cannot open %s", path); |
162 |
|
|
163 |
if (fread (img, 1, size, fp) != size) |
grub_util_read_at (img, size, 0, fp); |
|
grub_util_error ("cannot read %s", path); |
|
164 |
|
|
165 |
fclose (fp); |
fclose (fp); |
166 |
|
|
188 |
} |
} |
189 |
|
|
190 |
void |
void |
191 |
grub_util_write_image (const char *img, size_t size, FILE *out) |
grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out) |
192 |
{ |
{ |
193 |
grub_util_info ("writing 0x%x bytes", size); |
grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset); |
194 |
|
if (fseek (out, offset, SEEK_SET) == -1) |
195 |
|
grub_util_error ("seek failed"); |
196 |
if (fwrite (img, 1, size, out) != size) |
if (fwrite (img, 1, size, out) != size) |
197 |
grub_util_error ("write failed"); |
grub_util_error ("write failed"); |
198 |
} |
} |
199 |
|
|
200 |
|
void |
201 |
|
grub_util_write_image (const char *img, size_t size, FILE *out) |
202 |
|
{ |
203 |
|
grub_util_write_image_at (img, size, 0, out); |
204 |
|
} |
205 |
|
|
206 |
void * |
void * |
207 |
grub_malloc (unsigned size) |
grub_malloc (unsigned size) |
208 |
{ |
{ |