hdparm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* hdparm.c - command to get/set ATA disk parameters. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/ata.h>
  20. #include <grub/disk.h>
  21. #include <grub/dl.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/lib.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/i18n.h>
  27. static const struct grub_arg_option options[] = {
  28. {"apm", 'B', 0, N_("Set Advanced Power Management\n"
  29. "(1=low, ..., 254=high, 255=off)."),
  30. 0, ARG_TYPE_INT},
  31. {"power", 'C', 0, N_("Check power mode."), 0, ARG_TYPE_NONE},
  32. {"security-freeze", 'F', 0, N_("Freeze ATA security settings until reset."),
  33. 0, ARG_TYPE_NONE},
  34. {"health", 'H', 0, N_("Check SMART health status."), 0, ARG_TYPE_NONE},
  35. {"aam", 'M', 0, N_("Set Automatic Acoustic Management\n"
  36. "(0=off, 128=quiet, ..., 254=fast)."),
  37. 0, ARG_TYPE_INT},
  38. {"standby-timeout", 'S', 0, N_("Set standby timeout\n"
  39. "(0=off, 1=5s, 2=10s, ..., 240=20m, 241=30m, ...)."),
  40. 0, ARG_TYPE_INT},
  41. {"standby", 'y', 0, N_("Set drive to standby mode."), 0, ARG_TYPE_NONE},
  42. {"sleep", 'Y', 0, N_("Set drive to sleep mode."), 0, ARG_TYPE_NONE},
  43. {"identify", 'i', 0, N_("Print drive identity and settings."),
  44. 0, ARG_TYPE_NONE},
  45. {"dumpid", 'I', 0, N_("Dump contents of ATA IDENTIFY sector."),
  46. 0, ARG_TYPE_NONE},
  47. {"smart", -1, 0, N_("Disable/enable SMART (0/1)."), 0, ARG_TYPE_INT},
  48. {"quiet", 'q', 0, N_("Do not print messages."), 0, ARG_TYPE_NONE},
  49. {0, 0, 0, 0, 0, 0}
  50. };
  51. enum grub_ata_smart_commands
  52. {
  53. GRUB_ATA_FEAT_SMART_ENABLE = 0xd8,
  54. GRUB_ATA_FEAT_SMART_DISABLE = 0xd9,
  55. GRUB_ATA_FEAT_SMART_STATUS = 0xda,
  56. };
  57. static int quiet = 0;
  58. static grub_err_t
  59. grub_hdparm_do_ata_cmd (grub_disk_t disk, grub_uint8_t cmd,
  60. grub_uint8_t features, grub_uint8_t sectors,
  61. void * buffer, int size)
  62. {
  63. struct grub_disk_ata_pass_through_parms apt;
  64. grub_memset (&apt, 0, sizeof (apt));
  65. apt.taskfile[GRUB_ATA_REG_CMD] = cmd;
  66. apt.taskfile[GRUB_ATA_REG_FEATURES] = features;
  67. apt.taskfile[GRUB_ATA_REG_SECTORS] = sectors;
  68. apt.buffer = buffer;
  69. apt.size = size;
  70. if (grub_disk_ata_pass_through (disk, &apt))
  71. return grub_errno;
  72. return GRUB_ERR_NONE;
  73. }
  74. static int
  75. grub_hdparm_do_check_powermode_cmd (grub_disk_t disk)
  76. {
  77. struct grub_disk_ata_pass_through_parms apt;
  78. grub_memset (&apt, 0, sizeof (apt));
  79. apt.taskfile[GRUB_ATA_REG_CMD] = GRUB_ATA_CMD_CHECK_POWER_MODE;
  80. if (grub_disk_ata_pass_through (disk, &apt))
  81. return -1;
  82. return apt.taskfile[GRUB_ATA_REG_SECTORS];
  83. }
  84. static int
  85. grub_hdparm_do_smart_cmd (grub_disk_t disk, grub_uint8_t features)
  86. {
  87. struct grub_disk_ata_pass_through_parms apt;
  88. grub_memset (&apt, 0, sizeof (apt));
  89. apt.taskfile[GRUB_ATA_REG_CMD] = GRUB_ATA_CMD_SMART;
  90. apt.taskfile[GRUB_ATA_REG_FEATURES] = features;
  91. apt.taskfile[GRUB_ATA_REG_LBAMID] = 0x4f;
  92. apt.taskfile[GRUB_ATA_REG_LBAHIGH] = 0xc2;
  93. if (grub_disk_ata_pass_through (disk, &apt))
  94. return -1;
  95. if (features == GRUB_ATA_FEAT_SMART_STATUS)
  96. {
  97. if ( apt.taskfile[GRUB_ATA_REG_LBAMID] == 0x4f
  98. && apt.taskfile[GRUB_ATA_REG_LBAHIGH] == 0xc2)
  99. return 0; /* Good SMART status. */
  100. else if ( apt.taskfile[GRUB_ATA_REG_LBAMID] == 0xf4
  101. && apt.taskfile[GRUB_ATA_REG_LBAHIGH] == 0x2c)
  102. return 1; /* Bad SMART status. */
  103. else
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. static grub_err_t
  109. grub_hdparm_simple_cmd (const char * msg,
  110. grub_disk_t disk, grub_uint8_t cmd)
  111. {
  112. if (! quiet && msg)
  113. grub_printf ("%s", msg);
  114. grub_err_t err = grub_hdparm_do_ata_cmd (disk, cmd, 0, 0, NULL, 0);
  115. if (! quiet && msg)
  116. grub_printf ("%s\n", ! err ? "" : ": not supported");
  117. return err;
  118. }
  119. static grub_err_t
  120. grub_hdparm_set_val_cmd (const char * msg, int val,
  121. grub_disk_t disk, grub_uint8_t cmd,
  122. grub_uint8_t features, grub_uint8_t sectors)
  123. {
  124. if (! quiet && msg && *msg)
  125. {
  126. if (val >= 0)
  127. grub_printf ("Set %s to %d", msg, val);
  128. else
  129. grub_printf ("Disable %s", msg);
  130. }
  131. grub_err_t err = grub_hdparm_do_ata_cmd (disk, cmd, features, sectors,
  132. NULL, 0);
  133. if (! quiet && msg)
  134. grub_printf ("%s\n", ! err ? "" : ": not supported");
  135. return err;
  136. }
  137. static const char *
  138. le16_to_char (char *dest, const grub_uint16_t * src16, unsigned bytes)
  139. {
  140. grub_uint16_t * dest16 = (grub_uint16_t *) dest;
  141. unsigned i;
  142. for (i = 0; i < bytes / 2; i++)
  143. dest16[i] = grub_be_to_cpu16 (src16[i]);
  144. return dest;
  145. }
  146. static void
  147. grub_hdparm_print_identify (const char * idbuf)
  148. {
  149. const grub_uint16_t * idw = (const grub_uint16_t *) idbuf;
  150. /* Print identity strings. */
  151. char tmp[40];
  152. grub_printf ("Model: \"%.40s\"\n", le16_to_char (tmp, &idw[27], 40));
  153. grub_printf ("Firmware: \"%.8s\"\n", le16_to_char (tmp, &idw[23], 8));
  154. grub_printf ("Serial: \"%.20s\"\n", le16_to_char (tmp, &idw[10], 20));
  155. /* Print AAM, APM and SMART settings. */
  156. grub_uint16_t features1 = grub_le_to_cpu16 (idw[82]);
  157. grub_uint16_t features2 = grub_le_to_cpu16 (idw[83]);
  158. grub_uint16_t enabled1 = grub_le_to_cpu16 (idw[85]);
  159. grub_uint16_t enabled2 = grub_le_to_cpu16 (idw[86]);
  160. grub_printf ("Automatic Acoustic Management: ");
  161. if (features2 & 0x0200)
  162. {
  163. if (enabled2 & 0x0200)
  164. {
  165. grub_uint16_t aam = grub_le_to_cpu16 (idw[94]);
  166. grub_printf ("%u (128=quiet, ..., 254=fast, recommended=%u)\n",
  167. aam & 0xff, (aam >> 8) & 0xff);
  168. }
  169. else
  170. grub_printf ("disabled\n");
  171. }
  172. else
  173. grub_printf ("not supported\n");
  174. grub_printf ("Advanced Power Management: ");
  175. if (features2 & 0x0008)
  176. {
  177. if (enabled2 & 0x0008)
  178. grub_printf ("%u (1=low, ..., 254=high)\n",
  179. grub_le_to_cpu16 (idw[91]) & 0xff);
  180. else
  181. grub_printf ("disabled\n");
  182. }
  183. else
  184. grub_printf ("not supported\n");
  185. grub_printf ("SMART Feature Set: ");
  186. if (features1 & 0x0001)
  187. grub_printf ("%sabled\n", (enabled1 & 0x0001 ? "en" : "dis"));
  188. else
  189. grub_printf ("not supported\n");
  190. /* Print security settings. */
  191. grub_uint16_t security = grub_le_to_cpu16 (idw[128]);
  192. grub_printf ("ATA Security: ");
  193. if (security & 0x0001)
  194. grub_printf ("%s, %s, %s, %s\n",
  195. (security & 0x0002 ? "ENABLED" : "disabled"),
  196. (security & 0x0004 ? "**LOCKED**" : "not locked"),
  197. (security & 0x0008 ? "frozen" : "NOT FROZEN"),
  198. (security & 0x0010 ? "COUNT EXPIRED" : "count not expired"));
  199. else
  200. grub_printf ("not supported\n");
  201. }
  202. static void
  203. grub_hdparm_print_standby_tout (int timeout)
  204. {
  205. if (timeout == 0)
  206. grub_printf ("off");
  207. else if (timeout <= 252 || timeout == 255)
  208. {
  209. int h = 0, m = 0 , s = 0;
  210. if (timeout == 255)
  211. {
  212. m = 21;
  213. s = 15;
  214. }
  215. else if (timeout == 252)
  216. m = 21;
  217. else if (timeout <= 240)
  218. {
  219. s = timeout * 5;
  220. m = s / 60;
  221. s %= 60;
  222. }
  223. else
  224. {
  225. m = (timeout - 240) * 30;
  226. h = m / 60;
  227. m %= 60;
  228. }
  229. grub_printf ("%02d:%02d:%02d", h, m, s);
  230. }
  231. else
  232. grub_printf ("invalid or vendor-specific");
  233. }
  234. static int get_int_arg (const struct grub_arg_list *state)
  235. {
  236. return (state->set ? (int)grub_strtoul (state->arg, 0, 0) : -1);
  237. }
  238. static grub_err_t
  239. grub_cmd_hdparm (grub_extcmd_t cmd, int argc, char **args) // state????
  240. {
  241. struct grub_arg_list *state = cmd->state;
  242. /* Check command line. */
  243. if (argc != 1)
  244. return grub_error (GRUB_ERR_BAD_ARGUMENT, "missing device name argument");
  245. grub_size_t len = grub_strlen (args[0]);
  246. if (! (args[0][0] == '(' && args[0][len - 1] == ')'))
  247. return grub_error (GRUB_ERR_BAD_ARGUMENT, "argument is not a device name");
  248. args[0][len - 1] = 0;
  249. if (! grub_disk_ata_pass_through)
  250. return grub_error (GRUB_ERR_BAD_ARGUMENT, "ATA pass through not available");
  251. int i = 0;
  252. int apm = get_int_arg (&state[i++]);
  253. int power = state[i++].set;
  254. int sec_freeze = state[i++].set;
  255. int health = state[i++].set;
  256. int aam = get_int_arg (&state[i++]);
  257. int standby_tout = get_int_arg (&state[i++]);
  258. int standby_now = state[i++].set;
  259. int sleep_now = state[i++].set;
  260. int ident = state[i++].set;
  261. int dumpid = state[i++].set;
  262. int enable_smart = get_int_arg (&state[i++]);
  263. quiet = state[i++].set;
  264. /* Open disk. */
  265. grub_disk_t disk = grub_disk_open (&args[0][1]);
  266. if (! disk)
  267. return grub_errno;
  268. if (disk->partition)
  269. {
  270. grub_disk_close (disk);
  271. return grub_error (GRUB_ERR_BAD_ARGUMENT, "partition not allowed");
  272. }
  273. /* Change settings. */
  274. if (aam >= 0)
  275. grub_hdparm_set_val_cmd ("Automatic Acoustic Management", (aam ? aam : -1),
  276. disk, GRUB_ATA_CMD_SET_FEATURES, (aam ? 0x42 : 0xc2), aam);
  277. if (apm >= 0)
  278. grub_hdparm_set_val_cmd ("Advanced Power Management",
  279. (apm != 255 ? apm : -1), disk, GRUB_ATA_CMD_SET_FEATURES,
  280. (apm != 255 ? 0x05 : 0x85), (apm != 255 ? apm : 0));
  281. if (standby_tout >= 0)
  282. {
  283. if (! quiet)
  284. {
  285. grub_printf ("Set standby timeout to %d (", standby_tout);
  286. grub_hdparm_print_standby_tout (standby_tout);
  287. grub_printf (")");
  288. }
  289. /* The IDLE cmd sets disk to idle mode and configures standby timer. */
  290. grub_hdparm_set_val_cmd ("", -1, disk, GRUB_ATA_CMD_IDLE, 0, standby_tout);
  291. }
  292. if (enable_smart >= 0)
  293. {
  294. if (! quiet)
  295. grub_printf ("%sable SMART operations", (enable_smart ? "En" : "Dis"));
  296. int err = grub_hdparm_do_smart_cmd (disk, (enable_smart ?
  297. GRUB_ATA_FEAT_SMART_ENABLE : GRUB_ATA_FEAT_SMART_DISABLE));
  298. if (! quiet)
  299. grub_printf ("%s\n", err ? ": not supported" : "");
  300. }
  301. if (sec_freeze)
  302. grub_hdparm_simple_cmd ("Freeze security settings", disk,
  303. GRUB_ATA_CMD_SECURITY_FREEZE_LOCK);
  304. /* Print/dump IDENTIFY. */
  305. if (ident || dumpid)
  306. {
  307. char buf[GRUB_DISK_SECTOR_SIZE];
  308. if (grub_hdparm_do_ata_cmd (disk, GRUB_ATA_CMD_IDENTIFY_DEVICE,
  309. 0, 0, buf, sizeof (buf)))
  310. grub_printf ("Cannot read ATA IDENTIFY data\n");
  311. else
  312. {
  313. if (ident)
  314. grub_hdparm_print_identify (buf);
  315. if (dumpid)
  316. hexdump (0, buf, sizeof (buf));
  317. }
  318. }
  319. /* Check power mode. */
  320. if (power)
  321. {
  322. grub_printf ("Disk power mode is: ");
  323. int mode = grub_hdparm_do_check_powermode_cmd (disk);
  324. if (mode < 0)
  325. grub_printf ("unknown\n");
  326. else
  327. grub_printf ("%s (0x%02x)\n",
  328. (mode == 0xff ? "active/idle" :
  329. mode == 0x80 ? "idle" :
  330. mode == 0x00 ? "standby" : "unknown"), mode);
  331. }
  332. /* Check health. */
  333. int status = 0;
  334. if (health)
  335. {
  336. if (! quiet)
  337. grub_printf ("SMART status is: ");
  338. int err = grub_hdparm_do_smart_cmd (disk, GRUB_ATA_FEAT_SMART_STATUS);
  339. if (! quiet)
  340. grub_printf ("%s\n", (err < 0 ? "unknown" :
  341. err == 0 ? "OK" : "*BAD*"));
  342. status = (err > 0);
  343. }
  344. /* Change power mode. */
  345. if (standby_now)
  346. grub_hdparm_simple_cmd ("Set disk to standby mode", disk,
  347. GRUB_ATA_CMD_STANDBY_IMMEDIATE);
  348. if (sleep_now)
  349. grub_hdparm_simple_cmd ("Set disk to sleep mode", disk,
  350. GRUB_ATA_CMD_SLEEP);
  351. grub_disk_close (disk);
  352. grub_errno = GRUB_ERR_NONE;
  353. return status;
  354. }
  355. static grub_extcmd_t cmd;
  356. GRUB_MOD_INIT(hdparm)
  357. {
  358. cmd = grub_register_extcmd ("hdparm", grub_cmd_hdparm,
  359. GRUB_COMMAND_FLAG_BOTH,
  360. N_("[OPTIONS] DISK"),
  361. N_("Get/set ATA disk parameters."), options);
  362. }
  363. GRUB_MOD_FINI(hdparm)
  364. {
  365. grub_unregister_extcmd (cmd);
  366. }