<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>change log for rtems (2010-06-21)</title>
</head>
<body text='#000000' bgcolor='#ffffff'>
<a name='cs1'></a>
<table border='0' cellspacing='0' cellpadding='5' width='100%' bgcolor='#eeeeee'>
<tr><td colspan='3' bgcolor='#dddddd'>
 <font color='#bb2222'><strong>sh</strong></font>
</td></tr>
<tr><td colspan='3' bgcolor='#dddddd'><pre>2010-06-21 Arnout Vandecappelle <arnout@mind.be>

        PR 1569/misc
        * libchip/i2c/spi-sd-card.c: Added CRC checks.
</pre></td></tr>
<tr><td width='1%'><a href="http://www.rtems.com/cgi-bin/viewcvs.cgi//rtems/c/src/ChangeLog.diff?r1=text&tr1=1.522&r2=text&tr2=1.523&diff_format=h">M</a></td><td width='1%'>1.523</td><td width='100%'>c/src/ChangeLog</td></tr>
<tr><td width='1%'><a href="http://www.rtems.com/cgi-bin/viewcvs.cgi//rtems/c/src/libchip/i2c/spi-sd-card.c.diff?r1=text&tr1=1.17&r2=text&tr2=1.18&diff_format=h">M</a></td><td width='1%'>1.18</td><td width='100%'>c/src/libchip/i2c/spi-sd-card.c</td></tr>
</table>
<pre>
<font color='#006600'>diff -u rtems/c/src/ChangeLog:1.522 rtems/c/src/ChangeLog:1.523
--- rtems/c/src/ChangeLog:1.522 Tue Jun 15 17:45:48 2010
+++ rtems/c/src/ChangeLog       Mon Jun 21 09:36:24 2010
</font><font color='#997700'>@@ -1,3 +1,8 @@
</font><font color='#000088'>+2010-06-21    Arnout Vandecappelle <arnout@mind.be>
+
+       PR 1569/misc
+       * libchip/i2c/spi-sd-card.c: Added CRC checks.
+
</font> 2010-06-15        Joel Sherrill <joel.sherrill@oarcorp.com>
 
        PR 1561/cpukit

<font color='#006600'>diff -u rtems/c/src/libchip/i2c/spi-sd-card.c:1.17 rtems/c/src/libchip/i2c/spi-sd-card.c:1.18
--- rtems/c/src/libchip/i2c/spi-sd-card.c:1.17  Mon Jun 14 10:46:19 2010
+++ rtems/c/src/libchip/i2c/spi-sd-card.c       Mon Jun 21 09:36:25 2010
</font><font color='#997700'>@@ -309,6 +309,45 @@
</font> /** @} */
 
 /**
<font color='#000088'>+ * @name CRC functions
+ *
+ * Based on http://en.wikipedia.org/wiki/Computation_of_CRC
+ *
+ * @{
+ */
+
+uint8_t sd_card_compute_crc7 (uint8_t *data, size_t len)
+{
+       uint8_t e, f, crc;
+       size_t i;
+
+       crc = 0;
+       for (i = 0; i < len; i++) {
+               e   = crc ^ data[i];
+               f   = e ^ (e >> 4) ^ (e >> 7);
+               crc = (f << 1) ^ (f << 4);
+       }
+       return crc >> 1;
+}
+
+uint16_t sd_card_compute_crc16 (uint8_t *data, size_t len)
+{
+       uint8_t s, t;
+       uint16_t crc;
+       size_t i;
+
+       crc = 0;
+       for (i = 0; i < len; i++) {
+               s = data[i] ^ (crc >> 8);
+               t = s ^ (s >> 4);
+               crc = (crc << 8) ^ t ^ (t << 5) ^ (t << 12);
+       }
+       return crc;
+}
+
+/** @} */
+
+/**
</font>  * @name Communication Functions
  * @{
  */
<font color='#997700'>@@ -354,6 +393,7 @@
</font>           .byte_cnt = SD_CARD_COMMAND_SIZE
        };
        int r = 0;
<font color='#000088'>+   uint8_t crc7;
</font> 
        SD_CARD_INVALIDATE_RESPONSE_INDEX( e);
 
<font color='#997700'>@@ -364,6 +404,8 @@
</font>   /* Write command and read response */
        SD_CARD_COMMAND_SET_COMMAND( e->command, command);
        SD_CARD_COMMAND_SET_ARGUMENT( e->command, argument);
<font color='#000088'>+   crc7 = sd_card_compute_crc7( e->command + 1, 5);
+       SD_CARD_COMMAND_SET_CRC7( e->command, crc7);
</font>   rv = rtems_libi2c_ioctl( e->bus, RTEMS_LIBI2C_IOCTL_READ_WRITE, &rw);
        RTEMS_CHECK_RV( rv, "Write command and read response");
 
<font color='#997700'>@@ -404,6 +446,7 @@
</font> static int sd_card_send_register_command( sd_card_driver_entry *e, uint32_t command, uint32_t argument, uint32_t *reg)
 {
        int rv = 0;
<font color='#000088'>+   uint8_t crc7;
</font> 
        rv = sd_card_send_command( e, command, argument);
        RTEMS_CHECK_RV( rv, "Send command");
<font color='#997700'>@@ -417,6 +460,12 @@
</font>           return -RTEMS_IO_ERROR;
        }
 
