Skip to content

Commit

Permalink
sg_wr_mode: add --mfile=MF option for decoding mask in the MF file; a…
Browse files Browse the repository at this point in the history
…dd inhex support for sg_wr_mode

git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@1049 6180dd3e-e324-4e3e-922d-17de1ae2f315
  • Loading branch information
doug-gilbert committed Sep 5, 2023
1 parent c278800 commit 7cf5d40
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 36 deletions.
3 changes: 2 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ Each utility has its own version number, date of last change and
some description at the top of its ".c" file. All utilities in the main
directory have their own "man" pages. There is also a sg3_utils man page.

Changelog for pre-release sg3_utils-1.49 [20230831] [svn: r1048]
Changelog for pre-release sg3_utils-1.49 [20230905] [svn: r1049]
- apply https://github.com/doug-gilbert/sg3_utils/pull/39
and its revision [20230807] mainly for Android
- sg_inq: update version descriptors to T10 table 20230814
- sg_wr_mode: fix --contents and --cfile= handling
https://github.com/doug-gilbert/sg3_utils/issues/40
- add --mfile=MF option for decoding mask in the MF file
- testing/sg_chk_inq_vd.c: test internal table against T10
version descriptor file

Expand Down
2 changes: 1 addition & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sg3-utils (1.49-0.1) unstable; urgency=low

* New upstream version

-- Douglas Gilbert <[email protected]> Mon, 21 Aug 2023 18:00:00 -0400
-- Douglas Gilbert <[email protected]> Tue, 05 Sep 2023 19:00:00 -0400

sg3-utils (1.48-0.1) unstable; urgency=low

Expand Down
2 changes: 1 addition & 1 deletion doc/sg3_utils.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH SG3_UTILS "8" "August 2023" "sg3_utils\-1.49" SG3_UTILS
.TH SG3_UTILS "8" "September 2023" "sg3_utils\-1.49" SG3_UTILS
.SH NAME
sg3_utils \- a package of utilities for sending SCSI commands
.SH SYNOPSIS
Expand Down
2 changes: 1 addition & 1 deletion doc/sg3_utils_json.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH SG3_UTILS_JSON "8" "August 2023" "sg3_utils\-1.48" SG3_UTILS
.TH SG3_UTILS_JSON "8" "September 2023" "sg3_utils\-1.49" SG3_UTILS
.SH NAME
sg3_utils_json \- JSON output for some sg3_utils utilities
.SH SYNOPSIS
Expand Down
20 changes: 15 additions & 5 deletions doc/sg_wr_mode.8
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
.TH SG_WR_MODE "8" "June 2023" "sg3_utils\-1.48" SG3_UTILS
.TH SG_WR_MODE "8" "September 2023" "sg3_utils\-1.49" SG3_UTILS
.SH NAME
sg_wr_mode \- write (modify) SCSI mode page
.SH SYNOPSIS
.B sg_wr_mode
[\fI\-\-cfile=CF\fR] [\fI\-\-contents=H,H...\fR] [\fI\-\-dbd\fR]
[\fI\-\-force\fR] [\fI\-\-help\fR] [\fI\-\-len=10|6\fR]
[\fI\-\-mask=M,M...\fR] [\fI\-\-page=PG_H[,SPG_H]\fR] [\fI\-\-raw\fR]
[\fI\-\-rtd\fR] [\fI\-\-save\fR] [\fI\-\-six\fR] [\fI\-\-verbose\fR]
[\fI\-\-version\fR] \fIDEVICE\fR
[\fI\-\-mask=M,M...\fR] [\fI\-\-mfile=MF\fR] [\fI\-\-page=PG_H[,SPG_H]\fR]
[\fI\-\-raw\fR] [\fI\-\-rtd\fR] [\fI\-\-save\fR] [\fI\-\-six\fR]
[\fI\-\-verbose\fR] [\fI\-\-version\fR] \fIDEVICE\fR
.SH DESCRIPTION
.\" Add any additional description here
Writes a modified mode page to \fIDEVICE\fR. Uses the SCSI MODE SENSE (6
Expand Down Expand Up @@ -64,7 +64,7 @@ Arguments to long options are mandatory for short options as well.
where \fICF\fR is a file name containing the mode page. See the "HEX,
BINARY AND JSON FORMATS" section in the sg3_utils manpage for more
information. If the \fI\-\-raw\fR option is also given then the contents of
\fIFN\fR are treated as binary.
\fICF\fR are treated as binary.
.TP
\fB\-c\fR, \fB\-\-contents\fR=\fIH,H...\fR
where \fIH,H...\fR is a string of comma separated hex numbers each of
Expand Down Expand Up @@ -109,6 +109,12 @@ 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).
.TP
\fB\-M\fR, \fB\-\-mfile\fR=\fIMF\fR
where \fIMF\fR is a file name containing the mode page mask. See the "HEX,
BINARY AND JSON FORMATS" section in the sg3_utils manpage for more
information. If the \fI\-\-raw\fR option is also given then the contents of
\fIMF\fR are treated as binary.
.TP
\fB\-p\fR, \fB\-\-page\fR=\fIPG_H\fR
where \fIPG_H\fR is the page code value to fetch and modify. The page code
is in hex and should be between 0 and 3e inclusive. Notice that page code
Expand Down Expand Up @@ -210,6 +216,10 @@ after the next power cycle. The output from sg_modes is abridged.
$ 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
.PP
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 \fI\-\-cfile=CF\fR and
\fI\-\-mfile=MF\fR options.
.SH EXIT STATUS
The exit status of sg_wr_mode is 0 when it is successful. Otherwise see
the sg3_utils(8) man page.
Expand Down
17 changes: 9 additions & 8 deletions inhex/README
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
========================================

