msdospart.c 4.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* pcpart.c - manipulate fdisk partitions */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2009  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.
 */

#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
25
#include <grub/msdos_partition.h>
26 27 28 29
#include <grub/device.h>
#include <grub/disk.h>
#include <grub/partition.h>
#include <grub/parttool.h>
30
#include <grub/i18n.h>
31

32 33
GRUB_MOD_LICENSE ("GPLv2+");

34 35 36 37 38
static int activate_table_handle = -1;
static int type_table_handle = -1;

static struct grub_parttool_argdesc grub_pcpart_bootargs[] =
{
39
  {"boot", N_("Make partition active"), GRUB_PARTTOOL_ARG_BOOL},
40 41 42
  {0, 0, 0}
};

43
static grub_err_t grub_pcpart_boot (const grub_device_t dev,
44 45 46 47
				    const struct grub_parttool_args *args)
{
  int i, index;
  grub_partition_t part;
48
  struct grub_msdos_partition_mbr mbr;
49 50

  if (dev->disk->partition->offset)
51
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("not a primary partition"));
52 53 54

  index = dev->disk->partition->index;
  part = dev->disk->partition;
55
  dev->disk->partition = part->parent;
56 57

  /* Read the MBR.  */
58
  if (grub_disk_read (dev->disk, 0, 0, sizeof (mbr), &mbr))
59 60 61 62 63 64 65 66 67 68
    {
      dev->disk->partition = part;
      return grub_errno;
    }

  if (args[0].set && args[0].bool)
    {
      for (i = 0; i < 4; i++)
	mbr.entries[i].flag = 0x0;
      mbr.entries[index].flag = 0x80;
69
      grub_printf_ (N_("Partition %d is active now. \n"), index);
70 71
    }
  else
72 73
    {
      mbr.entries[index].flag = 0x0;
74
      grub_printf (N_("Cleared active flag on %d. \n"), index);
75
    }
76 77

   /* Write the MBR.  */
78
  grub_disk_write (dev->disk, 0, 0, sizeof (mbr), &mbr);
79 80 81 82 83 84 85 86

  dev->disk->partition = part;

  return GRUB_ERR_NONE;
}

static struct grub_parttool_argdesc grub_pcpart_typeargs[] =
{
87
  {"type", N_("Change partition type"), GRUB_PARTTOOL_ARG_VAL},
88
  {"hidden", N_("Set `hidden' flag in partition type"), GRUB_PARTTOOL_ARG_BOOL},
89 90 91
  {0, 0, 0}
};

92
static grub_err_t grub_pcpart_type (const grub_device_t dev,
93 94 95 96 97
				    const struct grub_parttool_args *args)
{
  int index;
  grub_uint8_t type;
  grub_partition_t part;
98
  struct grub_msdos_partition_mbr mbr;
99 100 101

  index = dev->disk->partition->index;
  part = dev->disk->partition;
102
  dev->disk->partition = part->parent;
103 104

  /* Read the parttable.  */
105
  if (grub_disk_read (dev->disk, part->offset, 0,
106
		      sizeof (mbr), &mbr))
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    {
      dev->disk->partition = part;
      return grub_errno;
    }

  if (args[0].set)
    type = grub_strtoul (args[0].str, 0, 0);
  else
    type = mbr.entries[index].type;

  if (args[1].set)
    {
      if (args[1].bool)
	type |= GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG;
      else
	type &= ~GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG;
    }

125 126
  if (grub_msdos_partition_is_empty (type)
      || grub_msdos_partition_is_extended (type))
127 128
    {
      dev->disk->partition = part;
129 130 131
      return grub_error (GRUB_ERR_BAD_ARGUMENT,
			 N_("the partition type 0x%x isn't "
			    "valid"));
132 133 134
    }

  mbr.entries[index].type = type;
135 136
  /* TRANSLATORS: In this case we're actually writing to the disk and actively
     modifying partition type rather than just defining it.  */
137
  grub_printf_ (N_("Setting partition type to 0x%x\n"), type);
138 139

   /* Write the parttable.  */
140
  grub_disk_write (dev->disk, part->offset, 0,
141
		   sizeof (mbr), &mbr);
142 143 144 145 146 147

  dev->disk->partition = part;

  return GRUB_ERR_NONE;
}

148
GRUB_MOD_INIT (msdospart)
149
{
150
  activate_table_handle = grub_parttool_register ("msdos",
151 152
						  grub_pcpart_boot,
						  grub_pcpart_bootargs);
153
  type_table_handle = grub_parttool_register ("msdos",
154 155
					      grub_pcpart_type,
					      grub_pcpart_typeargs);
156

157
}
158
GRUB_MOD_FINI(msdospart)
159 160 161 162
{
  grub_parttool_unregister (activate_table_handle);
  grub_parttool_unregister (type_table_handle);
}