SG_WR_MODE(8)
πΈ NAME πΈ
okay so this is SG_WR_MODE(8) from SG3_UTILS, and the utility is sg_wr_mode β literally write (modify) SCSI mode page. That's the name, bestie, and it's giving "tiny tool, big mode-page energy." π
β¨ SYNOPSIS β¨
The whole group-chat summoning ritual is:
sg_wr_mode [--cfile=CF] [--contents=H,H...] [--dbd] [--force] [--help]
[--len=10|6] [--mask=M,M...] [--mfile=MF] [--page=PG_H[,SPG_H]] [--raw]
[--rtd] [--save] [--six] [--verbose] [--version] DEVICE
π¦ DESCRIPTION π¦
okay so sg_wr_mode writes a modified mode page to DEVICE. It uses the SCSI MODE SENSE (6 or 10 byte variant) command to fetch the existing mode data, which includes a mode page (or subpage). Then it combines that with the contents, potentially masked, and writes the modified mode page with the SCSI MODE SELECT (6 or 10 byte variant) command. This utility does not modify the block descriptor(s); if any block descriptors are fetched by the MODE SENSE command then the same block descriptors are written back with the following MODE SELECT command. π¦
If the --rtd option is given then most other options apart from --save, --len=10|6 and --six are ignored. In this case only a MODE SELECT command is sent to the DEVICE with the RTD bit (Revert To Defaults) set. This bit was added to this command in SPC-5 revision 11, so older devices may not support it. The Extended Inquiry VPD page has the RTD_SUP bit to indicate whether the DEVICE supports the RTD bit in the MODE SELECT(6 and 10) commands. When the --rtd option is given the rest of this section can be ignored. π
If a contents option is not given then the various components (i.e. header, block descriptor(s) and mode page) of the "current" values of the existing mode page are printed out. In this case the mode page is not altered on the device. π«Ά
If the contents are specified, and a mask is not specified, then the contents must match the existing mode page in various aspects unless the --force option is given. These include length, mode page code and subpage code if applicable. If all is well then the contents string is written to DEVICE as the new mode page. π
If both contents and mask strings are specified then only bit positions in the contents corresponding to set bits in the mask are taken while the existing mode page supplies bit positions corresponding to clear bits. When a mask is given then the mask and/or the contents may be shorter than the existing mode page. If the mask is shorter than the contents then the remaining bytes are taken from the contents. If the contents are shorter than the existing mode page then the remaining bytes are taken from the existing mod page. π
The force option allows the contents string to be written as the new mode page without any prior checks on the existing mode page. This should only be required for vendor specific mode pages. The existing mode data is ignored apart from the block descriptors which can be suppressed with the --dbd option if need be. π₯
Changing individual fields in a mode page is probably more easily done with the sdparm utility. Fields can be identified by acronym or by a numerical descriptor. β
π OPTIONS π
Arguments to long options are mandatory for short options as well.
-C,--cfile=CFβ where CF is a file name containing the mode page. See the "HEX, BINARY AND JSON FORMATS" section in thesg3_utilsmanpage for more information. If the--rawoption is also given then the contents of CF are treated as binary. π-c,--contents=H,H...β whereH,H...is a string of comma separated hex numbers each of which should resolve to a byte value (i.e.0toffinclusive). A (single) space separated string of hex numbers is also allowed but the list needs to be in quotes. This is the new contents of the mode page to be written to DEVICE, potentially filtered by the mask string. π-c,--contents=-β reads contents string from stdin. See the--cfile=CFoption above. π«Ά-d,--dbdβ disable block descriptors (DBD flag in cdb). Some device types include block descriptors in the mode data returned by aMODE SENSEcommand. If so the same block descriptors are written by theMODE SELECTcommand. This option instructs theMODE SENSEcommand not to return any block descriptors. This would be a sensible default for this utility apart from the fact that not all SCSI devices support the DBD bit in the cdb. π§-f,--forceβ force the contents string to be taken as the new mode page, or at least doesn't do checks on the existing mode page. Note that DEVICE may still reject the new contents for the mode page. Cannot be given with the--mask=M,M...option. π₯-h,--helpβ output the usage message then exit. π-l,--len=10 | 6β length of the SCSI commands (cdb) sent to DEVICE. The default is10so 10 byteMODE SENSEandMODE SELECTcommands are issued. Some old devices don't support the 10 byte variants hence this option. π-m,--mask=M,M...β whereM,M...is a string of comma separated hex numbers each of which should resolve to a byte value (i.e.0toffinclusive). A (single) space separated string of hex numbers is also allowed but the list needs to be in quotes. The mask chooses (bit by bit) whether the new mode page comes from the contents (mask bit set) or from the existing mode page (mask bit clear). If the mask string is shorter than the contents string then the remaining bytes are taken from the contents string. If the contents string is shorter than the existing mode page then the remaining bytes are taken from the existing mode page (i.e. they are left unaltered). π-M,--mfile=MFβ where MF is a file name containing the mode page mask. See the "HEX, BINARY AND JSON FORMATS" section in thesg3_utilsmanpage for more information. If the--rawoption is also given then the contents of MF are treated as binary. π-p,--page=PG_Hβ wherePG_His the page code value to fetch and modify. The page code is in hex and should be between0and3einclusive. Notice that page code3fto fetch all mode pages is disallowed. π-p,--page=PG_H,SPG_Hβ wherePG_His the page code value andSPG_His the subpage code value to fetch and modify. Both values are in hex. The subpage code should be between0andfeinclusive. Notice that subpage codeffto fetch all mode subpages (for a given mode page or all mode pages in the case of3f,ff) is disallowed. π-r,--rawβ when this option is given then the file given by the--cfile=CFoption is treated as binary (the default is to treat it as ASCII hexadecimal). β-R,--rtdβ when this option is given most other actions are bypassed and aMODE SELECT(6 or 10)command is sent to the DEVICE with the RTD bit set. This will cause all current values (and saved values if the--saveoption is also given) of all mode pages to be reverted to their default values. π-s,--saveβ changes the "saved" mode page whenMODE SELECTis successful. By default (i.e. when--saveis not used) only the "current" mode page values are changed whenMODE SELECTis successful. In this case the new mode page will stay in effect until the device is reset (e.g. power cycled). When it restarts the "saved" values for the mode page will be re-instated. So to make changes permanent use the--saveoption. When used with the--rtdoption then both the current and saved values in each mode page are reverted to their default values. In the absence of--saveoption only the current values in each mode page are reverted to their default values. πΎ-6,--sixβ this option will cause the 6 byte variants ofMODE SENSEandMODE SELECTcommands to be used. The default is to use the 10 byte options. This option is equivalent to using the--len=6option. π-v,--verboseβ increase the level of verbosity, (i.e. debug output). π-V,--versionβ print the version string and then exit. π
π NOTES π
This utility does not check whether the contents string is trying to modify parts of the mode page which are changeable. The device should do that and if some part is not changeable then it should report: "Invalid field in parameter list". π
Some mode pages are not saveable. If so an attempt to use the --save option should cause an error to be reported from the device: "Illegal field in cdb". π
The device is required to do various checks before it accepts a new mode page. If these checks fail then the mode page is not altered and either a "parameter list length error" or an "invalid field in parameter list" error is returned by the device in the sense data. πΈ
The recommended way to modify a mode page is to read it with a MODE SENSE, modify some part of it then write it back to the device with a MODE SELECT command. For example, reading an existing mode page can be accomplished with sg_modes -p=1a -r /dev/sdb > mp_1a.txt (the power condition mode page). The mp_1a.txt file can be edited and then used as the contents string to this utility (e.g. sg_wr_mode -p 1a -s -c - /dev/sdb < mp_1a.txt). π¦
Two fields differ between what is read from the device with MODE SENSE and what is written to the device with MODE SELECT: the mode data length is reserved (i.e. zero(es)) in a MODE SELECT command while the PS bit ((sub)page byte 0 bit 7) in each mode (sub)page is reserved (zero) in a MODE SELECT command. The PS bit given in the contents string is zeroed unless the --force option is selected. β
β¨ EXAMPLES β¨
okay bestie, this little utility is literally built to vibe with sg_modes π«Ά. You can use them together to re-instate the default mode page values β like, the mode page values chosen by the manufacturer of the device β as both the current and saved mode page values. The sequence goes like this:
$ sg_modes --control=2 --page=1a -r /dev/sda > t
$ sg_wr_mode --page=1a --contents=- --save /dev/sda < t
next up: an example of using a mask to modify the "idle condition counter" of the "power condition" mode page (0x1a) from 0x28 to 0x37 πΈ. Heads up though β the change is not saved, so the "idle condition counter" will revert to 0x28 after the next power cycle. The output from sg_modes is abridged:
$ sg_modes --page=1a /dev/hdc
>> Power condition (mmc), page_control: current
00 1a 0a 00 03 00 00 00 28 00 00 01 2c
$ sg_wr_mode -p 1a -c 0,0,0,0,0,0,0,37 -m 0,0,0,0,0,0,0,ff /dev/hdc
$ sg_modes -p 1a /dev/hdc
>> Power condition (mmc), page_control: current
00 1a 0a 00 03 00 00 00 37 00 00 01 2c
also!! See inhex/wr_mode_cont.hex and wr_mode_mask.hex files in the source tarball for an example of using this utility with the --cfile=CF and --mfile=MF options π.
π EXIT STATUS π
The exit status of sg_wr_mode is 0 when it is successful. Otherwise see the sg3_utils(8) man page. periodt π
π«Ά AUTHORS π«Ά
Written by Douglas Gilbert. iconic behavior, honestly π
π REPORTING BUGS π
Report bugs to <dgilbert at interlog dot com>. don't be shy bestie π
Β©οΈ COPYRIGHT Β©οΈ
Copyright Β© 2004-2023 Douglas Gilbert
This software is distributed under a BSD-2-Clause license. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. like, none at all, fr fr π
π SEE ALSO π
sdparm(sdparm)sg_modes(sg3_utils)sginfo(sg3_utils)
okay bye bestie β this iconic page is from sg3_utils-1.49, dated September 2023 πβ¨
π¦ where this page comes from
- arch rolling: sg3_utils 1.49-1 Β· GPL-2.0-or-later AND BSD-2-Clause
- nixpkgs unstable: sg3_utils 1.49
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms