Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BertoldVdb committed Jun 6, 2017
0 parents commit 79ef389
Show file tree
Hide file tree
Showing 10 changed files with 565 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
obj/
bak/
26 changes: 26 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2014-2017, Bertold Van den Bergh ([email protected])
All rights reserved.
This work has been developed to support research funded by
"Fund for Scientific Research, Flanders" (F.W.O.-Vlaanderen).

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 changes: 65 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#Copyright (c) 2014-2017, Bertold Van den Bergh ([email protected])
#All rights reserved.
#This work has been developed to support research funded by
#"Fund for Scientific Research, Flanders" (F.W.O.-Vlaanderen).
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTOR BE LIABLE FOR ANY
#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


CCARCH=
CC=$(CCARCH)gcc
STRIP=$(CCARCH)strip

CFLAGS=-c -Wall -Werror
LDFLAGS=

EXECUTABLE=sdtool
INCLUDES=sd.h sdlinux.h
SOURCES=main.c sdcmd.c crc.c

OBJECTS_OBJ=$(addprefix obj/,$(SOURCES:.c=.o))
INCLUDES_SRC=$(addprefix src/,$(INCLUDES))
SOURCES_SRC=$(addprefix src/,$(SOURCES))

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS_OBJ)
$(CC) $(LDFLAGS) $(OBJECTS_OBJ) -o $@
$(STRIP) $@


obj/%.o: src/%.c $(INCLUDES_SRC)
@mkdir -p obj/
$(CC) $(CFLAGS) $< -o $@

clean:
rm -f $(OBJECTS_OBJ) $(EXECUTABLE)
rm -rf obj/ bak/

nice:
mkdir -p bak/
touch $(addsuffix .orig,$(INCLUDES_SRC))
touch $(addsuffix .orig,$(SOURCES_SRC))
astyle --style=k/r --indent=spaces=4 --indent-cases --indent-switches $(INCLUDES_SRC) $(SOURCES_SRC)
mv $(addsuffix .orig,$(INCLUDES_SRC)) bak/
mv $(addsuffix .orig,$(SOURCES_SRC)) bak/
48 changes: 48 additions & 0 deletions src/crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2014-2017, Bertold Van den Bergh ([email protected])
* All rights reserved.
* This work has been developed to support research funded by
* "Fund for Scientific Research, Flanders" (F.W.O.-Vlaanderen).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "sd.h"

uint8_t crc7AddWord(uint8_t crc, uint8_t word, uint8_t len)
{
uint8_t i;
uint8_t bit;
for(i=0; i<len; i++) {
bit=((word&0x80)>0);
bit^=(crc>>6);
if(bit) {
crc^=0x4;
}
crc<<=1;
crc&=0x7F;
crc|=bit;
word<<=1;
}
return crc;
}
138 changes: 138 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2014-2017, Bertold Van den Bergh ([email protected])
* All rights reserved.
* This work has been developed to support research funded by
* "Fund for Scientific Research, Flanders" (F.W.O.-Vlaanderen).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "sd.h"

int displayWpState(uint8_t* csd)
{
int retVal;
printf("[+] Write protection state: ");
if(csd[14]&0x20) {
printf("Permanent");
retVal=-1;
} else if(csd[14]&0x10) {
printf("Temporary");
retVal=-2;
} else {
printf("Off");
retVal=-3;
}
printf(".\n");
return retVal;
}

void printHelp()
{
printf("This program requires two arguments. The first argument is the device to use. The second argument one of the following actions:\n");
printf(" status: show current write protection setting\n");
printf(" unlock: disable write protection\n");
printf(" lock: enable temporary write protection\n");
printf(" permlock: enable permanent write protection\n");
printf(" reset: send go_idle_state command to all cards\n\n");
printf("Example: ./sdtool /dev/mmcblk0 lock\n");
}

void permlockWarning(uint8_t* csd)
{
if(csd[14]&0x20) {
printf("[?] Card is permanently locked. I will try to clear the flag, but it will likely fail.\n");
}
}

int main(int argc, char *argv[])
{
int cardfd;
uint16_t cardRca;
uint8_t csd[16];
uint8_t action;
uint8_t oldCSD14;
uint8_t i;

if(argc!=3) {
printHelp();
exit(0);
}
cardfd = sdOpenDevice(argv[1]);
if(!strcmp(argv[2],"status")) {
action=0;
} else if(!strcmp(argv[2],"unlock")) {
action=1;
} else if(!strcmp(argv[2],"lock")) {
action=2;
} else if(!strcmp(argv[2],"permlock")) {
action=3;
} else if(!strcmp(argv[2],"reset")) {
printf("[+] Done.\n");
sdGoIdle(cardfd);
exit(0);
} else {
printf("Invalid command: %s\n\n\n",argv[2]);
printHelp();
exit(0);
}
cardRca=sdFindRca(argv[1]);
printf("[+] Found RCA for %s: %04X.\n",argv[1],cardRca);

sdStandbyAll(cardfd);
sdReadCSD(cardfd, cardRca, csd);
sdActivate(cardfd, cardRca);
oldCSD14=csd[14];
switch (action) {
case 0:
sdCSDSetCRC(csd);
printf("[+] Card CSD: ");
for(i=0; i<16; i++) {
printf("%02X",csd[i]);
}
printf(".\n");
exit(displayWpState(csd));
break;
case 1:
permlockWarning(csd);
csd[14] &=~0x30;
break;
case 2:
permlockWarning(csd);
csd[14] &=~0x30;
csd[14] |= 0x10;
break;
case 3:
csd[14] &=~0x30;
csd[14] |= 0x20;
break;
}
if(csd[14]!=oldCSD14) {
printf("[+] Writing CSD.\n");
sdCSDSetCRC(csd);
sdWriteCSD(cardfd, csd);
} else {
printf("[+] CSD unchanged.\n");
}
return displayWpState(csd);
}
73 changes: 73 additions & 0 deletions src/sd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2014-2017, Bertold Van den Bergh ([email protected])
* All rights reserved.
* This work has been developed to support research funded by
* "Fund for Scientific Research, Flanders" (F.W.O.-Vlaanderen).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdint.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

#include <linux/types.h>
#include <sys/ioctl.h>
#include <linux/mmc/ioctl.h>

#include "sdlinux.h"

#ifndef _SD_H
#define _SD_H

#define SD_GO_IDLE (0)
#define SD_SELECT (7)
#define SD_READ_CSD (9)
#define SD_WRITE_CSD (27)

//crc.c
extern uint8_t crc7AddWord(uint8_t crc, uint8_t word, uint8_t len);

//sdcmd.c
extern int sdOpenDevice(char* filename);
extern void sdExitDeviceError(int fd, const char* error);
extern int sdSendCommand(int cardfd, uint8_t cmd, uint32_t arg, uint8_t* data, uint16_t dataLen, uint8_t dataWr, uint32_t replyType, uint32_t* reply);
extern uint16_t sdFindRca(char* filename);
extern void sdActivate(int cardfd, uint16_t cardRca);
extern void sdStandbyAll(int cardfd);
extern void sdCSDSetCRC(uint8_t* csd);
extern void sdReadCSD(int cardfd, uint16_t cardRca, uint8_t* csd);
extern void sdWriteCSD(int cardfd, uint8_t* csd);
extern void sdGoIdle(int cardfs);

//main.c
extern int displayWpState(uint8_t* csd);
extern void printHelp();
extern void permlockWarning(uint8_t* csd);

#endif
Loading

0 comments on commit 79ef389

Please sign in to comment.