hdparm.c 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* hdparm.c - command to get/set ATA disk parameters.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2009  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 <grub/ata.h>
21
#include <grub/scsi.h>
22 23 24 25 26
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/lib/hexdump.h>
27
#include <grub/extcmd.h>
28
#include <grub/i18n.h>
29

30 31
GRUB_MOD_LICENSE ("GPLv3+");

32
static const struct grub_arg_option options[] = {
33 34
  {"apm",             'B', 0, N_("Set Advanced Power Management\n"
			      "(1=low, ..., 254=high, 255=off)."),
35
			      0, ARG_TYPE_INT},
36
  {"power",           'C', 0, N_("Display power mode."), 0, ARG_TYPE_NONE},
37
  {"security-freeze", 'F', 0, N_("Freeze ATA security settings until reset."),
38
			      0, ARG_TYPE_NONE},
39
  {"health",          'H', 0, N_("Display SMART health status."), 0, ARG_TYPE_NONE},
40 41
  {"aam",             'M', 0, N_("Set Automatic Acoustic Management\n"
			      "(0=off, 128=quiet, ..., 254=fast)."),
42
			      0, ARG_TYPE_INT},
43 44
  {"standby-timeout", 'S', 0, N_("Set standby timeout\n"
			      "(0=off, 1=5s, 2=10s, ..., 240=20m, 241=30m, ...)."),
45
			      0, ARG_TYPE_INT},
46 47 48
  {"standby",         'y', 0, N_("Set drive to standby mode."), 0, ARG_TYPE_NONE},
  {"sleep",           'Y', 0, N_("Set drive to sleep mode."), 0, ARG_TYPE_NONE},
  {"identify",        'i', 0, N_("Print drive identity and settings."),
49
			      0, ARG_TYPE_NONE},
50
  {"dumpid",          'I', 0, N_("Show raw contents of ATA IDENTIFY sector."),
51
			       0, ARG_TYPE_NONE},
52 53
  {"smart",            -1, 0, N_("Disable/enable SMART (0/1)."), 0, ARG_TYPE_INT},
  {"quiet",           'q', 0, N_("Do not print messages."), 0, ARG_TYPE_NONE},
54 55 56 57 58 59 60 61 62 63 64 65 66
  {0, 0, 0, 0, 0, 0}
};

enum grub_ata_smart_commands
  {
    GRUB_ATA_FEAT_SMART_ENABLE  = 0xd8,
    GRUB_ATA_FEAT_SMART_DISABLE = 0xd9,
    GRUB_ATA_FEAT_SMART_STATUS  = 0xda,
  };

static int quiet = 0;

static grub_err_t
67
grub_hdparm_do_ata_cmd (grub_ata_t ata, grub_uint8_t cmd,
68 69 70 71 72 73
			grub_uint8_t features, grub_uint8_t sectors,
			void * buffer, int size)
{
  struct grub_disk_ata_pass_through_parms apt;
  grub_memset (&apt, 0, sizeof (apt));

74 75 76
  apt.taskfile.cmd = cmd;
  apt.taskfile.features = features;
  apt.taskfile.sectors = sectors;
77 78
  apt.taskfile.disk = 0xE0;

79 80 81
  apt.buffer = buffer;
  apt.size = size;

82
  if (ata->dev->readwrite (ata, &apt, 0))
83 84 85 86 87 88
    return grub_errno;

  return GRUB_ERR_NONE;
}

static int
89
grub_hdparm_do_check_powermode_cmd (grub_ata_t ata)
90 91 92 93
{
  struct grub_disk_ata_pass_through_parms apt;
  grub_memset (&apt, 0, sizeof (apt));

94
  apt.taskfile.cmd = GRUB_ATA_CMD_CHECK_POWER_MODE;
95
  apt.taskfile.disk = 0xE0;
96

97
  if (ata->dev->readwrite (ata, &apt, 0))
98 99
    return -1;

100
  return apt.taskfile.sectors;
101 102 103
}

