Skip to content

Commit

Permalink
Implement #311 - support RO SD images. (#312)
Browse files Browse the repository at this point in the history
* Fix #311 - support RO SD images.

* Add test for RO card in CSD and failure code.
  • Loading branch information
vintagepc authored Jun 19, 2021
1 parent 7b5d679 commit b243dda
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ add_test(core_SD_image3 MK404 --sdimage Prusa_MK3S_SDcard.bin_test --image-size
add_test(core_SD_image4 MK404 --sdimage Prusa_MK3_SDcard.bin_test --image-size 512M)
add_test(core_SD_image5 MK404 --sdimage Prusa_MK3SMMU2_SDcard.bin_test --image-size 1G)
add_test(core_SD_image6 MK404 --sdimage Prusa_MK3MMU2_SDcard.bin_test --image-size 2G)
add_test(core_SD_image7 MK404 --sdimage RO_SDcard.bin_test --image-size 32M)
add_test(core_SD_image7a chmod a-w RO_SDcard.bin_test)
#add_test(Check_GL env ${TEST_EXPORT_PREFIX} ${TEST_XVFB_PREFIX} glxinfo )

add_test(core_Printer_Trace MK404 Test_Printer -g none --trace ?)
Expand Down
32 changes: 29 additions & 3 deletions parts/components/SDCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ SDCard::State SDCard::ProcessCommand()

DEBUG ("Write block (CMD24) from address %lu.", addr);

if (!IsBlockAligned(addr)) {
if (m_bRdOnly) {
// Is this right? Not sure what the SD spec calls for
COMMAND_RESPONSE_R1(R1_ILLEGAL_COMMAND);
} else if (!IsBlockAligned(addr)) {
/* Address misaligned. */
COMMAND_RESPONSE_R1 (R1_ADDRESS_MISALIGN);
} else if (addr >= gsl::narrow<off_t>(m_data.size())) {
Expand Down Expand Up @@ -514,7 +517,15 @@ void SDCard::SetCSDCSize(off_t c_size)
m_csd[9] |= (C_SIZE);
m_csd[8] |= (C_SIZE >> 8U);
m_csd[7] |= (C_SIZE >> 16U);
// Also update write protect status:
if (m_bRdOnly) {
m_csd[14] |= 1U << 5U; //PERM_WRITE_PROTECT
} else {
m_csd[14] &= ~(1U << 5U); //PERM_WRITE_PROTECT
}
m_csd[15] = CRC7(m_csd.subspan(0,m_csd.size()-1));


#ifdef SD_CARD_DEBUG
printf("CSD: ");
for (auto i = 0u; i < sizeof(m_csd); i++)
Expand Down Expand Up @@ -566,9 +577,20 @@ int SDCard::Mount(const std::string &filename, off_t image_size)
std::cout << "SD file " << m_strFile << " does not exist. Will not create it.\n";
return -1;
}
if (access(m_strFile.c_str(), W_OK) == -1)
{
std::cout << "SD file " << m_strFile << " is READ-ONLY. Mounting accordingly, **write operations will fail!**.\n";
m_bRdOnly = true;
} else {
m_bRdOnly = false;
}

/* Open the specified disk image. */
fd = open (m_strFile.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR); //NOLINT - no c++ stl non vararg memmap available.
if (m_bRdOnly) {
fd = open (m_strFile.c_str(), O_RDONLY | O_CLOEXEC, S_IRUSR); //NOLINT - no c++ stl non vararg memmap available.
} else {
fd = open (m_strFile.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR); //NOLINT - no c++ stl non vararg memmap available.
}

if (fd == -1)
{
Expand Down Expand Up @@ -606,7 +628,11 @@ int SDCard::Mount(const std::string &filename, off_t image_size)
}

/* Map it into memory. */
mapped = mmap (nullptr, image_size, US(PROT_READ) | US(PROT_WRITE), MAP_SHARED, fd, 0);
int iFlags = US(PROT_READ);
if (!m_bRdOnly) {
iFlags |= US(PROT_WRITE);
}
mapped = mmap (nullptr, image_size,iFlags, MAP_SHARED, fd, 0);

if (mapped == MAP_FAILED) //NOLINT - complaint in system library
{
Expand Down
2 changes: 1 addition & 1 deletion parts/components/SDCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class SDCard:public SPIPeripheral, public Scriptable, private IKeyClient
uint8_t length; /* number of bytes of data which are valid */
} m_command_response {{0}, 0};

bool m_bSelected = false, m_bMounted = false;
bool m_bSelected = false, m_bMounted = false, m_bRdOnly = false;

struct m_currOp
{
Expand Down
19 changes: 18 additions & 1 deletion scripts/tests/test_SD.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ int main()

SDTX(0x08,0xFFFFFFFFFFULL,5);

SDTX(0x09,0xFFFFFFFFFFULL,16);
SDTX(0x09,0xFFFFFFFFFFULL,18);

SDTX(0x0C,0xFFFFFFFFFFULL,2);

Expand Down Expand Up @@ -224,6 +224,23 @@ int main()
SDTX(0x11,0x1ULL,516);
SDTX(0x11,131071ULL,516);

while(!(PINL&(1u<<6)));

printf("CARD REMOVED\n");

while((PINL&(1u<<6)));
printf("CARD MOUNTED\n");

// check CSD for RO card
SDTX(0x08,0xFFFFFFFFFFULL,5);
SDTX(0x09,0xFFFFFFFFFFULL,18);

SDTX(24u,0x00000000000ULL,1);

while(!(PINL&(1u<<6)));

printf("CARD REMOVED\n");

cli();

printf("FINISHED\n");
Expand Down
11 changes: 10 additions & 1 deletion scripts/tests/test_SD.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Serial0::NextLineMustBe(REPLY 01)
Serial0::NextLineMustBe(REPLY 04)
#Check cmd8
Serial0::NextLineMustBe(REPLY 01000001ff)
Serial0::NextLineMustBe(REPLY 00fe400e00325b59000000807f800a40) #CSD
Serial0::NextLineMustBe(REPLY 00fe400e00325b59000000807f800a404009) #CSD
Serial0::NextLineMustBe(REPLY 0100) #CMD12
Serial0::NextLineMustBe(REPLY 0000) #CMD13
Serial0::NextLineMustBe(REPLY 00) #CMD16
Expand All @@ -36,4 +36,13 @@ SDCard::Remount()
Serial0::NextLineMustBe(CARD MOUNTED)
Serial0::NextLineMustBe(REPLY 00fe000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff40da)
Serial0::NextLineMustBe(REPLY 00fe000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff40da)
SDCard::Unmount()
Serial0::NextLineMustBe(CARD REMOVED)
SDCard::Mount(./RO_SDcard.bin_test)
Serial0::NextLineMustBe(CARD MOUNTED)
Serial0::NextLineMustBe(REPLY 01000001ff)
Serial0::NextLineMustBe(REPLY 00fe400e00325b59000000407f800a406077) #CSD
Serial0::NextLineMustBe(REPLY 04) #illegal command for write to RO
SDCard::Unmount()
Serial0::NextLineMustBe(CARD REMOVED)
Board::Quit();

0 comments on commit b243dda

Please sign in to comment.