grub/util/getroot_aros.c
2013-10-04 02:35:03 +02:00

271 lines
6 KiB
C

/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 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 <config.h>
#include <grub/util/misc.h>
#include <grub/util/lvm.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/emu/misc.h>
#include <grub/emu/hostdisk.h>
#include <grub/emu/getroot.h>
#include <string.h>
#include <dos/dos.h>
#include <dos/filesystem.h>
#include <dos/exall.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <devices/trackdisk.h>
char *
grub_util_part_to_disk (const char *dev,
struct stat *st __attribute__ ((unused)),
int *is_part)
{
const char *p;
char *dname;
char *ret;
struct DosList *dl;
struct DeviceNode *dn;
struct FileSysStartupMsg *fm;
struct DosEnvec *envec;
if (!dev[0])
return xstrdup (dev);
p = dev + strlen (dev) - 1;
if (*p != ':')
return xstrdup (dev);
dname = xmalloc (p - dev + 1);
memcpy (dname, dev, p - dev);
dname[p - dev] = '\0';
dl = LockDosList(LDF_READ);
if (!dl)
{
free (dname);
return xstrdup (dev);
}
dn = (struct DeviceNode *) FindDosEntry (dl, (unsigned char *) dname,
LDF_DEVICES);
UnLockDosList (LDF_READ);
free (dname);
if (!dn)
return xstrdup (dev);
fm = (struct FileSysStartupMsg *) BADDR(dn->dn_Startup);
envec = (struct DosEnvec *) fm->fssm_Environ;
if (envec->de_LowCyl == 0)
return xstrdup (dev);
*is_part = 1;
ret = xasprintf ("%s/%lx/%lx:", fm->fssm_Device, fm->fssm_Unit, (unsigned long) fm->fssm_Flags);
return ret;
}
enum grub_dev_abstraction_types
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
{
return GRUB_DEV_ABSTRACTION_NONE;
}
int
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
{
return 0;
}
char *
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
{
return NULL;
}
grub_disk_addr_t
grub_util_find_partition_start_os (const char *dev)
{
const char *p;
char *dname;
struct DosList *dl;
struct DeviceNode *dn;
struct FileSysStartupMsg *fm;
struct DosEnvec *envec;
if (!dev[0])
return 0;
p = dev + strlen (dev) - 1;
if (*p != ':')
return 0;
dname = xmalloc (p - dev + 1);
memcpy (dname, dev, p - dev);
dname[p - dev] = '\0';
dl = LockDosList(LDF_READ);
if (!dl)
{
free (dname);
return 0;
}
dn = (struct DeviceNode *) FindDosEntry (dl, (unsigned char *) dname,
LDF_DEVICES);
UnLockDosList (LDF_READ);
free (dname);
if (!dn)
return 0;
fm = (struct FileSysStartupMsg *) BADDR(dn->dn_Startup);
envec = (struct DosEnvec *) fm->fssm_Environ;
return (((grub_uint64_t) envec->de_Surfaces
* (grub_uint64_t) envec->de_BlocksPerTrack
* (grub_uint64_t) envec->de_LowCyl)
* (grub_uint64_t) envec->de_SizeBlock) >> 7;
}
char *
grub_make_system_path_relative_to_its_root (const char *path)
{
char *p;
unsigned char *tmp = xmalloc (2048);
char *ret;
BPTR lck;
lck = Lock ((const unsigned char *) path, SHARED_LOCK);
if (!lck || !NameFromLock (lck, tmp, 2040))
{
free (tmp);
tmp = (unsigned char *) xstrdup (path);
}
if (lck)
UnLock (lck);
p = strchr ((char *) tmp, ':');
if (!p)
return (char *) tmp;
if (p[1] == '/' || p[1] == '\0')
{
ret = xstrdup (p + 1);
}
else
{
ret = xmalloc (strlen (p + 1) + 2);
ret[0] = '/';
strcpy (ret + 1, p + 1);
}
free (tmp);
return ret;
}
char **
grub_guess_root_devices (const char *path)
{
char **os_dev = NULL;
struct InfoData id;
BPTR lck;
struct DeviceList *dl;
struct DosList *dl2;
size_t sz;
const char *nm;
lck = Lock ((const unsigned char *) path, SHARED_LOCK);
if (!lck)
grub_util_info ("Lock(%s) failed", path);
if (!lck || !Info (lck, &id))
{
char *p;
if (lck)
UnLock (lck);
grub_util_info ("Info(%s) failed", path);
os_dev = xmalloc (2 * sizeof (os_dev[0]));
sz = strlen (path);
os_dev[0] = xmalloc (sz + 2);
memcpy (os_dev[0], path, sz);
os_dev[0][sz] = ':';
os_dev[0][sz+1] = '\0';
p = strchr (os_dev[0], ':');
*(p + 1) = '\0';
os_dev[1] = NULL;
return os_dev;
}
dl = BADDR (id.id_VolumeNode);
if (!dl->dl_Task)
grub_util_error ("unsupported device %s", dl->dl_Name);
grub_util_info ("dl=%p, dl->dl_Name=%s, dl->dl_Task=%p",
dl, dl->dl_Name,
dl->dl_Task);
for (dl2 = LockDosList(LDF_READ | LDF_DEVICES);
dl2;
dl2 = NextDosEntry (dl2, LDF_DEVICES))
{
if (dl2->dol_Task == dl->dl_Task)
break;
}
if (lck)
UnLock (lck);
if (dl2)
nm = (char *) dl2->dol_Name;
else
nm = (char *) dl->dl_Name;
grub_util_info ("dl2=%p, nm=%s", dl2, nm);
os_dev = xmalloc (2 * sizeof (os_dev[0]));
sz = strlen (nm);
os_dev[0] = xmalloc (sz + 2);
memcpy (os_dev[0], nm, sz);
os_dev[0][sz] = ':';
os_dev[0][sz+1] = '\0';
os_dev[1] = NULL;
UnLockDosList (LDF_READ | LDF_DEVICES);
return os_dev;
}
int
grub_util_biosdisk_is_floppy (grub_disk_t disk)
{
const char *dname, *p;
dname = grub_util_biosdisk_get_osdev (disk);
p = strchr (dname, ':');
if (!p || p[1])
return 0;
if (strncmp (dname, TD_NAME, sizeof (TD_NAME) - 1) == 0
&& (TD_NAME[sizeof (TD_NAME) - 1] == '/'
|| TD_NAME[sizeof (TD_NAME) - 1] == ':'))
return 1;
return 0;
}