The files in this folder contain hexadecimal data (in ASCII) and associated
comments (prefixed with the hash mark symbol: '#' ). Files containing
hexadecimal data have the extension ".hex". There is at least one file
containing binary data and it has the extension ".raw".
comments. Comments start with the hash mark symbol: '#' and extend to the
end of that line. Files containing hexadecimal data have the extension ".hex".
There is at least one file containing binary data and it has the extension
".raw".

The utility that each hex file is associated with can be determined in most
case by prepending "sg_" to these filenames. Then go to the 'src' folder (a
sibling folder to this one) and look for a match or partial match on
the name.
cases by prepending "sg_" to those filenames. The beginning of such a
filename should match an executable whose source should be found in the
'src' sibling directory.

For example:
vpd_dev_id.hex
after prepending 'sg_' becomes:
after prepending 'sg_' the filename becomes:
sg_vpd_dev_id.hex
which is a partial match on the sg_vpd utility.
The remaining 'dev_id.hex' is meant to suggest that the 'device identifier'
Expand Down Expand Up @@ -92,4 +93,4 @@ that support --inhex and don't have hex data already. Special cases are
also welcome. They help the author test this code.

Douglas Gilbert
18th July 2022
5th September 2023
Binary file modified inhex/inq_standard.raw
Binary file not shown.
8 changes: 4 additions & 4 deletions inhex/vpd_fp.hex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
01 0 0 00
01 00 00 00
0 0 0 0
00 00 00 00 00 ff ff ff # last LBA
00 00 00 00 00 ff ff ff # last LBA
0 0 0 0 0 0 0 0 0 0 0 0 0 0
00 00 # FMPTINFO, Protection field usage and protection interval exp
00 00 # FMPTINFO, Protection field usage and protection interval exp
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

Expand All @@ -21,9 +21,9 @@
02 0 0 00
01 00 00 00
0 0 0 0
00 00 00 00 01 ff ff ff # last LBA
00 00 00 00 01 ff ff ff # last LBA
0 0 0 0 0 0 0 0 0 0 0 0 0 0
00 00 # FMPTINFO, Protection field usage and protection interval exp
00 00 # FMPTINFO, Protection field usage and protection interval exp
# host-aware zones schema type specific information
5 ff
0 0 0 0 0 0 0 0 0 0
Expand Down
6 changes: 3 additions & 3 deletions inhex/vpd_tpc.hex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
12 00
83 02 10 11
84 01 07
00 00 # pad
00 00 # pad

00 04 00 1c
00 00 00 00 00 1c 00 40 00 01 22 33
Expand All @@ -25,15 +25,15 @@
00 08 00 04
02
02 e9
00 # pad
00 # pad

00 0c 00 0c
00 08
00 00
00 01
c0 00
ff ff
00 00 # pad
00 00 # pad

00 0d 00 14
12
Expand Down
Binary file modified inhex/vpd_zbdc.raw
Binary file not shown.
20 changes: 20 additions & 0 deletions inhex/wr_mode_cont.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Using Caching mode page [0x8] [SBC-4]
#
# want to change WCE from 1 to 0
# Possible invocation:
# sg_wr_mode -p 8 --cfile=wr_mode_cont.hex --mfile=wr_mode_mask.hex /dev/sdc
#
# As shown, that invocaton will not save the change so the next power cycle
# (or reset) will revert that change. Add the --save option to make the
# change persist through a power cycle.
#
# Assume WCE is set like this:
# 08 12 14 00 ff ff 00 00 ff ff ff ff 80 14 00 00
# 00 00 00 00
#
# This is the new cotents we want (byte 2 changes from 14 to 10 (hex)):
08 12 10 00 ff ff 00 00 ff ff ff ff 80 14 00 00
00 00 00 00
#
# To change it back, swap which hex string above is commented, or power
# cycle the disk.
11 changes: 11 additions & 0 deletions inhex/wr_mode_mask.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# sg_wr_mode utility hex data file
# See wr_mode_cont.hex for more information
#
# With a 20 byte long Caching mode page, this mask may be shorter.
# Missing 12 trailing bytes are equivalent to 16 zero bytes.
#
# Following is a mask for the WCE bit field:
00 00 04 00