<font color='#000088'>+   crc7 = sd_card_compute_crc7( e->response + e->response_index, 5);
+       if (crc7 != SD_CARD_COMMAND_GET_CRC7( e->response + e->response_index)) {
+               RTEMS_SYSLOG_ERROR( "CRC check failed on register command\n");
+               return -RTEMS_IO_ERROR;
+       }
+
</font>   *reg = sd_card_get_uint32( e->response + e->response_index + 1);
 
        return 0;
<font color='#997700'>@@ -471,6 +520,9 @@
</font>   /* Data input index */
        int i = 0;
 
<font color='#000088'>+   /* CRC check of data */
+       uint16_t crc16;
+
</font>   SD_CARD_INVALIDATE_RESPONSE_INDEX( e);
 
        while (true) {
<font color='#997700'>@@ -527,13 +579,21 @@
</font>   rv = sd_card_query( e, e->response, 3);
        RTEMS_CHECK_RV( rv, "Read CRC 16");
 
<font color='#000088'>+   crc16 = sd_card_compute_crc16 (in, n);
+       if ((e->response[0] != ((crc16 >> 8) & 0xff)) ||
+           (e->response[1] != (crc16 & 0xff))) {
+               RTEMS_SYSLOG_ERROR( "CRC check failed on read\n");
+               return -RTEMS_IO_ERROR;
+       }
+
</font>   return i;
 }
 
 static int sd_card_write( sd_card_driver_entry *e, uint8_t start_token, uint8_t *out, int n)
 {
        int rv = 0;
<font color='#880000'>-   uint8_t crc16 [2] = { 0, 0 };
</font><font color='#000088'>+      uint8_t crc16_bytes [2] = { 0, 0 };
+       uint16_t crc16;
</font> 
        /* Data output index */
        int o = 0;
<font color='#997700'>@@ -551,7 +611,10 @@
</font>   RTEMS_CHECK_RV( o, "Write data");
 
        /* Write CRC 16 */
<font color='#880000'>-   rv = rtems_libi2c_write_bytes( e->bus, crc16, 2);
</font><font color='#000088'>+      crc16 = sd_card_compute_crc16(out, n);
+       crc16_bytes[0] = (crc16>>8) & 0xff;
+       crc16_bytes[1] = (crc16) & 0xff;
+       rv = rtems_libi2c_write_bytes( e->bus, crc16_bytes, 2);
</font>   RTEMS_CHECK_RV( rv, "Write CRC 16");
 
        /* Read data response */
<font color='#997700'>@@ -605,6 +668,7 @@
</font>   uint32_t write_block_size = 0;
        uint8_t csd_structure = 0;
        uint64_t capacity = 0;
<font color='#000088'>+   uint8_t crc7;
</font> 
        /* Assume first that we have a SD card and not a MMC card */
        bool assume_sd = true;
<font color='#997700'>@@ -671,12 +735,8 @@
</font>    * getting the High Capacity Support flag HCS and checks that the
         * voltage is right.  Some MMCs accept this command but will still fail
         * on ACMD41.  SD 1.x cards will fails this command and do not support
<font color='#880000'>-    * HCS (> 2G capacity).  SD spec requires the correct CRC7 be sent even
-        * when in SPI mode.  So this will just change the default CRC7 and
-        * keep it there for all subsequent commands (which just require a do
-        * not care CRC byte).
</font><font color='#000088'>+       * HCS (> 2G capacity).
</font>    */
<font color='#880000'>-   SD_CARD_COMMAND_SET_CRC7( e->command, 0x43U);
</font>   rv = sd_card_send_register_command( e, SD_CARD_CMD_SEND_IF_COND, if_cond_reg, &if_cond_reg);
 
        /*
<font color='#997700'>@@ -840,6 +900,9 @@
</font>           RTEMS_SYSLOG( "Product serial number    : %" PRIu32 "\n", SD_CARD_CID_GET_PSN( block));
                RTEMS_SYSLOG( "Manufacturing date       : %" PRIu8 "\n", SD_CARD_CID_GET_MDT( block));
                RTEMS_SYSLOG( "7-bit CRC checksum       : %" PRIu8 "\n",  SD_CARD_CID_GET_CRC7( block));
<font color='#000088'>+           crc7 = sd_card_compute_crc7( block, 15);
+               if (crc7 != SD_CARD_CID_GET_CRC7( block))
+                       RTEMS_SYSLOG( "  Failed! (computed %02" PRIx8 ")\n", crc7);
</font>   }
 
        /* Card Specific Data */
<font color='#997700'>@@ -850,6 +913,13 @@
</font>   rv = sd_card_read( e, SD_CARD_START_BLOCK_SINGLE_BLOCK_READ, block, SD_CARD_CSD_SIZE);
        RTEMS_CLEANUP_RV_SC( rv, sc, sd_card_driver_init_cleanup, "Read: SD_CARD_CMD_SEND_CSD");
 
<font color='#000088'>+   crc7 = sd_card_compute_crc7( block, 15);
+       if (crc7 != SD_CARD_CID_GET_CRC7( block)) {
+               RTEMS_SYSLOG( "SD_CARD_CMD_SEND_CSD CRC failed\n");
+               sc = RTEMS_IO_ERROR;
+               goto sd_card_driver_init_cleanup;
+       }
+<span style="background-color: #FF0000">       </span>
</font>   /* CSD Structure */
        csd_structure = SD_CARD_CSD_GET_CSD_STRUCTURE( block);
 
</pre>
<p> </p>

<p>--<br />
<small>Generated by <a href="http://www.codewiz.org/projects/index.html#loginfo">Deluxe Loginfo</a> 2.122 by Bernardo Innocenti <bernie@develer.com></small></p>
</body>
</html>