hdparm.mod - get/set ATA disk parameters
PATCH 1/1 hdparm.mod - get/set ATA disk parameters
This patch adds a command which allows to change a few (S)ATA drive settings. It relies on a new pass-through function in ata.mod.
Command name and option syntax are borrowed from the well-known Linux tool.
Examples:
- Set AAM of system drive to "fast", other drive to "quiet":
hdparm -S 254 (ata0)
hdparm -S 128 (ata1)
- Change APM level
(reportedly useful to avoid clicking noise of some Notebook disks):
hdparm -B 254 (ata0)
- Set standby timeout to 10min
(e.g. if disk is likely not used in the selected boot configuration):
hdparm -S 120 (ata0)
- Set disk to standby or sleep mode (e.g. before running a long memtest):
hdparm -y (ata0)
hdparm -Y (ata0)
- Freeze security settings (some BIOS don't):
hdparm -F (ata0)
Thanks for any comment.
Christian
2009-01-24 Christian Franke <[email protected]>
* commands/hdparm.c: New file. Provides `hdparm' command which sends ATA commands via grub_ata_pass_through.
* conf/i386-pc.rmk: Add hdparm.mod.
* disk/ata.c: Include <grub/ata.h>. (GRUB_CDROM_SECTOR_SIZE): Remove.
(GRUB_ATA_*): Move to include/grub/ata.h.
(GRUB_ATAPI_*): Likewise.
(enum grub_ata_commands): Likewise. (grub_ata_pass_through): New function.
(GRUB_MOD_INIT): Set grub_disk_ata_pass_through pointer. (GRUB_MOD_FINI): Reset grub_disk_ata_pass_through pointer.
* include/grub/ata.h: New file, contains declarations from ata.c.
* include/grub/disk.h (grub_disk_ata_pass_through_parms): New struct. (grub_disk_ata_pass_through): New exported variable.
* kern/disk.c (grub_disk_ata_pass_through): New variable.
commands/hdparm.c new file +340 -0
@@ -0,0 +1,340 @@ +/* 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/arg.h> +#include <grub/ata.h> +#include <grub/disk.h> +#include <grub/dl.h> +#include <grub/misc.h> +#include <grub/normal.h> +#include <grub/mm.h> +#include <grub/lib/hexdump.h> + + +static const struct grub_arg_option options[] = { + {"apm" ,'B' ,0 ,"Set Advanced Power Management\n" +"(1=low, ..., 254=high, 255=off)" , +0 ,ARG_TYPE_INT }, + {"power" ,'C' ,0 ,"Check power mode" ,0 ,ARG_TYPE_NONE }, + {"security-freeze" ,'F' ,0 ,"Freeze ATA security settings until reset" , +0 ,ARG_TYPE_NONE }, + {"aam" ,'M' ,0 ,"Set Automatic Acoustic Management\n" +"(0=off, 128=quiet, ..., 254=fast)" , +0 ,ARG_TYPE_INT }, + {"standby-timeout" ,'S' ,0 ,"Set standby timeout\n" +"(0=off, 1=5s, 2=10s, ..., 240=20m, 241=30m, ...)" , +0 ,ARG_TYPE_INT }, + {"standby" ,'y' ,0 ,"Set drive to standby mode" ,0 ,ARG_TYPE_NONE }, + {"sleep" ,'Y' ,0 ,"Set drive to sleep mode" ,0 ,ARG_TYPE_NONE }, + {"identify" ,'i' ,0 ,"Print drive identity and settings" , +0 ,ARG_TYPE_NONE }, + {"dumpid" ,'I' ,0 ,"Dump contents of ATA IDENTIFY sector" , +0 ,ARG_TYPE_NONE }, + {"quiet" ,'q' ,0 ,"Do not print messages" ,0 ,ARG_TYPE_NONE }, + {0 ,0 ,0 ,0 ,0 ,0 } +}; + +static int quiet =0 ; + +static grub_err_t +grub_hdparm_do_ata_cmd (grub_disk_t disk,grub_uint8_t cmd, +grub_uint8_t features,grub_uint8_t sectors, +grub_uint8_t *ret_sectors, +void * buffer,int size) +{ +struct grub_disk_ata_pass_through_parms apt; +grub_memset (&apt,0 ,sizeof (apt)); + + apt.taskfile[GRUB_ATA_REG_CMD ] = cmd; + apt.taskfile[GRUB_ATA_REG_FEATURES ] = features; + apt.taskfile[GRUB_ATA_REG_SECTORS ] = sectors; + apt.buffer = buffer; + apt.size = size; + +if (grub_disk_ata_pass_through (disk, &apt)) +return grub_errno; + +if (ret_sectors) + *ret_sectors = apt.taskfile[GRUB_ATA_REG_SECTORS ]; +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_hdparm_simple_cmd (const char * msg, +grub_disk_t disk,grub_uint8_t cmd) +{ +if (! quiet && msg) +grub_printf ("%s" , msg); + +grub_err_t err =grub_hdparm_do_ata_cmd (disk, cmd,0 ,0 ,NULL ,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_disk_t disk,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 (disk, cmd, features, sectors, +NULL ,NULL ,0 ); + +if (! quiet && msg) +grub_printf ("%s\n" , ! err ?"" :": not supported" ); +return err; +} + +static const char * +le16_to_char (char *dest,const grub_uint16_t * src16,unsigned bytes) +{ +grub_uint16_t * dest16 = (grub_uint16_t *) dest; +unsigned i; +for (i =0 ; i < bytes /2 ; i++) + dest16[i] =grub_be_to_cpu16 (src16[i]); +return dest; +} + +static void +grub_hdparm_print_identify (const char * idbuf) +{ +const grub_uint16_t * idw = (const grub_uint16_t *) idbuf; + +/* Print identity strings. */ +char tmp[40 ]; +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 and APM settings. */ +grub_uint16_t features =grub_le_to_cpu16 (idw[83 ]); +grub_uint16_t enabled =grub_le_to_cpu16 (idw[86 ]); + +grub_printf ("Automatic Acoustic Management: " ); +if (features &0x0200 ) + { +if (enabled &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 (features &0x0008 ) + { +if (enabled &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" ); + +/* 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 ("no 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 (struct grub_arg_list *state,int argc,char **args)// state???? +{ +/* Check command line. */ +if (argc !=1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"missing device name argument" ); + +grub_size_t len =grub_strlen (args[0 ]); +if (! (args[0 ][0 ] =='(' && args[0 ][len -1 ] ==')' )) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"argument is not a device name" ); + args[0 ][len -1 ] =0 ; + +if (! grub_disk_ata_pass_through) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"ATA pass through not available" ); + +int i =0 ; +int apm =get_int_arg (&state[i++]); +int power = state[i++].set; +int sec_freeze = 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; + quiet = state[i++].set; + +/* Open disk. */ +grub_disk_t disk =grub_disk_open (&args[0 ][1 ]); +if (! disk) +return grub_errno; + +if (disk->partition) + { +grub_disk_close (disk); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"partition not allowed" ); + } + +/* Change settings. */ +if (aam >=0 ) +grub_hdparm_set_val_cmd ("Automatic Acoustic Management" , (aam ? aam :-1 ), + disk,GRUB_ATA_CMD_SET_FEATURES , (aam ?0x42 :0xc2 ), aam); + +if (apm >=0 ) +grub_hdparm_set_val_cmd ("Advanced Power Management" , + (apm !=255 ? apm :-1 ), disk,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 , disk,GRUB_ATA_CMD_IDLE ,0 , standby_tout); + } + +if (sec_freeze) +grub_hdparm_simple_cmd ("Freeze security settings" , disk, +GRUB_ATA_CMD_SECURITY_FREEZE_LOCK ); + +/* Print/dump IDENTIFY. */ +if (ident || dumpid) + { +char buf[GRUB_DISK_SECTOR_SIZE ]; +if (grub_hdparm_do_ata_cmd (disk,GRUB_ATA_CMD_IDENTIFY_DEVICE , +0 ,0 ,NULL , buf,sizeof (buf))) +grub_printf ("Cannot read ATA IDENTIFY data\n" ); +else + { +if (ident) +grub_hdparm_print_identify (buf); +if (dumpid) +hexdump (0 , buf,sizeof (buf)); + } + } + +/* Check power mode. */ +if (power) + { +grub_printf ("Disk power mode is " ); +grub_uint8_t mode =0 ; +if (grub_hdparm_do_ata_cmd (disk,GRUB_ATA_CMD_CHECK_POWER_MODE , +0 ,0 , &mode,NULL ,0 )) +grub_printf ("unknown\n" ); +else +grub_printf ("%s (0x%02x)\n" , + (mode ==0xff ?"active/idle" : + mode ==0x80 ?"idle" : + mode ==0x00 ?"standby" :"unknown" ), mode); + } + +/* Change power mode. */ +if (standby_now) +grub_hdparm_simple_cmd ("Set disk to standby mode" , disk, +GRUB_ATA_CMD_STANDBY_IMMEDIATE ); + +if (sleep_now) +grub_hdparm_simple_cmd ("Set disk to sleep mode" , disk, +GRUB_ATA_CMD_SLEEP ); + +grub_disk_close (disk); + grub_errno =GRUB_ERR_NONE ; +return 0 ; +} + + +GRUB_MOD_INIT (hdparm) +{ + (void ) mod; + +grub_register_command ("hdparm" , grub_cmd_hdparm,GRUB_COMMAND_FLAG_BOTH , +"hdparm [OPTIONS] DISK" , +"Get/set ATA disk parameters." , options); +} + +GRUB_MOD_FINI (hdparm) +{ +grub_unregister_command ("hdparm" ); +}
conf/i386-pc.rmk +6 -1
@@ -164,7 +164,7 @@ pkglib_MODULES = biosdisk.mod _chain.mod _linux.mod linux.mod normal.mod \ vbe.mod vbetest.mod vbeinfo.mod play.mod serial.mod \ ata.mod vga.mod memdisk.mod pci.mod lspci.mod \ aout.mod _bsd.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \ - datehook.mod lsmmap.mod + datehook.mod lsmmap.mod hdparm.mod # For biosdisk.mod. biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c
@@ -321,5 +321,10 @@ lsmmap_mod_SOURCES = commands/lsmmap.c lsmmap_mod_CFLAGS = $(COMMON_CFLAGS) lsmmap_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For hdparm.mod. +hdparm_mod_SOURCES = commands/hdparm.c lib/hexdump.c +hdparm_mod_CFLAGS = $(COMMON_CFLAGS) +hdparm_mod_LDFLAGS = $(COMMON_LDFLAGS) + include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk
disk/.ata.c.swp new file binary +0 -0
disk/ata.c +69 -49
152 lines not on this page · raw message
include/grub/ata.h new file +76 -0
76 lines not on this page · raw message
include/grub/disk.h +11 -0
@@ -161,6 +161,17 @@ grub_uint64_t EXPORT_FUNC(grub_disk_get_size) (grub_disk_t disk);extern void (*EXPORT_VAR (grub_disk_firmware_fini )) (void );extern int EXPORT_VAR (grub_disk_firmware_is_tainted ); +/* ATA pass through parameters and function. */ +struct grub_disk_ata_pass_through_parms +{ +grub_uint8_t taskfile[8 ]; +void * buffer; +int size; +}; + +extern grub_err_t (*EXPORT_VAR (grub_disk_ata_pass_through )) (grub_disk_t , +struct grub_disk_ata_pass_through_parms *); +#ifdef GRUB_UTIL void grub_raid_init (void );void grub_raid_fini (void );
kern/disk.c +4 -0
@@ -46,6 +46,10 @@ static struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];void (*grub_disk_firmware_fini) (void );int grub_disk_firmware_is_tainted; +grub_err_t (* grub_disk_ata_pass_through) (grub_disk_t , +struct grub_ata_pass_through_cmd *); + +#if 0 static unsigned long grub_disk_cache_hits;static unsigned long grub_disk_cache_misses;
Quoted 2 lines · Christian Franke
This patch adds a command which allows to change a few (S)ATA drive settings. It relies on a new pass-through function in ata.mod.
Is this function going to be used for "normal" use of ata? Note that ata.mod should be as small as possible, since coreboot users usually want to include it the GRUB that is installed to flash.
Would it make sense to put the function elsewhere?
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Quoted 10 lines · Robert Millan
Quoted 2 lines · Christian Franke
This patch adds a command which allows to change a few (S)ATA drive settings. It relies on a new pass-through function in ata.mod.
Is this function going to be used for "normal" use of ata? Note that ata.mod should be as small as possible, since coreboot users usually want to include it the GRUB that is installed to flash.
Like 'hdparm.mod' itself, the ATA pass-through function is not needed for normal boot.
Quoted 3 lines
Would it make sense to put the function elsewhere?
I would suggest to move grub_ata_pass_through() to a new module (e.g. ata2.mod, atax.mod, ataex.mod, ...)
The function should not be in hdparm.mod itself, because the hdparm command would also work with other ATA pass-through functions. For example with some future ahci.mod or even in conjunction with native USB support through a SAT tunnel.
Christian
Quoted 15 lines · Christian Franke
Quoted 6 lines · Robert Millan
Quoted 3 lines · Christian Franke
This patch adds a command which allows to change a few (S)ATA drive settings. It relies on a new pass-through function in ata.mod.
Quoted 7 lines
Is this function going to be used for "normal" use of ata? Note that ata.mod
should be as small as possible, since coreboot users usually want to include
it the GRUB that is installed to flash.
Quoted 2 lines
Like 'hdparm.mod' itself, the ATA pass-through function is not needed for normal boot.
Well, I know it's not needed, but I was wondering if there's the possibility that other functions in ata.mod could start using it later on, or so.
Quoted 2 lines
I would suggest to move
grub_ata_pass_through()to a new module (e.g. ata2.mod, atax.mod, ataex.mod, ...)
How about ata_something.mod? (consistent with ntfs.mod & ntfs_comp.mod)
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Quoted 7 lines · Robert Millan
Quoted 3 lines
I would suggest to move
grub_ata_pass_through()to a new module (e.g. ata2.mod, atax.mod, ataex.mod, ...)How about ata_something.mod? (consistent with ntfs.mod & ntfs_comp.mod)
ata_pthru.mod ?
Christian
Quoted 10 lines · Christian Franke
Quoted 8 lines · Robert Millan
Quoted 4 lines
I would suggest to move
grub_ata_pass_through()to a new module (e.g. ata2.mod, atax.mod, ataex.mod, ...)How about ata_something.mod? (consistent with ntfs.mod & ntfs_comp.mod)
ata_pthru.mod ?
Deal!
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Quoted 10 lines · Robert Millan
Quoted 8 lines · Christian Franke
Quoted 6 lines · Robert Millan
Quoted 3 lines
I would suggest to move
grub_ata_pass_through()to a new module (e.g. ata2.mod, atax.mod, ataex.mod, ...)How about ata_something.mod? (consistent with ntfs.mod & ntfs_comp.mod)
It is actually ntfscomp.mod.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
ata_pthru.mod ?
Deal!
I will prepare a patch - ata_pthru.mod, atapthru.mod or a more generic name to allow to add other ata_extension functions ?
Christian
Quoted 7 lines · Robert Millan
Quoted 5 lines · Christian Franke
Quoted 3 lines · Robert Millan
Quoted 3 lines
I would suggest to move
grub_ata_pass_through()to a new module (e.g. ata2.mod, atax.mod, ataex.mod, ...)
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
How about ata_something.mod? (consistent with ntfs.mod & ntfs_comp.mod)
ata_pthru.mod ?
Deal!
New patch below. Function grub_ata_pass_through() now moved to new module ata_pthru.mod.
Christian
2009-02-12 Christian Franke <[email protected]>
* commands/hdparm.c: New file. Provides `hdparm' command which sends ATA commands via grub_disk_ata_pass_through ().
* conf/i386-pc.rmk: Add ata_pthru.mod and hdparm.mod.
* disk/ata.c: Include <grub/ata.h>. Move <grub/misc.h> and <grub/cpu/io.h> to include/grub/ata.h.
(enum grub_ata_addressing_t): Move to include/grub/ata.h. (GRUB_CDROM_SECTOR_SIZE): Remove.
(GRUB_ATA_*): Move to include/grub/ata.h.
(GRUB_ATAPI_*): Likewise.
(enum grub_ata_commands): Likewise.
(enum grub_ata_timeout_milliseconds): Likewise.
(struct grub_ata_device): Likewise.
(grub_ata_regset): Likewise.
(grub_ata_regget): Likewise.
(grub_ata_regset2): Likewise.
(grub_ata_regget2): Likewise.
(grub_ata_check_ready): Likewise. (grub_ata_wait_not_busy): Remove static, exported in include/grub/ata.h.
(grub_ata_wait_drq): Likewise.
(grub_ata_pio_read): Likewise.
* disk/ata_pthru.c: New file. Provides grub_ata_pass_through () function for hdparm.mod.
* include/grub/ata.h: New file, contains declarations from ata.c. (enum grub_ata_commands): Add new commands for hdparm.c.
* include/grub/disk.h (grub_disk_ata_pass_through_parms): New struct. (grub_disk_ata_pass_through): New exported variable.
* kern/disk.c (grub_disk_ata_pass_through): New variable.
commands/hdparm.c new file +340 -0
@@ -0,0 +1,340 @@ +/* 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/arg.h> +#include <grub/ata.h> +#include <grub/disk.h> +#include <grub/dl.h> +#include <grub/misc.h> +#include <grub/normal.h> +#include <grub/mm.h> +#include <grub/lib/hexdump.h> + + +static const struct grub_arg_option options[] = { + {"apm" ,'B' ,0 ,"set Advanced Power Management\n" +"(1=low, ..., 254=high, 255=off)" , +0 ,ARG_TYPE_INT }, + {"power" ,'C' ,0 ,"check power mode" ,0 ,ARG_TYPE_NONE }, + {"security-freeze" ,'F' ,0 ,"freeze ATA security settings until reset" , +0 ,ARG_TYPE_NONE }, + {"aam" ,'M' ,0 ,"set Automatic Acoustic Management\n" +"(0=off, 128=quiet, ..., 254=fast)" , +0 ,ARG_TYPE_INT }, + {"standby-timeout" ,'S' ,0 ,"set standby timeout\n" +"(0=off, 1=5s, 2=10s, ..., 240=20m, 241=30m, ...)" , +0 ,ARG_TYPE_INT }, + {"standby" ,'y' ,0 ,"set drive to standby mode" ,0 ,ARG_TYPE_NONE }, + {"sleep" ,'Y' ,0 ,"set drive to sleep mode" ,0 ,ARG_TYPE_NONE }, + {"identify" ,'i' ,0 ,"print drive identity and settings" , +0 ,ARG_TYPE_NONE }, + {"dumpid" ,'I' ,0 ,"dump contents of ATA IDENTIFY sector" , +0 ,ARG_TYPE_NONE }, + {"quiet" ,'q' ,0 ,"do not print messages" ,0 ,ARG_TYPE_NONE }, + {0 ,0 ,0 ,0 ,0 ,0 } +}; + +static int quiet =0 ; + +static grub_err_t +grub_hdparm_do_ata_cmd (grub_disk_t disk,grub_uint8_t cmd, +grub_uint8_t features,grub_uint8_t sectors, +grub_uint8_t *ret_sectors, +void * buffer,int size) +{ +struct grub_disk_ata_pass_through_parms apt; +grub_memset (&apt,0 ,sizeof (apt)); + + apt.taskfile[GRUB_ATA_REG_CMD ] = cmd; + apt.taskfile[GRUB_ATA_REG_FEATURES ] = features; + apt.taskfile[GRUB_ATA_REG_SECTORS ] = sectors; + apt.buffer = buffer; + apt.size = size; + +if (grub_disk_ata_pass_through (disk, &apt)) +return grub_errno; + +if (ret_sectors) + *ret_sectors = apt.taskfile[GRUB_ATA_REG_SECTORS ]; +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_hdparm_simple_cmd (const char * msg, +grub_disk_t disk,grub_uint8_t cmd) +{ +if (! quiet && msg) +grub_printf ("%s" , msg); + +grub_err_t err =grub_hdparm_do_ata_cmd (disk, cmd,0 ,0 ,NULL ,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_disk_t disk,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 (disk, cmd, features, sectors, +NULL ,NULL ,0 ); + +if (! quiet && msg) +grub_printf ("%s\n" , ! err ?"" :": not supported" ); +return err; +} + +static const char * +le16_to_char (char *dest,const grub_uint16_t * src16,unsigned bytes) +{ +grub_uint16_t * dest16 = (grub_uint16_t *) dest; +unsigned i; +for (i =0 ; i < bytes /2 ; i++) + dest16[i] =grub_be_to_cpu16 (src16[i]); +return dest; +} + +static void +grub_hdparm_print_identify (const char * idbuf) +{ +const grub_uint16_t * idw = (const grub_uint16_t *) idbuf; + +/* Print identity strings. */ +char tmp[40 ]; +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 and APM settings. */ +grub_uint16_t features =grub_le_to_cpu16 (idw[83 ]); +grub_uint16_t enabled =grub_le_to_cpu16 (idw[86 ]); + +grub_printf ("Automatic Acoustic Management: " ); +if (features &0x0200 ) + { +if (enabled &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 (features &0x0008 ) + { +if (enabled &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" ); + +/* 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 (struct grub_arg_list *state,int argc,char **args)// state???? +{ +/* Check command line. */ +if (argc !=1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"missing device name argument" ); + +grub_size_t len =grub_strlen (args[0 ]); +if (! (args[0 ][0 ] =='(' && args[0 ][len -1 ] ==')' )) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"argument is not a device name" ); + args[0 ][len -1 ] =0 ; + +if (! grub_disk_ata_pass_through) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"ATA pass through not available" ); + +int i =0 ; +int apm =get_int_arg (&state[i++]); +int power = state[i++].set; +int sec_freeze = 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; + quiet = state[i++].set; + +/* Open disk. */ +grub_disk_t disk =grub_disk_open (&args[0 ][1 ]); +if (! disk) +return grub_errno; + +if (disk->partition) + { +grub_disk_close (disk); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"partition not allowed" ); + } + +/* Change settings. */ +if (aam >=0 ) +grub_hdparm_set_val_cmd ("Automatic Acoustic Management" , (aam ? aam :-1 ), + disk,GRUB_ATA_CMD_SET_FEATURES , (aam ?0x42 :0xc2 ), aam); + +if (apm >=0 ) +grub_hdparm_set_val_cmd ("Advanced Power Management" , + (apm !=255 ? apm :-1 ), disk,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 , disk,GRUB_ATA_CMD_IDLE ,0 , standby_tout); + } + +if (sec_freeze) +grub_hdparm_simple_cmd ("Freeze security settings" , disk, +GRUB_ATA_CMD_SECURITY_FREEZE_LOCK ); + +/* Print/dump IDENTIFY. */ +if (ident || dumpid) + { +char buf[GRUB_DISK_SECTOR_SIZE ]; +if (grub_hdparm_do_ata_cmd (disk,GRUB_ATA_CMD_IDENTIFY_DEVICE , +0 ,0 ,NULL , buf,sizeof (buf))) +grub_printf ("Cannot read ATA IDENTIFY data\n" ); +else + { +if (ident) +grub_hdparm_print_identify (buf); +if (dumpid) +hexdump (0 , buf,sizeof (buf)); + } + } + +/* Check power mode. */ +if (power) + { +grub_printf ("Disk power mode is " ); +grub_uint8_t mode =0 ; +if (grub_hdparm_do_ata_cmd (disk,GRUB_ATA_CMD_CHECK_POWER_MODE , +0 ,0 , &mode,NULL ,0 )) +grub_printf ("unknown\n" ); +else +grub_printf ("%s (0x%02x)\n" , + (mode ==0xff ?"active/idle" : + mode ==0x80 ?"idle" : + mode ==0x00 ?"standby" :"unknown" ), mode); + } + +/* Change power mode. */ +if (standby_now) +grub_hdparm_simple_cmd ("Set disk to standby mode" , disk, +GRUB_ATA_CMD_STANDBY_IMMEDIATE ); + +if (sleep_now) +grub_hdparm_simple_cmd ("Set disk to sleep mode" , disk, +GRUB_ATA_CMD_SLEEP ); + +grub_disk_close (disk); + grub_errno =GRUB_ERR_NONE ; +return 0 ; +} + + +GRUB_MOD_INIT (hdparm) +{ + (void ) mod; + +grub_register_command ("hdparm" , grub_cmd_hdparm,GRUB_COMMAND_FLAG_BOTH , +"hdparm [OPTIONS] DISK" , +"Get/set ATA disk parameters." , options); +} + +GRUB_MOD_FINI (hdparm) +{ +grub_unregister_command ("hdparm" ); +}
conf/i386-pc.rmk +11 -1
@@ -171,7 +171,7 @@ pkglib_MODULES = biosdisk.mod _chain.mod _linux.mod linux.mod normal.mod \ vbe.mod vbetest.mod vbeinfo.mod play.mod serial.mod \ ata.mod vga.mod memdisk.mod pci.mod lspci.mod \ aout.mod _bsd.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \ - datehook.mod lsmmap.mod \ + datehook.mod lsmmap.mod ata_pthru.mod hdparm.mod \ usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod # For biosdisk.mod.
@@ -363,5 +363,15 @@ lsmmap_mod_SOURCES = commands/lsmmap.c lsmmap_mod_CFLAGS = $(COMMON_CFLAGS) lsmmap_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For ata_pthru.mod. +ata_pthru_mod_SOURCES = disk/ata_pthru.c +ata_pthru_mod_CFLAGS = $(COMMON_CFLAGS) +ata_pthru_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For hdparm.mod. +hdparm_mod_SOURCES = commands/hdparm.c lib/hexdump.c +hdparm_mod_CFLAGS = $(COMMON_CFLAGS) +hdparm_mod_LDFLAGS = $(COMMON_LDFLAGS) + include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk
disk/ata.c +5 -136
180 lines not on this page · raw message
disk/ata_pthru.c new file +103 -0
103 lines not on this page · raw message
include/grub/ata.h new file +167 -0
167 lines not on this page · raw message
include/grub/disk.h +11 -0
@@ -161,6 +161,17 @@ grub_uint64_t EXPORT_FUNC(grub_disk_get_size) (grub_disk_t disk);extern void (*EXPORT_VAR (grub_disk_firmware_fini )) (void );extern int EXPORT_VAR (grub_disk_firmware_is_tainted ); +/* ATA pass through parameters and function. */ +struct grub_disk_ata_pass_through_parms +{ +grub_uint8_t taskfile[8 ]; +void * buffer; +int size; +}; + +extern grub_err_t (*EXPORT_VAR (grub_disk_ata_pass_through )) (grub_disk_t , +struct grub_disk_ata_pass_through_parms *); +#ifdef GRUB_UTIL void grub_raid_init (void );void grub_raid_fini (void );
kern/disk.c +4 -0
@@ -46,6 +46,10 @@ static struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];void (*grub_disk_firmware_fini) (void );int grub_disk_firmware_is_tainted; +grub_err_t (* grub_disk_ata_pass_through) (grub_disk_t , +struct grub_ata_pass_through_cmd *); + +#if 0 static unsigned long grub_disk_cache_hits;static unsigned long grub_disk_cache_misses;
Quoted 3 lines · Christian Franke
...
New patch below. Functiongrub_ata_pass_through()now moved to new module ata_pthru.mod.
Committed. Now also includes a SMART status check.
grub.cfg example (assumes ata.mod is used):
...
insmod ata_pthru
insmod hdparm
# Make sure disks cannot be locked by an ATA password hdparm --quiet --security-freeze (ata4) hdparm --quiet --security-freeze (ata6)
menuentry "Boot" {
# Check health
if hdparm --quiet --health (ata4) ; then echo -n ; else
echo "Warning: SMART status check failed" read
fi
# Set boot disk to "fast", disable spin down hdparm --quiet --aam=254 --standby-timeout=0 (ata4)
# Set other disk to "quiet", spin down after 5min inactivity hdparm --quiet --aam=128 --standby-timeout=60 (ata6)
# Boot ...
}
menuentry "Memory Test" {
# Spin down both disks after 10min
hdparm --quiet --standby-timeout=120 (ata4)
hdparm --quiet --standby-timeout=120 (ata6)
# Load memtest ...
}
Christian
Quoted 1 line · Christian Franke
insmod
ata_pthru
(note that module dependencies should make this unnecessary)
Quoted 23 lines
insmod hdparm
# Make sure disks cannot be locked by an ATA password hdparm --quiet --security-freeze (ata4) hdparm --quiet --security-freeze (ata6)
menuentry "Boot" {
# Check health
if hdparm --quiet --health (ata4) ; then echo -n ; else
echo "Warning: SMART status check failed" read
fi# Set boot disk to "fast", disable spin down hdparm --quiet --aam=254 --standby-timeout=0 (ata4)
# Set other disk to "quiet", spin down after 5min inactivity hdparm --quiet --aam=128 --standby-timeout=60 (ata6)
# Boot ...
}
Quoted 8 lines
menuentry "Memory Test" {
# Spin down both disks after 10min
hdparm --quiet --standby-timeout=120 (ata4)
hdparm --quiet --standby-timeout=120 (ata6)# Load memtest ...
}
Very interesting. Do you think any of these features could be useful as a default option in grub-mkconfig?
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Quoted 8 lines · Robert Millan
Quoted 2 lines · Christian Franke
insmod
ata_pthru(note that module dependencies should make this unnecessary)
This insmod is necessary for now. hdparm.mod does not directly call ata_pthru.mod, it uses kern/disk.c::grub_disk_ata_pass_through function pointer instead. Otherwise, using other future ata_pass_through functions (e.g. from some ahci.mod :-) would not be possible.
Another drawback of a hdparm->ata_pthru dependency would be that 'help' or 'hdparm -h' would load ata.mod which disables e.g. biosdisk.mod.
Quoted 29 lines
Quoted 29 lines
insmod hdparm
# Make sure disks cannot be locked by an ATA password hdparm --quiet --security-freeze (ata4) hdparm --quiet --security-freeze (ata6)
menuentry "Boot" {
# Check health
if hdparm --quiet --health (ata4) ; then echo -n ; else
echo "Warning: SMART status check failed" read
fi# Set boot disk to "fast", disable spin down hdparm --quiet --aam=254 --standby-timeout=0 (ata4)
# Set other disk to "quiet", spin down after 5min inactivity hdparm --quiet --aam=128 --standby-timeout=60 (ata6)
# Boot ...
}menuentry "Memory Test" {
# Spin down both disks after 10min
hdparm --quiet --standby-timeout=120 (ata4)
hdparm --quiet --standby-timeout=120 (ata6)
Quoted 8 lines
Quoted 3 lines
# Load memtest ...
}Very interesting. Do you think any of these features could be useful as a default option in grub-mkconfig?
At least the --health check and --security-freeze are IMO recommended for each disk. Both is also done by a typical PC BIOS. AAM and standby settings are more user specific. The problem is that this relies on native ATA support which is now only available for controllers supported by ata.mod.
At least for the boot disk, such hdparm calls could be added by e.g. extending 'prepare_grub_to_access_device'.
Christian
Quoted 6 lines · Christian Franke
Quoted 5 lines
Very interesting. Do you think any of these features could be useful as a default option in grub-mkconfig?
Quoted 8 lines
At least the --health check and --security-freeze are IMO recommended for each disk. Both is also done by a typical PC BIOS. AAM and standby settings are more user specific. The problem is that this relies on native ATA support which is now only available for controllers supported by ata.mod.
At least for the boot disk, such hdparm calls could be added by e.g. extending '
prepare_grub_to_access_device'.
But if the firmware already did, wouldn't this be a waste of boot time?
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Quoted 7 lines · Robert Millan
Quoted 5 lines · Christian Franke
Quoted 5 lines
Very interesting. Do you think any of these features could be useful as a default option in grub-mkconfig?
Quoted 8 lines
Quoted 4 lines
At least the --health check and --security-freeze are IMO recommended for each disk. Both is also done by a typical PC BIOS. AAM and standby settings are more user specific. The problem is that this relies on native ATA support which is now only available for controllers supported by ata.mod.
At least for the boot disk, such hdparm calls could be added by e.g. extending '
prepare_grub_to_access_device'.But if the firmware already did, wouldn't this be a waste of boot time?
Yes, it would. Most BIOS perform both health check and security freeze, but some don't. For coreboot, it is not a waste of boot time.
So if this is added to grub-mkconfig, it should be configurable by some variable.
Signature · 1 line
Christian Franke
Quoted 10 lines · Christian Franke
Quoted 4 lines
But if the firmware already did, wouldn't this be a waste of boot time?
Yes, it would. Most BIOS perform both health check and security freeze, but some don't. For coreboot, it is not a waste of boot time.
So if this is added to grub-mkconfig, it should be configurable by some variable.
When using coreboot with GRUB, you usually have either:
- A single GRUB, in chip, with a hand-crafted configuration.
- Two GRUBs, one in chip which loads the other one from disk. The configuration for the first is hand-crafted but the second one would usually be grub-mkconfig'ed.
In this case sounds like the right place for this would be the hand-crafted part. There's some example script code in:
http://grub.enbug.org/CoreBoot
How about adding it there?
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."