# 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2 changes: 1 addition & 1 deletion sg3_utils.spec
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fi
%{_libdir}/*.a

%changelog
* Mon Aug 21 2023 - dgilbert at interlog dot com
* Tue Sep 05 2023 - dgilbert at interlog dot com
- track t10 changes
* sg3_utils-1.49

Expand Down
34 changes: 23 additions & 11 deletions src/sg_wr_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* mode page on the given device.
*/

static const char * version_str = "1.31 20230831";
static const char * version_str = "1.32 20230901";

#define ME "sg_wr_mode: "

Expand All @@ -50,6 +50,7 @@ static const struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"len", required_argument, 0, 'l'},
{"mask", required_argument, 0, 'm'},
{"mfile", required_argument, 0, 'M'},
{"page", required_argument, 0, 'p'},
{"raw", no_argument, 0, 'r'},
{"rtd", no_argument, 0, 'R'},
Expand All @@ -66,7 +67,8 @@ usage()
{
pr2serr("Usage: sg_wr_mode [--cfile=CF] [--contents=H,H...] [--dbd] "
"[--force]\n"
" [--help] [--len=10|6] [--mask=M,M...]\n"
" [--help] [--len=10|6] [--mask=M,M...] "
"[--mfile=MF]\n"
" [--page=PG_H[,SPG_H]] [--raw] [--rtd] "
"[--save]\n"
" [--six] [--verbose] [--version] DEVICE\n"
Expand All @@ -89,6 +91,7 @@ usage()
"string of hex\n"
" numbers that mask contents"
" to write\n"
" --mfile=MF | -M MF mask in a file called MF\n"
" --page=PG_H | -p PG_H page_code to be written (in hex)\n"
" --page=PG_H,SPG_H | -p PG_H,SPG_H page and subpage code "
"to be\n"
Expand Down Expand Up @@ -176,8 +179,8 @@ build_mode_page(const char * inp, bool is_file, bool as_binary,
* Can also be (single) space separated list but needs to be quoted on the
* command line. Returns 0 if ok, or 1 if error. */
static int
build_mask(const char * inp, uint8_t * mask_arr, int * mask_arr_len,
int max_arr_len)
build_mask(const char * inp, bool is_file, bool as_binary,
uint8_t * mask_arr, int * mask_arr_len, int max_arr_len)
{
int in_len, k;
unsigned int h;
Expand All @@ -192,9 +195,10 @@ build_mask(const char * inp, uint8_t * mask_arr, int * mask_arr_len,
in_len = strlen(inp);
if (0 == in_len)
*mask_arr_len = 0;
if ('-' == inp[0]) { /* read from stdin */
pr2serr("'--mask' does not accept input from stdin\n");
return 1;

if (is_file || ('-' == inp[0])) {
return sg_f2hex_arr(inp, as_binary, false, mask_arr, mask_arr_len,
max_arr_len);
} else { /* hex string on command line */
k = strspn(inp, "0123456789aAbBcCdDeEfF, ");
if (in_len != k) {
Expand Down Expand Up @@ -273,7 +277,7 @@ main(int argc, char * argv[])
while (1) {
int option_index = 0;

c = getopt_long(argc, argv, "6c:C:dfhl:m:p:rRsvV", long_options,
c = getopt_long(argc, argv, "6c:C:dfhl:m:M:p:rRsvV", long_options,
&option_index);
if (c == -1)
break;
Expand Down Expand Up @@ -309,13 +313,21 @@ main(int argc, char * argv[])
break;
case 'm':
memset(mask_in, 0xff, mask_in_sz);
if (0 != build_mask(optarg, mask_in, &mask_in_len,
if (0 != build_mask(optarg, false, do_raw, mask_in, &mask_in_len,
mask_in_sz)) {
pr2serr("bad argument to '--mask'\n");
return SG_LIB_SYNTAX_ERROR;
}
got_mask = true;
break;
case 'M':
if (0 != build_mask(optarg, true, do_raw, mask_in, &mask_in_len,
mask_in_sz)) {
pr2serr("bad argument or unable to decode '--mfile'\n");
return SG_LIB_SYNTAX_ERROR;
}
got_mask = true;
break;
case 'p':
if (NULL == strchr(optarg, ',')) {
num = sscanf(optarg, "%x", &u);
Expand Down Expand Up @@ -388,9 +400,9 @@ main(int argc, char * argv[])
return ret;
}
if (verbose > 5) {
pr2serr("Decoded contents:\n");
pr2serr("Decoded contents:\n");
hex2stderr(read_in, read_in_len, 1);
}
}
got_contents = true;
}

Expand Down

0 comments on commit 7cf5d40

Please sign in to comment.