static int
104
grub_hdparm_do_smart_cmd (grub_ata_t ata, grub_uint8_t features)
105 106 107 108
{
  struct grub_disk_ata_pass_through_parms apt;
  grub_memset (&apt, 0, sizeof (apt));

109 110 111 112
  apt.taskfile.cmd = GRUB_ATA_CMD_SMART;
  apt.taskfile.features = features;
  apt.taskfile.lba_mid  = 0x4f;
  apt.taskfile.lba_high = 0xc2;
113
  apt.taskfile.disk = 0xE0;
114

115
  if (ata->dev->readwrite (ata, &apt, 0))
116 117 118 119
    return -1;

  if (features == GRUB_ATA_FEAT_SMART_STATUS)
    {
120 121
      if (   apt.taskfile.lba_mid  == 0x4f
          && apt.taskfile.lba_high == 0xc2)
122
	return 0; /* Good SMART status.  */
123 124
      else if (   apt.taskfile.lba_mid  == 0xf4
	       && apt.taskfile.lba_high == 0x2c)
125 126 127 128 129 130 131 132 133
	return 1; /* Bad SMART status.  */
      else
	return -1;
    }
  return 0;
}

static grub_err_t
grub_hdparm_simple_cmd (const char * msg,
134
			grub_ata_t ata, grub_uint8_t cmd)
135 136 137 138
{
  if (! quiet && msg)
    grub_printf ("%s", msg);

139
  grub_err_t err = grub_hdparm_do_ata_cmd (ata, cmd, 0, 0, NULL, 0);
140 141 142 143 144 145 146 147

  if (! quiet && msg)
    grub_printf ("%s\n", ! err ? "" : ": not supported");
  return err;
}

static grub_err_t
grub_hdparm_set_val_cmd (const char * msg, int val,
148
			 grub_ata_t ata, grub_uint8_t cmd,
149 150 151 152 153 154 155 156 157 158
			 grub_uint8_t features, grub_uint8_t sectors)
{
  if (! quiet && msg && *msg)
    {
      if (val >= 0)
	grub_printf ("Set %s to %d", msg, val);
      else
	grub_printf ("Disable %s", msg);
    }

159
  grub_err_t err = grub_hdparm_do_ata_cmd (ata, cmd, features, sectors,
160 161 162 163 164 165 166 167
					   NULL, 0);

  if (! quiet && msg)
    grub_printf ("%s\n", ! err ? "" : ": not supported");
  return err;
}

static const char *
168
le16_to_char (grub_uint16_t *dest, const grub_uint16_t * src16, unsigned bytes)
169 170 171
{
  unsigned i;
  for (i = 0; i < bytes / 2; i++)
172
    dest[i] = grub_swap_bytes16 (src16[i]);
173 174
  dest[i] = 0;
  return (char *) dest;
175 176 177
}

static void
178
grub_hdparm_print_identify (const grub_uint16_t * idw)
179 180
{
  /* Print identity strings.  */
181
  grub_uint16_t tmp[21];
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  grub_printf ("Model:    \"%.40s\"\n", le16_to_char (tmp, &idw[27], 40));
  grub_printf ("Firmware: \"%.8s\"\n",  le16_to_char (tmp, &idw[23], 8));
  grub_printf ("Serial:   \"%.20s\"\n", le16_to_char (tmp, &idw[10], 20));

  /* Print AAM, APM and SMART settings.  */
  grub_uint16_t features1 = grub_le_to_cpu16 (idw[82]);
  grub_uint16_t features2 = grub_le_to_cpu16 (idw[83]);
  grub_uint16_t enabled1  = grub_le_to_cpu16 (idw[85]);
  grub_uint16_t enabled2  = grub_le_to_cpu16 (idw[86]);

  grub_printf ("Automatic Acoustic Management: ");
  if (features2 & 0x0200)
    {
      if (enabled2 & 0x0200)
	{
	  grub_uint16_t aam = grub_le_to_cpu16 (idw[94]);
	  grub_printf ("%u (128=quiet, ..., 254=fast, recommended=%u)\n",
		       aam & 0xff, (aam >> 8) & 0xff);
	}
      else
	grub_printf ("disabled\n");
    }
  else
    grub_printf ("not supported\n");

  grub_printf ("Advanced Power Management: ");
  if (features2 & 0x0008)
    {
      if (enabled2 & 0x0008)
	grub_printf ("%u (1=low, ..., 254=high)\n",
		     grub_le_to_cpu16 (idw[91]) & 0xff);
      else
	grub_printf ("disabled\n");
    }
  else
    grub_printf ("not supported\n");

  grub_printf ("SMART Feature Set: ");
  if (features1 & 0x0001)
    grub_printf ("%sabled\n", (enabled1 & 0x0001 ? "en" : "dis"));
  else
    grub_printf ("not supported\n");

  /* Print security settings.  */
  grub_uint16_t security = grub_le_to_cpu16 (idw[128]);

  grub_printf ("ATA Security: ");
  if (security & 0x0001)
    grub_printf ("%s, %s, %s, %s\n",
		 (security & 0x0002 ? "ENABLED" : "disabled"),
		 (security & 0x0004 ? "**LOCKED**"  : "not locked"),
		 (security & 0x0008 ? "frozen" : "NOT FROZEN"),
		 (security & 0x0010 ? "COUNT EXPIRED" : "count not expired"));
  else
    grub_printf ("not supported\n");
}

