2013-10-04 00:35:03 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config-util.h>
|
|
|
|
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/partition.h>
|
|
|
|
#include <grub/msdos_partition.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/emu/misc.h>
|
|
|
|
#include <grub/emu/hostdisk.h>
|
|
|
|
#include <grub/emu/getroot.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/i18n.h>
|
|
|
|
#include <grub/list.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2013-11-16 19:21:16 +00:00
|
|
|
#include <time.h>
|
2013-10-04 00:35:03 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <dos/dos.h>
|
|
|
|
#include <dos/filesystem.h>
|
|
|
|
#include <dos/exall.h>
|
|
|
|
#include <proto/dos.h>
|
|
|
|
#include <devices/hardblocks.h>
|
|
|
|
#include <devices/newstyle.h>
|
|
|
|
#include <proto/exec.h>
|
|
|
|
#include <proto/utility.h>
|
|
|
|
#include <proto/partition.h>
|
|
|
|
#include <devices/trackdisk.h>
|
|
|
|
#include <exec/errors.h>
|
|
|
|
|
|
|
|
#define BOUNCE_SIZE 1048576
|
|
|
|
|
|
|
|
static ULONG *bounce;
|
|
|
|
|
2013-10-08 15:04:46 +00:00
|
|
|
char *
|
2015-03-04 00:00:19 +00:00
|
|
|
grub_canonicalize_file_name (const char *path)
|
2013-10-08 15:04:46 +00:00
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
BPTR lck;
|
|
|
|
|
2013-10-10 07:21:33 +00:00
|
|
|
if (path[0] == '/' && path[1] == '/' && path[2] == ':')
|
2013-10-08 15:04:46 +00:00
|
|
|
return xstrdup (path);
|
|
|
|
|
|
|
|
ret = xmalloc (2048);
|
|
|
|
lck = Lock ((const unsigned char *) path, SHARED_LOCK);
|
|
|
|
|
|
|
|
if (!lck || !NameFromLock (lck, (unsigned char *) ret, 2040))
|
|
|
|
{
|
|
|
|
free (ret);
|
|
|
|
ret = xstrdup (path);
|
|
|
|
}
|
|
|
|
if (lck)
|
|
|
|
UnLock (lck);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-04 00:35:03 +00:00
|
|
|
static grub_uint64_t
|
|
|
|
grub_util_get_fd_size_volume (grub_util_fd_t fd __attribute__ ((unused)),
|
|
|
|
const char *dev,
|
|
|
|
unsigned *log_secsize)
|
|
|
|
{
|
|
|
|
struct DriveGeometry *geo;
|
|
|
|
LONG err;
|
|
|
|
unsigned sector_size, log_sector_size;
|
|
|
|
|
|
|
|
if (!bounce)
|
|
|
|
bounce = AllocVec (BOUNCE_SIZE, MEMF_PUBLIC | MEMF_CLEAR);
|
|
|
|
if (!bounce)
|
|
|
|
grub_util_error ("out of memory");
|
|
|
|
|
|
|
|
fd->ioreq->iotd_Req.io_Command = TD_GETGEOMETRY;
|
|
|
|
fd->ioreq->iotd_Req.io_Length = sizeof (*geo);
|
|
|
|
fd->ioreq->iotd_Req.io_Data = bounce;
|
|
|
|
fd->ioreq->iotd_Req.io_Offset = 0;
|
|
|
|
fd->ioreq->iotd_Req.io_Actual = 0;
|
|
|
|
err = DoIO ((struct IORequest *) fd->ioreq);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
geo = (struct DriveGeometry *) bounce;
|
|
|
|
|
|
|
|
sector_size = geo->dg_SectorSize;
|
|
|
|
|
|
|
|
if (sector_size & (sector_size - 1) || !sector_size)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (log_sector_size = 0;
|
|
|
|
(1 << log_sector_size) < sector_size;
|
|
|
|
log_sector_size++);
|
|
|
|
|
|
|
|
if (log_secsize)
|
|
|
|
*log_secsize = log_sector_size;
|
|
|
|
|
|
|
|
return (grub_uint64_t) geo->dg_TotalSectors * (grub_uint64_t) geo->dg_SectorSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_uint64_t
|
|
|
|
grub_util_get_fd_size_file (grub_util_fd_t fd,
|
|
|
|
const char *dev __attribute__ ((unused)),
|
|
|
|
unsigned *log_secsize)
|
|
|
|
{
|
|
|
|
off_t oo, ro;
|
|
|
|
*log_secsize = 9;
|
|
|
|
/* FIXME: support 64-bit offsets. */
|
|
|
|
oo = lseek (fd->fd, 0, SEEK_CUR);
|
|
|
|
ro = lseek (fd->fd, 0, SEEK_END);
|
|
|
|
lseek (fd->fd, oo, SEEK_SET);
|
|
|
|
return ro;
|
|
|
|
}
|
|
|
|
|
2013-10-14 10:47:09 +00:00
|
|
|
int
|
|
|
|
grub_util_fd_seek (grub_util_fd_t fd, grub_uint64_t off)
|
2013-10-04 00:35:03 +00:00
|
|
|
{
|
|
|
|
switch (fd->type)
|
|
|
|
{
|
|
|
|
case GRUB_UTIL_FD_FILE:
|
|
|
|
if (lseek (fd->fd, 0, SEEK_SET) == (off_t) -1)
|
2013-10-14 10:47:09 +00:00
|
|
|
return -1;
|
2013-10-04 00:35:03 +00:00
|
|
|
fd->off = off;
|
|
|
|
return 0;
|
|
|
|
case GRUB_UTIL_FD_DISK:
|
|
|
|
fd->off = off;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-14 10:47:09 +00:00
|
|
|
return -1;
|
2013-10-04 00:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grub_util_fd_t
|
|
|
|
grub_util_fd_open (const char *dev, int flg)
|
|
|
|
{
|
|
|
|
grub_util_fd_t ret = xmalloc (sizeof (*ret));
|
2013-10-10 07:21:33 +00:00
|
|
|
const char *p1, *p2;
|
|
|
|
const char *dname;
|
2013-10-04 00:35:03 +00:00
|
|
|
char *tmp;
|
|
|
|
IPTR unit = 0;
|
|
|
|
ULONG flags = 0;
|
|
|
|
|
2013-10-09 05:04:25 +00:00
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flg |= O_LARGEFILE;
|
|
|
|
#endif
|
|
|
|
#ifdef O_BINARY
|
|
|
|
flg |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
|
2013-10-04 00:35:03 +00:00
|
|
|
ret->off = 0;
|
|
|
|
|
2013-10-10 07:21:33 +00:00
|
|
|
if (dev[0] != '/' || dev[1] != '/' || dev[2] != ':')
|
2013-10-04 00:35:03 +00:00
|
|
|
{
|
|
|
|
ret->type = GRUB_UTIL_FD_FILE;
|
2013-12-24 16:36:10 +00:00
|
|
|
ret->fd = open (dev, flg, S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR);
|
2013-10-04 00:35:03 +00:00
|
|
|
if (ret->fd < 0)
|
|
|
|
{
|
|
|
|
free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-10 07:21:33 +00:00
|
|
|
p1 = strchr (dev + 3, '/');
|
2013-10-04 00:35:03 +00:00
|
|
|
if (!p1)
|
2013-10-10 07:21:33 +00:00
|
|
|
p1 = dev + strlen (dev);
|
2013-10-04 00:35:03 +00:00
|
|
|
else
|
|
|
|
{
|
misc: Make grub_strtol() "end" pointers have safer const qualifiers
Currently the string functions grub_strtol(), grub_strtoul(), and
grub_strtoull() don't declare the "end" pointer in such a way as to
require the pointer itself or the character array to be immutable to the
implementation, nor does the C standard do so in its similar functions,
though it does require us not to change any of it.
The typical declarations of these functions follow this pattern:
long
strtol(const char * restrict nptr, char ** restrict endptr, int base);
Much of the reason for this is historic, and a discussion of that
follows below, after the explanation of this change. (GRUB currently
does not include the "restrict" qualifiers, and we name the arguments a
bit differently.)
The implementation is semantically required to treat the character array
as immutable, but such accidental modifications aren't stopped by the
compiler, and the semantics for both the callers and the implementation
of these functions are sometimes also helped by adding that requirement.
This patch changes these declarations to follow this pattern instead:
long
strtol(const char * restrict nptr,
const char ** const restrict endptr,
int base);
This means that if any modification to these functions accidentally
introduces either an errant modification to the underlying character
array, or an accidental assignment to endptr rather than *endptr, the
compiler should generate an error. (The two uses of "restrict" in this
case basically mean strtol() isn't allowed to modify the character array
by going through *endptr, and endptr isn't allowed to point inside the
array.)
It also means the typical use case changes to:
char *s = ...;
const char *end;
long l;
l = strtol(s, &end, 10);
Or even:
const char *p = str;
while (p && *p) {
long l = strtol(p, &p, 10);
...
}
This fixes 26 places where we discard our attempts at treating the data
safely by doing:
const char *p = str;
long l;
l = strtol(p, (char **)&ptr, 10);
It also adds 5 places where we do:
char *p = str;
while (p && *p) {
long l = strtol(p, (const char ** const)&p, 10);
...
/* more calls that need p not to be pointer-to-const */
}
While moderately distasteful, this is a better problem to have.
With one minor exception, I have tested that all of this compiles
without relevant warnings or errors, and that /much/ of it behaves
correctly, with gcc 9 using 'gcc -W -Wall -Wextra'. The one exception
is the changes in grub-core/osdep/aros/hostdisk.c , which I have no idea
how to build.
Because the C standard defined type-qualifiers in a way that can be
confusing, in the past there's been a slow but fairly regular stream of
churn within our patches, which add and remove the const qualifier in many
of the users of these functions. This change should help avoid that in
the future, and in order to help ensure this, I've added an explanation
in misc.h so that when someone does get a compiler warning about a type
error, they have the fix at hand.
The reason we don't have "const" in these calls in the standard is
purely anachronistic: C78 (de facto) did not have type qualifiers in the
syntax, and the "const" type qualifier was added for C89 (I think; it
may have been later). strtol() appears to date from 4.3BSD in 1986,
which means it could not be added to those functions in the standard
without breaking compatibility, which is usually avoided.
The syntax chosen for type qualifiers is what has led to the churn
regarding usage of const, and is especially confusing on string
functions due to the lack of a string type. Quoting from C99, the
syntax is:
declarator:
pointer[opt] direct-declarator
direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-list[opt] assignment-expression[opt] ]
...
direct-declarator [ type-qualifier-list[opt] * ]
...
pointer:
* type-qualifier-list[opt]
* type-qualifier-list[opt] pointer
type-qualifier-list:
type-qualifier
type-qualifier-list type-qualifier
...
type-qualifier:
const
restrict
volatile
So the examples go like:
const char foo; // immutable object
const char *foo; // mutable pointer to object
char * const foo; // immutable pointer to mutable object
const char * const foo; // immutable pointer to immutable object
const char const * const foo; // XXX extra const keyword in the middle
const char * const * const foo; // immutable pointer to immutable
// pointer to immutable object
const char ** const foo; // immutable pointer to mutable pointer
// to immutable object
Making const left-associative for * and right-associative for everything
else may not have been the best choice ever, but here we are, and the
inevitable result is people using trying to use const (as they should!),
putting it at the wrong place, fighting with the compiler for a bit, and
then either removing it or typecasting something in a bad way. I won't
go into describing restrict, but its syntax has exactly the same issue
as with const.
Anyway, the last example above actually represents the *behavior* that's
required of strtol()-like functions, so that's our choice for the "end"
pointer.
Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-02-21 21:39:33 +00:00
|
|
|
unit = grub_strtoul (p1 + 1, &p2, 16);
|
2013-10-04 00:35:03 +00:00
|
|
|
if (p2 && *p2 == '/')
|
|
|
|
flags = grub_strtoul (p2 + 1, 0, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->mp = CreateMsgPort();
|
|
|
|
if (!ret->mp)
|
|
|
|
{
|
|
|
|
free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret->ioreq = (struct IOExtTD *) CreateIORequest(ret->mp,
|
|
|
|
sizeof(struct IOExtTD));
|
|
|
|
if (!ret->ioreq)
|
|
|
|
{
|
|
|
|
free (ret);
|
|
|
|
DeleteMsgPort (ret->mp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-10 07:21:33 +00:00
|
|
|
dname = dev + 3;
|
2013-10-04 00:35:03 +00:00
|
|
|
ret->type = GRUB_UTIL_FD_DISK;
|
|
|
|
|
2013-10-10 07:21:33 +00:00
|
|
|
tmp = xmalloc (p1 - dname + 1);
|
|
|
|
memcpy (tmp, dname, p1 - dname);
|
|
|
|
tmp[p1 - dname] = '\0';
|
|
|
|
|
2013-10-04 00:35:03 +00:00
|
|
|
ret->is_floppy = (strcmp (tmp, TD_NAME) == 0);
|
|
|
|
ret->is_64 = 1;
|
|
|
|
|
2013-10-10 07:21:33 +00:00
|
|
|
if (OpenDevice ((unsigned char *) tmp, unit,
|
|
|
|
(struct IORequest *) ret->ioreq, flags))
|
2013-10-04 00:35:03 +00:00
|
|
|
{
|
|
|
|
free (tmp);
|
|
|
|
free (ret);
|
|
|
|
DeleteMsgPort (ret->mp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
free (tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
grub_util_fd_read_file (grub_util_fd_t fd, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t size = len;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
ssize_t ret = read (fd->fd, buf, len);
|
|
|
|
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd->off += ret;
|
|
|
|
len -= ret;
|
|
|
|
buf += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write LEN bytes from BUF to FD. Return less than or equal to zero if an
|
|
|
|
error occurs, otherwise return LEN. */
|
|
|
|
static ssize_t
|
|
|
|
grub_util_fd_write_file (grub_util_fd_t fd, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t size = len;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
ssize_t ret = write (fd->fd, buf, len);
|
|
|
|
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd->off += ret;
|
|
|
|
len -= ret;
|
|
|
|
buf += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stop_motor (grub_util_fd_t fd)
|
|
|
|
{
|
|
|
|
if (!fd->is_floppy)
|
|
|
|
return;
|
|
|
|
fd->ioreq->iotd_Req.io_Command = TD_MOTOR;
|
|
|
|
fd->ioreq->iotd_Req.io_Length = 0;
|
|
|
|
fd->ioreq->iotd_Req.io_Data = 0;
|
|
|
|
fd->ioreq->iotd_Req.io_Offset = 0;
|
|
|
|
fd->ioreq->iotd_Req.io_Actual = 0;
|
|
|
|
DoIO ((struct IORequest *) fd->ioreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
grub_util_fd_read_volume (grub_util_fd_t fd, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
grub_uint64_t adj = 0;
|
|
|
|
|
|
|
|
if (!bounce)
|
|
|
|
bounce = AllocVec (BOUNCE_SIZE, MEMF_PUBLIC | MEMF_CLEAR);
|
|
|
|
if (!bounce)
|
|
|
|
grub_util_error ("out of memory");
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
size_t cr = len;
|
|
|
|
LONG err;
|
|
|
|
if (cr > BOUNCE_SIZE)
|
|
|
|
cr = BOUNCE_SIZE;
|
|
|
|
retry:
|
|
|
|
if (fd->is_64)
|
|
|
|
fd->ioreq->iotd_Req.io_Command = NSCMD_TD_READ64;
|
|
|
|
else
|
|
|
|
fd->ioreq->iotd_Req.io_Command = CMD_READ;
|
|
|
|
fd->ioreq->iotd_Req.io_Length = cr;
|
|
|
|
fd->ioreq->iotd_Req.io_Data = bounce;
|
|
|
|
fd->ioreq->iotd_Req.io_Offset = fd->off & 0xFFFFFFFF;
|
|
|
|
fd->ioreq->iotd_Req.io_Actual = fd->off >> 32;
|
|
|
|
err = DoIO ((struct IORequest *) fd->ioreq);
|
|
|
|
if (err == IOERR_NOCMD && fd->is_64)
|
|
|
|
{
|
|
|
|
fd->is_64 = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ());
|
|
|
|
stop_motor (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy (buf, bounce, cr);
|
|
|
|
adj += cr;
|
|
|
|
len -= cr;
|
|
|
|
buf += cr;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd->off += adj;
|
|
|
|
stop_motor (fd);
|
|
|
|
return adj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
grub_util_fd_write_volume (grub_util_fd_t fd, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
grub_uint64_t adj = 0;
|
|
|
|
if (!bounce)
|
|
|
|
bounce = AllocVec (BOUNCE_SIZE, MEMF_PUBLIC | MEMF_CLEAR);
|
|
|
|
if (!bounce)
|
|
|
|
grub_util_error ("out of memory");
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
size_t cr = len;
|
|
|
|
LONG err;
|
|
|
|
if (cr > BOUNCE_SIZE)
|
|
|
|
cr = BOUNCE_SIZE;
|
|
|
|
retry:
|
|
|
|
if (fd->is_64)
|
|
|
|
fd->ioreq->iotd_Req.io_Command = NSCMD_TD_WRITE64;
|
|
|
|
else
|
|
|
|
fd->ioreq->iotd_Req.io_Command = CMD_WRITE;
|
|
|
|
fd->ioreq->iotd_Req.io_Length = cr;
|
|
|
|
fd->ioreq->iotd_Req.io_Data = bounce;
|
|
|
|
fd->ioreq->iotd_Req.io_Offset = fd->off & 0xFFFFFFFF;
|
|
|
|
fd->ioreq->iotd_Req.io_Actual = fd->off >> 32;
|
|
|
|
memcpy (bounce, buf, cr);
|
|
|
|
err = DoIO ((struct IORequest *) fd->ioreq);
|
|
|
|
if (err == IOERR_NOCMD && fd->is_64)
|
|
|
|
{
|
|
|
|
fd->is_64 = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_util_info ("I/O failed with error %d", err);
|
|
|
|
stop_motor (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
adj += cr;
|
|
|
|
len -= cr;
|
|
|
|
buf += cr;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd->off += adj;
|
|
|
|
stop_motor (fd);
|
|
|
|
return adj;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
grub_util_fd_read (grub_util_fd_t fd, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
switch (fd->type)
|
|
|
|
{
|
|
|
|
case GRUB_UTIL_FD_FILE:
|
|
|
|
return grub_util_fd_read_file (fd, buf, len);
|
|
|
|
case GRUB_UTIL_FD_DISK:
|
|
|
|
return grub_util_fd_read_volume (fd, buf, len);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
switch (fd->type)
|
|
|
|
{
|
|
|
|
case GRUB_UTIL_FD_FILE:
|
|
|
|
return grub_util_fd_write_file (fd, buf, len);
|
|
|
|
case GRUB_UTIL_FD_DISK:
|
|
|
|
return grub_util_fd_write_volume (fd, buf, len);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_uint64_t
|
|
|
|
grub_util_get_fd_size (grub_util_fd_t fd,
|
|
|
|
const char *dev,
|
|
|
|
unsigned *log_secsize)
|
|
|
|
{
|
|
|
|
switch (fd->type)
|
|
|
|
{
|
|
|
|
case GRUB_UTIL_FD_FILE:
|
|
|
|
return grub_util_get_fd_size_file (fd, dev, log_secsize);
|
|
|
|
|
|
|
|
case GRUB_UTIL_FD_DISK:
|
|
|
|
return grub_util_get_fd_size_volume (fd, dev, log_secsize);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-27 09:10:08 +00:00
|
|
|
int
|
2013-10-04 00:35:03 +00:00
|
|
|
grub_util_fd_close (grub_util_fd_t fd)
|
|
|
|
{
|
|
|
|
switch (fd->type)
|
|
|
|
{
|
|
|
|
case GRUB_UTIL_FD_FILE:
|
2019-02-27 09:10:08 +00:00
|
|
|
return close (fd->fd);
|
2013-10-04 00:35:03 +00:00
|
|
|
case GRUB_UTIL_FD_DISK:
|
|
|
|
CloseDevice ((struct IORequest *) fd->ioreq);
|
|
|
|
DeleteIORequest((struct IORequest *) fd->ioreq);
|
|
|
|
DeleteMsgPort (fd->mp);
|
2019-02-27 09:10:08 +00:00
|
|
|
return 0;
|
2013-10-04 00:35:03 +00:00
|
|
|
}
|
2019-02-27 09:10:08 +00:00
|
|
|
return 0;
|
2013-10-04 00:35:03 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 10:06:07 +00:00
|
|
|
static int allow_fd_syncs = 1;
|
|
|
|
|
2019-02-27 09:10:08 +00:00
|
|
|
static int
|
2013-10-04 00:35:03 +00:00
|
|
|
grub_util_fd_sync_volume (grub_util_fd_t fd)
|
|
|
|
{
|
2019-02-27 09:10:08 +00:00
|
|
|
LONG err;
|
|
|
|
|
2013-10-04 00:35:03 +00:00
|
|
|
fd->ioreq->iotd_Req.io_Command = CMD_UPDATE;
|
|
|
|
fd->ioreq->iotd_Req.io_Length = 0;
|
|
|
|
fd->ioreq->iotd_Req.io_Data = 0;
|
|
|
|
fd->ioreq->iotd_Req.io_Offset = 0;
|
|
|
|
fd->ioreq->iotd_Req.io_Actual = 0;
|
2019-02-27 09:10:08 +00:00
|
|
|
err = DoIO ((struct IORequest *) fd->ioreq);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-10-04 00:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 09:10:08 +00:00
|
|
|
int
|
2013-10-04 00:35:03 +00:00
|
|
|
grub_util_fd_sync (grub_util_fd_t fd)
|
|
|
|
{
|
2013-11-27 10:06:07 +00:00
|
|
|
if (allow_fd_syncs)
|
2013-10-04 00:35:03 +00:00
|
|
|
{
|
2013-11-27 10:06:07 +00:00
|
|
|
switch (fd->type)
|
|
|
|
{
|
|
|
|
case GRUB_UTIL_FD_FILE:
|
2019-02-27 09:10:08 +00:00
|
|
|
return fsync (fd->fd);
|
2013-11-27 10:06:07 +00:00
|
|
|
case GRUB_UTIL_FD_DISK:
|
2019-02-27 09:10:08 +00:00
|
|
|
return grub_util_fd_sync_volume (fd);
|
2013-11-27 10:06:07 +00:00
|
|
|
}
|
2013-10-04 00:35:03 +00:00
|
|
|
}
|
2019-02-27 09:10:08 +00:00
|
|
|
return 0;
|
2013-10-04 00:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 09:10:08 +00:00
|
|
|
int
|
2013-11-27 13:13:50 +00:00
|
|
|
grub_util_file_sync (FILE *f)
|
|
|
|
{
|
2019-02-27 09:10:08 +00:00
|
|
|
if (fflush (f) != 0)
|
|
|
|
return -1;
|
2013-11-27 13:13:50 +00:00
|
|
|
if (!allow_fd_syncs)
|
2019-02-27 09:10:08 +00:00
|
|
|
return 0;
|
|
|
|
return fsync (fileno (f));
|
2013-11-27 13:13:50 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 10:06:07 +00:00
|
|
|
void
|
|
|
|
grub_util_disable_fd_syncs (void)
|
|
|
|
{
|
|
|
|
allow_fd_syncs = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-04 00:35:03 +00:00
|
|
|
void
|
|
|
|
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
grub_util_fd_strerror (void)
|
|
|
|
{
|
|
|
|
static char buf[201];
|
|
|
|
LONG err = IoErr ();
|
|
|
|
if (!err)
|
|
|
|
return _("Success");
|
|
|
|
memset (buf, '\0', sizeof (buf));
|
|
|
|
Fault (err, (const unsigned char *) "", (STRPTR) buf, sizeof (buf));
|
|
|
|
if (buf[0] == ':')
|
|
|
|
return buf + 1;
|
|
|
|
return buf;
|
|
|
|
}
|
2013-10-13 18:36:28 +00:00
|
|
|
|
|
|
|
FILE *
|
|
|
|
grub_util_fopen (const char *path, const char *mode)
|
|
|
|
{
|
|
|
|
return fopen (path, mode);
|
|
|
|
}
|
2013-10-19 14:14:30 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
grub_util_is_directory (const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (path, &st) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return S_ISDIR (st.st_mode);
|
|
|
|
}
|
|
|
|
|
2013-11-16 19:21:16 +00:00
|
|
|
int
|
|
|
|
grub_util_is_regular (const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (path, &st) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return S_ISREG (st.st_mode);
|
|
|
|
}
|
|
|
|
|
2013-10-19 14:14:30 +00:00
|
|
|
int
|
|
|
|
grub_util_is_special_file (const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (lstat (path, &st) == -1)
|
|
|
|
return 1;
|
|
|
|
return (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode));
|
|
|
|
}
|
2013-11-16 19:21:16 +00:00
|
|
|
|
|
|
|
static char *
|
|
|
|
get_temp_name (void)
|
|
|
|
{
|
|
|
|
static int ctr = 0;
|
|
|
|
char *t;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
t = xasprintf ("T:grub.%d.%d.%d.%d", (int) getpid (), (int) getppid (),
|
|
|
|
ctr++, time (0));
|
|
|
|
if (stat (t, &st) == -1)
|
|
|
|
return t;
|
|
|
|
free (t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
grub_util_make_temporary_file (void)
|
|
|
|
{
|
|
|
|
char *ret = get_temp_name ();
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = grub_util_fopen (ret, "wb");
|
|
|
|
if (f)
|
|
|
|
fclose (f);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
grub_util_make_temporary_dir (void)
|
|
|
|
{
|
|
|
|
char *ret = get_temp_name ();
|
|
|
|
|
|
|
|
grub_util_mkdir (ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_uint32_t
|
|
|
|
grub_util_get_mtime (const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (path, &st) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return st.st_mtime;
|
|
|
|
}
|