summaryrefslogtreecommitdiffstats
path: root/grub-core/commands/hdparm.c
blob: 2e2319e645a11e3120c9cf8bf1158ff5b80ca455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* 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>
#include <grub/scsi.h>
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/lib/hexdump.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>

GRUB_MOD_LICENSE ("GPLv3+");

static const struct grub_arg_option options[] = {
  {"apm",             'B', 0, N_("Set Advanced Power Management\n"
			      "(1=low, ..., 254=high, 255=off)."),
			      0, ARG_TYPE_INT},
  {"power",           'C', 0, N_("Display power mode."), 0, ARG_TYPE_NONE},
  {"security-freeze", 'F', 0, N_("Freeze ATA security settings until reset."),
			      0, ARG_TYPE_NONE},
  {"health",          'H', 0, N_("Display SMART health status."), 0, ARG_TYPE_NONE},
  {"aam",             'M', 0, N_("Set Automatic Acoustic Management\n"
			      "(0=off, 128=quiet, ..., 254=fast)."),
			      0, ARG_TYPE_INT},
  {"standby-timeout", 'S', 0, N_("Set standby timeout\n"
			      "(0=off, 1=5s, 2=10s, ..., 240=20m, 241=30m, ...)."),
			      0, ARG_TYPE_INT},
  {"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."),
			      0, ARG_TYPE_NONE},
  {"dumpid",          'I', 0, N_("Show raw contents of ATA IDENTIFY sector."),
			       0, ARG_TYPE_NONE},
  {"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},
  {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
grub_hdparm_do_ata_cmd (grub_ata_t ata, grub_uint8_t cmd,
			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));

  apt.taskfile.cmd = cmd;
  apt.taskfile.features = features;
  apt.taskfile.sectors = sectors;
  apt.taskfile.disk = 0xE0;

  apt.buffer = buffer;
  apt.size = size;

  if (ata->dev->readwrite (ata, &apt, 0))
    return grub_errno;

  return GRUB_ERR_NONE;
}

static int
grub_hdparm_do_check_powermode_cmd (grub_ata_t ata)
{
  struct grub_disk_ata_pass_through_parms apt;
  grub_memset (&apt, 0, sizeof (apt));

  apt.taskfile.cmd = GRUB_ATA_CMD_CHECK_POWER_MODE;
  apt.taskfile.disk = 0xE0;

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

  return apt.taskfile.sectors;
}

static int
grub_hdparm_do_smart_cmd (grub_ata_t ata, grub_uint8_t features)
{
  struct grub_disk_ata_pass_through_parms apt;
  grub_memset (&apt, 0, sizeof (apt));

  apt.taskfile.cmd = GRUB_ATA_CMD_SMART;
  apt.taskfile.features = features;
  apt.taskfile.lba_mid  = 0x4f;
  apt.taskfile.lba_high = 0xc2;
  apt.taskfile.disk = 0xE0;

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

  if (features == GRUB_ATA_FEAT_SMART_STATUS)
    {
      if (   apt.taskfile.lba_mid  == 0x4f
          && apt.taskfile.lba_high == 0xc2)
	return 0; /* Good SMART status.  */
      else if (   apt.taskfile.lba_mid  == 0xf4
	       && apt.taskfile.lba_high == 0x2c)
	return 1; /* Bad SMART status.  */
      else
	return -1;
    }
  return 0;
}

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

  grub_err_t err = grub_hdparm_do_ata_cmd (ata, cmd, 0, 0, NULL, 0);

  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,
			 grub_ata_t ata, grub_uint8_t cmd,
			 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);
    }

  grub_err_t err = grub_hdparm_do_ata_cmd (ata, cmd, features, sectors,
					   NULL, 0);

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

static const char *
le16_to_char (grub_uint16_t *dest, const grub_uint16_t * src16, unsigned bytes)
{
  unsigned i;
  for (i = 0; i < bytes / 2; i++)
    dest[i] = grub_swap_bytes16 (src16[i]);
  dest[i] = 0;
  return (char *) dest;
}

static void
grub_hdparm_print_identify (const grub_uint16_t * idw)
{
  /* Print identity strings.  */
  grub_uint16_t tmp[21];
  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
grub_cmd_hdparm (grub_extcmd_context_t ctxt, int argc, char **args)
{
  struct grub_arg_list *state = ctxt->state;
  struct grub_ata *ata;
  const char *diskname;

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

  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];

  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.  */
  grub_disk_t disk = grub_disk_open (diskname);
  if (! disk)
    return grub_errno;

  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;
	}
      /* FALLTHROUGH */
    default:
      grub_disk_close (disk);
      return grub_error (GRUB_ERR_IO, "not an ATA device");
    }
    

  /* Change settings.  */
  if (aam >= 0)
    grub_hdparm_set_val_cmd ("Automatic Acoustic Management", (aam ? aam : -1),
			     ata, GRUB_ATA_CMD_SET_FEATURES,
			     (aam ? 0x42 : 0xc2), aam);

  if (apm >= 0)
    grub_hdparm_set_val_cmd ("Advanced Power Management",
			     (apm != 255 ? apm : -1), ata,
			     GRUB_ATA_CMD_SET_FEATURES,
			     (apm != 255 ? 0x05 : 0x85),
			     (apm != 255 ? apm : 0));

  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.  */
      grub_hdparm_set_val_cmd ("", -1, ata, GRUB_ATA_CMD_IDLE, 0, standby_tout);
    }

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

  if (sec_freeze)
    grub_hdparm_simple_cmd ("Freeze security settings", ata,
                            GRUB_ATA_CMD_SECURITY_FREEZE_LOCK);

  /* Print/dump IDENTIFY.  */
  if (ident || dumpid)
    {
      grub_uint16_t buf[GRUB_DISK_SECTOR_SIZE / 2];
      if (grub_hdparm_do_ata_cmd (ata, GRUB_ATA_CMD_IDENTIFY_DEVICE,
          0, 0, buf, sizeof (buf)))
	grub_printf ("Cannot read ATA IDENTIFY data\n");
      else
	{
	  if (ident)
	    grub_hdparm_print_identify (buf);
	  if (dumpid)
	    hexdump (0, (char *) buf, sizeof (buf));
	}
    }

  /* Check power mode.  */
  if (power)
    {
      grub_printf ("Disk power mode is: ");
      int mode = grub_hdparm_do_check_powermode_cmd (ata);
      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: ");
      int err = grub_hdparm_do_smart_cmd (ata, GRUB_ATA_FEAT_SMART_STATUS);
      if (! quiet)
	grub_printf ("%s\n", (err  < 0 ? "unknown" :
	                      err == 0 ? "OK" : "*BAD*"));
      status = (err > 0);
    }

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

  if (sleep_now)
    grub_hdparm_simple_cmd ("Set disk to sleep mode", ata,
			    GRUB_ATA_CMD_SLEEP);

  grub_disk_close (disk);

  grub_errno = GRUB_ERR_NONE;
  return status;
}

static grub_extcmd_t cmd;

GRUB_MOD_INIT(hdparm)
{
  cmd = grub_register_extcmd_lockdown ("hdparm", grub_cmd_hdparm, 0,
				       N_("[OPTIONS] DISK"),
				       N_("Get/set ATA disk parameters."), options);
}

GRUB_MOD_FINI(hdparm)
{
  grub_unregister_extcmd (cmd);
}