static void
grub_hdparm_print_standby_tout (int timeout)
{
  if (timeout == 0)
    grub_printf ("off");
  else if (timeout <= 252 || timeout == 255)
    {
      int h = 0, m = 0 , s = 0;
      if (timeout == 255)
	{
	  m = 21;
	  s = 15;
	}
      else if (timeout == 252)
	m = 21;
      else if (timeout <= 240)
	{
	  s = timeout * 5;
	  m = s / 60;
	  s %= 60;
	}
      else
	{
	  m = (timeout - 240) * 30;
	  h  = m / 60;
	  m %= 60;
	}
      grub_printf ("%02d:%02d:%02d", h, m, s);
    }
  else
    grub_printf ("invalid or vendor-specific");
}

static int get_int_arg (const struct grub_arg_list *state)
{
  return (state->set ? (int)grub_strtoul (state->arg, 0, 0) : -1);
}

static grub_err_t
278
grub_cmd_hdparm (grub_extcmd_context_t ctxt, int argc, char **args)
279
{
280
  struct grub_arg_list *state = ctxt->state;
281
  struct grub_ata *ata;
282
  const char *diskname;
283

284 285
  /* Check command line.  */
  if (argc != 1)
286
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
287

288 289 290 291 292 293 294 295 296
  if (args[0][0] == '(')
    {
      grub_size_t len = grub_strlen (args[0]);
      if (args[0][len - 1] == ')')
	args[0][len - 1] = 0;
      diskname = &args[0][1];
    }
  else
    diskname = &args[0][0];
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

  int i = 0;
  int apm          = get_int_arg (&state[i++]);
  int power        = state[i++].set;
  int sec_freeze   = state[i++].set;
  int health       = state[i++].set;
  int aam          = get_int_arg (&state[i++]);
  int standby_tout = get_int_arg (&state[i++]);
  int standby_now  = state[i++].set;
  int sleep_now    = state[i++].set;
  int ident        = state[i++].set;
  int dumpid       = state[i++].set;
  int enable_smart = get_int_arg (&state[i++]);
  quiet            = state[i++].set;

  /* Open disk.  */
313
  grub_disk_t disk = grub_disk_open (diskname);
314 315 316
  if (! disk)
    return grub_errno;

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  switch (disk->dev->id)
    {
    case GRUB_DISK_DEVICE_ATA_ID:
      ata = disk->data;
      break;
    case GRUB_DISK_DEVICE_SCSI_ID:
      if (((disk->id >> GRUB_SCSI_ID_SUBSYSTEM_SHIFT) & 0xFF)
	  == GRUB_SCSI_SUBSYSTEM_PATA
	  || (((disk->id >> GRUB_SCSI_ID_SUBSYSTEM_SHIFT) & 0xFF)
	      == GRUB_SCSI_SUBSYSTEM_AHCI))
	{
	  ata = ((struct grub_scsi *) disk->data)->data;
	  break;
	}
    default:
332
      grub_disk_close (disk);
333 334 335 336
      return grub_error (GRUB_ERR_IO, "not an ATA device");
    }
    

337 338 339
  /* Change settings.  */
  if (aam >= 0)
    grub_hdparm_set_val_cmd ("Automatic Acoustic Management", (aam ? aam : -1),
340 341
			     ata, GRUB_ATA_CMD_SET_FEATURES,
			     (aam ? 0x42 : 0xc2), aam);
342 343 344

  if (apm >= 0)
    grub_hdparm_set_val_cmd ("Advanced Power Management",
345 346 347 348
			     (apm != 255 ? apm : -1), ata,
			     GRUB_ATA_CMD_SET_FEATURES,
			     (apm != 255 ? 0x05 : 0x85),
			     (apm != 255 ? apm : 0));
349 350 351 352 353 354 355 356 357 358

  if (standby_tout >= 0)
    {
      if (! quiet)
	{
	  grub_printf ("Set standby timeout to %d (", standby_tout);
	  grub_hdparm_print_standby_tout (standby_tout);
	  grub_printf (")");
	}
      /* The IDLE cmd sets disk to idle mode and configures standby timer.  */
359
      grub_hdparm_set_val_cmd ("", -1, ata, GRUB_ATA_CMD_IDLE, 0, standby_tout);
360 361 362 363 364 365
    }

  if (enable_smart >= 0)
    {
      if (! quiet)
	grub_printf ("%sable SMART operations", (enable_smart ? "En" : "Dis"));
366
      int err = grub_hdparm_do_smart_cmd (ata, (enable_smart ?
367 368 369 370 371 372
	          GRUB_ATA_FEAT_SMART_ENABLE : GRUB_ATA_FEAT_SMART_DISABLE));
      if (! quiet)
	grub_printf ("%s\n", err ? ": not supported" : "");
    }

  if (sec_freeze)
373
    grub_hdparm_simple_cmd ("Freeze security settings", ata,
374 375 376 377 378
                            GRUB_ATA_CMD_SECURITY_FREEZE_LOCK);

  /* Print/dump IDENTIFY.  */
  if (ident || dumpid)
    {
379
      grub_uint16_t buf[GRUB_DISK_SECTOR_SIZE / 2];
380
      if (grub_hdparm_do_ata_cmd (ata, GRUB_ATA_CMD_IDENTIFY_DEVICE,
381 382 383 384 385 386 387
          0, 0, buf, sizeof (buf)))
	grub_printf ("Cannot read ATA IDENTIFY data\n");
      else
	{
	  if (ident)
	    grub_hdparm_print_identify (buf);
	  if (dumpid)
388
	    hexdump (0, (char *) buf, sizeof (buf));
389 390 391 392 393 394 395
	}
    }

  /* Check power mode.  */
  if (power)
    {
      grub_printf ("Disk power mode is: ");
396
      int mode = grub_hdparm_do_check_powermode_cmd (ata);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
      if (mode < 0)
        grub_printf ("unknown\n");
      else
	grub_printf ("%s (0x%02x)\n",
		     (mode == 0xff ? "active/idle" :
		      mode == 0x80 ? "idle" :
		      mode == 0x00 ? "standby" : "unknown"), mode);
    }

  /* Check health.  */
  int status = 0;
  if (health)
    {
      if (! quiet)
	grub_printf ("SMART status is: ");
412
      int err = grub_hdparm_do_smart_cmd (ata, GRUB_ATA_FEAT_SMART_STATUS);
413 414 415 416 417 418 419 420
      if (! quiet)
	grub_printf ("%s\n", (err  < 0 ? "unknown" :
	                      err == 0 ? "OK" : "*BAD*"));
      status = (err > 0);
    }

  /* Change power mode.  */
  if (standby_now)
421
    grub_hdparm_simple_cmd ("Set disk to standby mode", ata,
422 423 424
			    GRUB_ATA_CMD_STANDBY_IMMEDIATE);

  if (sleep_now)
425
    grub_hdparm_simple_cmd ("Set disk to sleep mode", ata,
426 427 428 429 430 431 432 433
			    GRUB_ATA_CMD_SLEEP);

  grub_disk_close (disk);

  grub_errno = GRUB_ERR_NONE;
  return status;
}

434
static grub_extcmd_t cmd;
435 436 437

GRUB_MOD_INIT(hdparm)
{
438
  cmd = grub_register_extcmd ("hdparm", grub_cmd_hdparm, 0,
439 440
			      N_("[OPTIONS] DISK"),
			      N_("Get/set ATA disk parameters."), options);
441 442 443 444
}

GRUB_MOD_FINI(hdparm)
{
445
  grub_unregister_extcmd (cmd);
446
}