⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 device.c

📁 Linux的启动装载(BootLoader)程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* device.c - Some helper functions for OS devices and BIOS drives *//* *  GRUB  --  GRand Unified Bootloader *  Copyright (C) 1999,2000,2001,2002,2003  Free Software Foundation, Inc. * *  This program 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 2 of the License, or *  (at your option) any later version. * *  This program 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 this program; if not, write to the Free Software *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* Try to use glibc's transparant LFS support. */#define _LARGEFILE_SOURCE       1/* lseek becomes synonymous with lseek64.  */#define _FILE_OFFSET_BITS       64#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>#include <stdarg.h>#ifdef __linux__# if !defined(__GLIBC__) || \        ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1)))/* Maybe libc doesn't have large file support.  */#  include <linux/unistd.h>     /* _llseek */# endif /* (GLIBC < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR < 1)) */# include <sys/ioctl.h>		/* ioctl */# ifndef HDIO_GETGEO#  define HDIO_GETGEO	0x0301	/* get device geometry *//* If HDIO_GETGEO is not defined, it is unlikely that hd_geometry is   defined.  */struct hd_geometry{  unsigned char heads;  unsigned char sectors;  unsigned short cylinders;  unsigned long start;};# endif /* ! HDIO_GETGEO */# ifndef FLOPPY_MAJOR#  define FLOPPY_MAJOR	2	/* the major number for floppy */# endif /* ! FLOPPY_MAJOR */# ifndef MAJOR#  define MAJOR(dev)	\  ({ \     unsigned long long __dev = (dev); \     (unsigned) ((__dev >> 8) & 0xfff) \                 | ((unsigned int) (__dev >> 32) & ~0xfff); \  })# endif /* ! MAJOR */# ifndef CDROM_GET_CAPABILITY#  define CDROM_GET_CAPABILITY	0x5331	/* get capabilities */# endif /* ! CDROM_GET_CAPABILITY */# ifndef BLKGETSIZE#  define BLKGETSIZE	_IO(0x12,96)	/* return device size */# endif /* ! BLKGETSIZE */#endif /* __linux__ */#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)# include <sys/ioctl.h>		/* ioctl */# include <sys/disklabel.h># include <sys/cdio.h>		/* CDIOCCLRDEBUG */#endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ */#ifdef HAVE_OPENDISK# include <util.h>#endif /* HAVE_OPENDISK */#define WITHOUT_LIBC_STUBS	1#include <shared.h>#include <device.h>/* Get the geometry of a drive DRIVE.  */voidget_drive_geometry (struct geometry *geom, char **map, int drive){  int fd;  fd = open (map[drive], O_RDONLY);  assert (fd >= 0);#if defined(__linux__)  /* Linux */  {    struct hd_geometry hdg;    unsigned long nr;        if (ioctl (fd, HDIO_GETGEO, &hdg))      goto fail;    if (ioctl (fd, BLKGETSIZE, &nr))      goto fail;        /* Got the geometry, so save it. */    geom->cylinders = hdg.cylinders;    geom->heads = hdg.heads;    geom->sectors = hdg.sectors;    geom->total_sectors = nr;        close (fd);    return;  }#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)  /* FreeBSD, NetBSD or OpenBSD */  {    struct disklabel hdg;    if (ioctl (fd, DIOCGDINFO, &hdg))      goto fail;        geom->cylinders = hdg.d_ncylinders;    geom->heads = hdg.d_ntracks;    geom->sectors = hdg.d_nsectors;    geom->total_sectors = hdg.d_secperunit;    close (fd);    return;  }  #else  /* Notably, defined(__GNU__) */# warning "Automatic detection of geometries will be performed only \partially. This is not fatal."#endif fail:  {    struct stat st;    /* FIXME: It would be nice to somehow compute fake C/H/S settings,       given a proper st_blocks size. */    if (drive & 0x80)      {	geom->cylinders = DEFAULT_HD_CYLINDERS;	geom->heads = DEFAULT_HD_HEADS;	geom->sectors = DEFAULT_HD_SECTORS;      }    else      {	geom->cylinders = DEFAULT_FD_CYLINDERS;	geom->heads = DEFAULT_FD_HEADS;	geom->sectors = DEFAULT_FD_SECTORS;      }    /* Set the total sectors properly, if we can. */    if (! fstat (fd, &st) && st.st_blocks)      geom->total_sectors = st.st_blocks;    else      geom->total_sectors = geom->cylinders * geom->heads * geom->sectors;  }  close (fd);}#ifdef __linux__/* Check if we have devfs support.  */static inthave_devfs (void){  static int dev_devfsd_exists = -1;    if (dev_devfsd_exists < 0)    {      struct stat st;            dev_devfsd_exists = stat ("/dev/.devfsd", &st) == 0;    }    return dev_devfsd_exists;}#endif /* __linux__ *//* These three functions are quite different among OSes.  */static voidget_floppy_disk_name (char *name, int unit){#if defined(__linux__)  /* GNU/Linux */  if (have_devfs ())    sprintf (name, "/dev/floppy/%d", unit);  else    sprintf (name, "/dev/fd%d", unit);#elif defined(__GNU__)  /* GNU/Hurd */  sprintf (name, "/dev/fd%d", unit);#elif defined(__FreeBSD__)  /* FreeBSD */  sprintf (name, "/dev/rfd%d", unit);#elif defined(__NetBSD__)  /* NetBSD */  /* opendisk() doesn't work for floppies.  */  sprintf (name, "/dev/rfd%da", unit);#elif defined(__OpenBSD__)  /* OpenBSD */  sprintf (name, "/dev/rfd%dc", unit);#elif defined(__QNXNTO__)  /* QNX RTP */  sprintf (name, "/dev/fd%d", unit);#else# warning "BIOS floppy drives cannot be guessed in your operating system."  /* Set NAME to a bogus string.  */  *name = 0;#endif}static voidget_ide_disk_name (char *name, int unit){#if defined(__linux__)  /* GNU/Linux */  sprintf (name, "/dev/hd%c", unit + 'a');#elif defined(__GNU__)  /* GNU/Hurd */  sprintf (name, "/dev/hd%d", unit);#elif defined(__FreeBSD__)  /* FreeBSD */# if __FreeBSD__ >= 4  sprintf (name, "/dev/rad%d", unit);# else /* __FreeBSD__ <= 3 */  sprintf (name, "/dev/rwd%d", unit);# endif /* __FreeBSD__ <= 3 */#elif defined(__NetBSD__) && defined(HAVE_OPENDISK)  /* NetBSD */  char shortname[16];  int fd;    sprintf (shortname, "wd%d", unit);  fd = opendisk (shortname, O_RDONLY, name,		 16,	/* length of NAME */		 0	/* char device */		 );  close (fd);#elif defined(__OpenBSD__)  /* OpenBSD */  sprintf (name, "/dev/rwd%dc", unit);#elif defined(__QNXNTO__)  /* QNX RTP */  /* Actually, QNX RTP doesn't distinguish IDE from SCSI, so this could     contain SCSI disks.  */  sprintf (name, "/dev/hd%d", unit);#else# warning "BIOS IDE drives cannot be guessed in your operating system."  /* Set NAME to a bogus string.  */  *name = 0;#endif}static voidget_scsi_disk_name (char *name, int unit){#if defined(__linux__)  /* GNU/Linux */  sprintf (name, "/dev/sd%c", unit + 'a');#elif defined(__GNU__)  /* GNU/Hurd */  sprintf (name, "/dev/sd%d", unit);#elif defined(__FreeBSD__)  /* FreeBSD */  sprintf (name, "/dev/rda%d", unit);#elif defined(__NetBSD__) && defined(HAVE_OPENDISK)  /* NetBSD */  char shortname[16];  int fd;    sprintf (shortname, "sd%d", unit);  fd = opendisk (shortname, O_RDONLY, name,		 16,	/* length of NAME */		 0	/* char device */		 );  close (fd);#elif defined(__OpenBSD__)  /* OpenBSD */  sprintf (name, "/dev/rsd%dc", unit);#elif defined(__QNXNTO__)  /* QNX RTP */  /* QNX RTP doesn't distinguish SCSI from IDE, so it is better to     disable the detection of SCSI disks here.  */  *name = 0;#else# warning "BIOS SCSI drives cannot be guessed in your operating system."  /* Set NAME to a bogus string.  */  *name = 0;#endif}#ifdef __linux__static voidget_dac960_disk_name (char *name, int controller, int drive){  sprintf (name, "/dev/rd/c%dd%d", controller, drive);}#endif/* Check if DEVICE can be read. If an error occurs, return zero,   otherwise return non-zero.  */intcheck_device (const char *device){  char buf[512];  FILE *fp;  /* If DEVICE is empty, just return 1.  */  if (*device == 0)    return 1;    fp = fopen (device, "r");  if (! fp)    {      switch (errno)	{#ifdef ENOMEDIUM	case ENOMEDIUM:# if 0	  /* At the moment, this finds only CDROMs, which can't be	     read anyway, so leave it out. Code should be	     reactivated if `removable disks' and CDROMs are	     supported.  */	  /* Accept it, it may be inserted.  */	  return 1;# endif	  break;#endif /* ENOMEDIUM */	default:	  /* Break case and leave.  */	  break;	}      /* Error opening the device.  */      return 0;    }    /* Make sure CD-ROMs don't get assigned a BIOS disk number      before SCSI disks!  */#ifdef __linux__# ifdef CDROM_GET_CAPABILITY  if (ioctl (fileno (fp), CDROM_GET_CAPABILITY, 0) >= 0)    return 0;# else /* ! CDROM_GET_CAPABILITY */  /* Check if DEVICE is a CD-ROM drive by the HDIO_GETGEO ioctl.  */  {    struct hd_geometry hdg;    struct stat st;    if (fstat (fileno (fp), &st))      return 0;    /* If it is a block device and isn't a floppy, check if HDIO_GETGEO       succeeds.  */    if (S_ISBLK (st.st_mode)	&& MAJOR (st.st_rdev) != FLOPPY_MAJOR	&& ioctl (fileno (fp), HDIO_GETGEO, &hdg))      return 0;  }# endif /* ! CDROM_GET_CAPABILITY */#endif /* __linux__ */#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)# ifdef CDIOCCLRDEBUG  if (ioctl (fileno (fp), CDIOCCLRDEBUG, 0) >= 0)    return 0;# endif /* CDIOCCLRDEBUG */#endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ */    /* Attempt to read the first sector.  */  if (fread (buf, 1, 512, fp) != 512)    {      fclose (fp);      return 0;    }    fclose (fp);  return 1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -