Created
April 10, 2017 13:49
-
-
Save mrtnmtth/19c10cc0dda43bbd3920b4fcf3c84172 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys, binascii | |
| def main(argv): | |
| cid = '' | |
| csd = '' | |
| if (len(argv) == 0): | |
| print 'Error: Need CID as an argument' | |
| return | |
| if (len(argv) >= 1): | |
| cid = argv[0] | |
| if (len(cid) != 32): | |
| print 'Error: CID has to be 32 hex digits' | |
| return | |
| decode_cid(cid) | |
| if (len(argv) >= 2): | |
| csd = argv[1] | |
| if (len(csd) != 32): | |
| print 'Error: CSD has to be 32 hex digits' | |
| return | |
| decode_csd(csd) | |
| def decode_cid(cid): | |
| mid = cid[0:2] | |
| oid = cid[2:6] | |
| pnm = cid[6:16] | |
| prv = cid[16:18] | |
| psn = cid[18:26] | |
| mdt = cid[27:30] | |
| crc = cid[30:32] | |
| #TODO: Check CRC | |
| print 'Manufacturer ID: ', '0x' + mid | |
| print 'OEM/Application ID: ', binascii.a2b_hex(oid) | |
| print 'Product Name: ', binascii.a2b_hex(pnm) | |
| print 'Product Revision: ', prv[0] + '.' + prv[1] | |
| print 'Product Serial Number:', '0x' + psn | |
| print 'Manufacturing Date: ', str(int(mdt[2:4], 16)) + '/' + str(int(mdt[0:2], 16) + 2000) | |
| def decode_csd(csd): | |
| csd = '{:0128b}'.format(int(csd, 16)) | |
| CSD_STRUCTURE = csd[0:2] | |
| csd_version = int(CSD_STRUCTURE, 2) | |
| if (csd_version == 0): | |
| #untested | |
| READ_BL_LEN = csd[44:48] | |
| C_SIZE = csd[54:66] | |
| C_SIZE_MULT = csd[78:81] | |
| memory_capacity = (int(C_SIZE, 2) + 1) * 2**(int(C_SIZE_MULT, 2) + (int(READ_BL_LEN, 2) + 2)) | |
| memory_capacity = memory_capacity / 1024 / 1024 #Capacity in MiBytes | |
| elif (csd_version == 1): | |
| C_SIZE = csd[58:80] | |
| memory_capacity = (int(C_SIZE, 2) + 1) / 2 #Capacity in MiBytes | |
| else: | |
| print 'Error: CSD version', csd_version, '. Unknown version.' | |
| return | |
| print 'Memory Capacity: ', memory_capacity, 'MiB' | |
| oids = { | |
| # Source: SD Insight Andriod App https://www.humanlogic.com/sdinsight/ | |
| 'AD': 'ADATA Technology Co., Ltd.', | |
| 'AT': 'ATP Electronics Inc.', | |
| 'BE': 'Barun Electronics', | |
| 'CZ': 'Cactus Technologies', | |
| 'DY': 'Dynacard Co., Ltd.', | |
| 'JE': 'Jiaelec Corp.', | |
| 'JT': 'Jiang Tay Technical Co., Ltd.', | |
| 'KG': 'Kingmax Semiconductor Inc.', | |
| 'NC': 'Netcom Technology (HK) Ltd.', | |
| 'NL': 'Netlist, Inc.', | |
| 'PA': 'Panasonic Corporation', | |
| 'PQ': 'Power Quotient Intl. Co., Ltd', | |
| 'PH': 'Phison Electronics Corp.', | |
| 'RK': 'Ritek Corporation', | |
| 'SB': 'SwissBit AG', | |
| 'SD': 'SanDisk Corporation', | |
| 'SM': 'Samsung Electronics Co., Ltd.', | |
| 'S`': 'STEC Inc. / SimpleTech', | |
| 'TF': 'Strontium Technologies Pte Ltd', | |
| 'TM': 'Toshiba Corporation' | |
| } | |
| if __name__ == "__main__": | |
| main(sys.argv[1:]) | |
| # Resources: | |
| # https://members.sdcard.org/downloads/pls/simplified_specs/part1_410.pdf | |
| # https://github.com/torvalds/linux/blob/master/drivers/mmc/core/sd.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment