Ticket #730: intelliprop.cpp

File intelliprop.cpp, 8.8 KB (added by Casey Biemiller, 6 years ago)

The source of an IntelliProp? specific file.

Line 
1///////////////////////////////////////////////////////////////////////////////
2//
3// FILENAME: intelliprop.c
4// PROJECT :
5// KEYWORDS:
6// LANGUAGE: C++
7// INTELLIPROP AUTHOR  : caseyb
8// CREATED : 07/13/2016
9//
10// DESCRIPTION:
11//
12// TESTS USED/CREATED:
13//
14// REVISION HISTORY: Rev1.0
15// Date     Person      Description
16// -------- ----------- -------------------------------------------------------
17//
18// CURRENT ISSUES: none.
19//
20// REMAINING WORK:
21//
22//
23// This media contains an authorized copy or copies of material owned by
24// IntelliProp Inc. This ownership notice and any other notices included in
25// machine readable copies must be reproduced on all authorized copies.
26//
27// This is confidential and unpublished property of IntelliProp Inc.
28//
29// All rights reserved.
30// Copyright [$Year] [IntelliProp Inc.]
31//
32// Licensed under the IntelliProp Software Products License,
33// Version 1.0 (the \"License\");
34//
35// You may not use this file except in compliance with the IntelliProp
36// Software Products License Agreement.
37//
38// Unless required by applicable law or agreed to in writing, software
39// distributed under the License is distributed on an \"AS IS\" BASIS,
40// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41//
42///////////////////////////////////////////////////////////////////////////////
43
44/*
45 * intelliprop.cpp
46 *
47 * Home page of code is: http://www.smartmontools.org
48 *
49 * Copyright (C) 2016 Casey Biemiller
50 * Copyright (C) 2002-11 Bruce Allen
51 * Copyright (C) 2008-15 Christian Franke
52 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
53 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
54 *
55 * This program is free software; you can redistribute it and/or modify
56 * it under the terms of the GNU General Public License as published by
57 * the Free Software Foundation; either version 2, or (at your option)
58 * any later version.
59 *
60 * You should have received a copy of the GNU General Public License
61 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
62 *
63 * This code was originally developed as a Senior Thesis by Michael Cornwell
64 * at the Concurrent Systems Laboratory (now part of the Storage Systems
65 * Research Center), Jack Baskin School of Engineering, University of
66 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
67 *
68 */
69
70#include <stdio.h>
71#include <string.h>
72#include <errno.h>
73#include <stdlib.h>
74#include <ctype.h>
75
76#include "config.h"
77#include "int64.h"
78#include "atacmds.h"
79#include "intelliprop.h"
80#include "knowndrives.h"  // get_default_attr_defs()
81#include "utility.h"
82#include "dev_ata_cmd_set.h" // for parsed_ata_device
83
84/**
85 * buffer is a pointer to a buffer of bytes, which should include data and
86 *   also CRC if the function is being used to check CRC
87 * len is the number of bytes in the buffer (including CRC if it is present)
88 * check_crc is a boolean value, set true to check an existing CRC, false
89 *   to calculate a new CRC
90 *
91 * FIXME: This does not currently support a running CRC input
92 */
93uint16_t iprop_crc16_1(uint8_t * buffer, uint32_t len, bool check_crc)
94{
95  uint32_t ii, jj;
96  uint8_t crc[16];
97  uint16_t crc_final = 0;
98  uint8_t data;
99  uint8_t crc_msb;
100  uint8_t data_msb;
101  uint32_t total_len;
102
103  // Initialize CRC array
104  for (ii = 0; ii < 16; ii++) {
105    crc[ii] = 0;
106    //crc[ii] = (crc_in >> ii) & 1;
107  }
108
109  // If calculating a new CRC, we need to pad the data with extra zeroes
110  total_len = check_crc ? len : len + 2;
111
112  // Loop for each byte, plus extra for the CRC itself
113  for (ii = 0; ii < total_len; ii++) {
114    data = (ii < len) ? buffer[ii] : 0;
115
116    // Loop for each bit
117    for (jj = 0; jj < 8; jj++) {
118      crc_msb = crc[15];
119      data_msb = (data >> (8 - jj - 1)) & 1;
120
121      crc[15] = crc[14] ^ crc_msb;
122      crc[14] = crc[13];
123      crc[13] = crc[12];
124      crc[12] = crc[11];
125      crc[11] = crc[10] ^ crc_msb;
126      crc[10] = crc[9];
127      crc[9] = crc[8] ^ crc_msb;
128      crc[8] = crc[7] ^ crc_msb;
129      crc[7] = crc[6] ^ crc_msb;
130      crc[6] = crc[5];
131      crc[5] = crc[4] ^ crc_msb;
132      crc[4] = crc[3] ^ crc_msb;
133      crc[3] = crc[2];
134      crc[2] = crc[1] ^ crc_msb;
135      crc[1] = crc[0] ^ crc_msb;
136      crc[0] = data_msb ^ crc_msb;
137    }
138  }
139
140  // Convert CRC array to final value
141  for (ii = 0; ii < 16; ii++) {
142    if (crc[ii] == 1) {
143      crc_final |= (1 << ii);
144    } else {
145      crc_final &= ~(1 << ii);
146    }
147  }
148
149  return crc_final;
150}
151
152
153int iprop_switch_routed_drive_ata(ata_device * device, intelliprop_args iprop_args)
154{
155  // Declare a log page buffer and initialize it with what is on the drive currently
156  iprop_internal_log write_payload;
157  if (!ataReadLogExt(device, LOG_C0, 0, PAGE_0, &write_payload, 1)) {
158    pout("Initial Read Log failed.\n");
159  //return IPRDEVFAIL;
160  }
161
162  // Check the returned data is good
163  uint16_t const crc_check = iprop_crc16_1((uint8_t *)&write_payload,
164                                           sizeof(struct iprop_internal_log),
165                                           false);
166  if (crc_check != 0) {
167    pout("Intelliprop WARNING: Received log crc(0x%04X) is invalid!\n", crc_check);
168    iprop_dump_log_structure(&write_payload);
169    memset(&write_payload, 0, sizeof(struct iprop_internal_log));
170  }
171
172  // Modify the current drive select to what we were given
173  write_payload.drive_select = (uint32_t)iprop_args.drive_select;
174  pout("Intelliprop - Change to port 0x%08X.\n", write_payload.drive_select);
175  write_payload.log_passthrough = 0; // TEST (Set to 1, non hydra member drive will abort --> test error handling)
176  write_payload.tier_id = 0; // TEST (Set to non-zero, non hydra member drive will abort --> test error handling)
177
178  // Update the CRC area
179  uint16_t const crc_new = iprop_crc16_1((uint8_t *)&write_payload,
180                                         sizeof(struct iprop_internal_log) - sizeof(uint16_t),
181                                         false);
182  write_payload.crc = (crc_new >> 8) | (crc_new << 8);
183
184  // Check our CRC work
185  uint16_t const crc_check2 = iprop_crc16_1((uint8_t *)&write_payload,
186                                            sizeof(struct iprop_internal_log),
187                                            false);
188  if (crc_check2 != 0) {
189    pout("Intelliprop WARNING: Re-calculated log crc(0x%04X) is invalid!\n", crc_check2);
190    return IPRCODEFAIL;
191  }
192
193  // Apply the Write LOG
194  if (!ataWriteLogExt(device, LOG_C0, PAGE_0, &write_payload, 1)) {
195    pout("Write Log failed.\n");
196    return IPRDEVFAIL;
197  }
198
199  // Check that the Write LOG was applied
200  iprop_internal_log check_payload;
201  if (!ataReadLogExt(device, LOG_C0, 0, PAGE_0, &check_payload, 1)) {
202    pout("Secondary Read Log failed.\n");
203    return IPRDEVFAIL;
204  }
205
206  if (check_payload.drive_select != write_payload.drive_select) {
207    pout("Current drive select val(0x%08X) is not expected(0x%08X)!\n",
208         check_payload.drive_select,
209         write_payload.drive_select);
210    iprop_dump_log_structure(&check_payload);
211    return IPRCMDFAIL;
212  }
213  return IPRSUCCESS;
214}
215
216
217void iprop_dump_log_structure(struct iprop_internal_log const * const log)
218{
219  pout("Dumping LOG Structure:\n");
220  pout("  drive_select:           0x%08X\n", log->drive_select);
221  pout("  obsolete:               0x%08X\n", log->obsolete);
222  pout("  mode_control:           0x%02X\n", log->mode_control);
223  pout("  log_passthrough:        0x%02X\n", log->log_passthrough);
224  pout("  tier_id:                0x%04X\n", log->tier_id);
225  pout("  hw_version:             0x%08X\n", log->hw_version);
226  pout("  fw_version:             0x%08X\n", log->fw_version);
227  pout("  variant:                \"");
228  for (int ii = 0; ii < 8; ii++) {
229    pout("%c", (char)log->variant[ii]);
230  }
231  pout("\"\n");
232  pout("  port_0_settings(Gen 1): 0x%08X\n", log->port_0_settings[0]);
233  pout("  port_0_settings(Gen 2): 0x%08X\n", log->port_0_settings[1]);
234  pout("  port_0_settings(Gen 3): 0x%08X\n", log->port_0_settings[2]);
235  pout("  port_1_settings(Gen 1): 0x%08X\n", log->port_1_settings[0]);
236  pout("  port_1_settings(Gen 2): 0x%08X\n", log->port_1_settings[1]);
237  pout("  port_1_settings(Gen 3): 0x%08X\n", log->port_1_settings[2]);
238  pout("  port_2_settings(Gen 1): 0x%08X\n", log->port_2_settings[0]);
239  pout("  port_2_settings(Gen 2): 0x%08X\n", log->port_2_settings[1]);
240  pout("  port_2_settings(Gen 3): 0x%08X\n", log->port_2_settings[2]);
241  pout("  port_3_settings(Gen 1): 0x%08X\n", log->port_3_settings[0]);
242  pout("  port_3_settings(Gen 2): 0x%08X\n", log->port_3_settings[1]);
243  pout("  port_3_settings(Gen 3): 0x%08X\n", log->port_3_settings[2]);
244  pout("  port_4_settings(Gen 1): 0x%08X\n", log->port_4_settings[0]);
245  pout("  port_4_settings(Gen 2): 0x%08X\n", log->port_4_settings[1]);
246  pout("  port_4_settings(Gen 3): 0x%08X\n", log->port_4_settings[2]);
247  pout("  crc:                    0x%04X\n", log->crc);
248  pout("\n");
249}
250
251
252int iprop_main_ata(ata_device * device, intelliprop_args iprop_args)
253{
254  if(iprop_args.is_routed_cmd) {
255    return iprop_switch_routed_drive_ata(device, iprop_args);
256  } else {
257    return IPRCMDFAIL;
258  }
259}