lsmmc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010-2011
  3. * Author: Sebastian Rasmussen <sebastian.rasmussen@stericsson.com>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. * 3. Neither the name of the ST-Ericsson SA nor the names of its
  18. * contributors may be used to endorse or promote products
  19. * derived from this software without specific prior written
  20. * permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  27. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  31. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  33. * OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <assert.h>
  36. #include <ctype.h>
  37. #include <dirent.h>
  38. #include <errno.h>
  39. #include <limits.h>
  40. #include <stdarg.h>
  41. #include <stdbool.h>
  42. #include <stdint.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <unistd.h>
  47. #include "mmc.h"
  48. #define MASKTOBIT0(high) \
  49. ((high >= 0) ? ((1ull << ((high) + 1ull)) - 1ull) : 0ull)
  50. #define MASK(high, low) (MASKTOBIT0(high) & ~MASKTOBIT0(low - 1))
  51. #define BITS(value, high, low) (((value) & MASK((high), (low))) >> (low))
  52. #define IDS_MAX 256
  53. struct config {
  54. char *idsfile;
  55. char *dir;
  56. bool verbose;
  57. int interfaces;
  58. char **interface;
  59. char **mmc_ids;
  60. char **sd_ids;
  61. char *type;
  62. char *cid;
  63. char *csd;
  64. char *scr;
  65. char *ext_csd;
  66. };
  67. enum REG_TYPE {
  68. CID = 0,
  69. CSD,
  70. SCR,
  71. EXT_CSD,
  72. };
  73. struct ids_database {
  74. char *type;
  75. int id;
  76. char *manufacturer;
  77. };
  78. struct ids_database database[] = {
  79. {
  80. .type = "sd",
  81. .id = 0x01,
  82. .manufacturer = "Panasonic",
  83. },
  84. {
  85. .type = "sd",
  86. .id = 0x02,
  87. .manufacturer = "Toshiba/Kingston/Viking",
  88. },
  89. {
  90. .type = "sd",
  91. .id = 0x03,
  92. .manufacturer = "SanDisk",
  93. },
  94. {
  95. .type = "sd",
  96. .id = 0x08,
  97. .manufacturer = "Silicon Power",
  98. },
  99. {
  100. .type = "sd",
  101. .id = 0x18,
  102. .manufacturer = "Infineon",
  103. },
  104. {
  105. .type = "sd",
  106. .id = 0x1b,
  107. .manufacturer = "Transcend",
  108. },
  109. {
  110. .type = "sd",
  111. .id = 0x1c,
  112. .manufacturer = "Transcend",
  113. },
  114. {
  115. .type = "sd",
  116. .id = 0x1d,
  117. .manufacturer = "Corsair",
  118. },
  119. {
  120. .type = "sd",
  121. .id = 0x1e,
  122. .manufacturer = "Transcend",
  123. },
  124. {
  125. .type = "sd",
  126. .id = 0x1f,
  127. .manufacturer = "Kingston",
  128. },
  129. {
  130. .type = "sd",
  131. .id = 0x28,
  132. .manufacturer = "Lexar",
  133. },
  134. {
  135. .type = "sd",
  136. .id = 0x30,
  137. .manufacturer = "SanDisk",
  138. },
  139. {
  140. .type = "sd",
  141. .id = 0x33,
  142. .manufacturer = "STMicroelectronics",
  143. },
  144. {
  145. .type = "sd",
  146. .id = 0x41,
  147. .manufacturer = "Kingston",
  148. },
  149. {
  150. .type = "sd",
  151. .id = 0x6f,
  152. .manufacturer = "STMicroelectronics",
  153. },
  154. {
  155. .type = "sd",
  156. .id = 0x89,
  157. .manufacturer = "Unknown",
  158. },
  159. {
  160. .type = "mmc",
  161. .id = 0x00,
  162. .manufacturer = "SanDisk",
  163. },
  164. {
  165. .type = "mmc",
  166. .id = 0x02,
  167. .manufacturer = "Kingston/SanDisk",
  168. },
  169. {
  170. .type = "mmc",
  171. .id = 0x03,
  172. .manufacturer = "Toshiba",
  173. },
  174. {
  175. .type = "mmc",
  176. .id = 0x05,
  177. .manufacturer = "Unknown",
  178. },
  179. {
  180. .type = "mmc",
  181. .id = 0x06,
  182. .manufacturer = "Unknown",
  183. },
  184. {
  185. .type = "mmc",
  186. .id = 0x11,
  187. .manufacturer = "Toshiba",
  188. },
  189. {
  190. .type = "mmc",
  191. .id = 0x13,
  192. .manufacturer = "Micron",
  193. },
  194. {
  195. .type = "mmc",
  196. .id = 0x15,
  197. .manufacturer = "Samsung/SanDisk/LG",
  198. },
  199. {
  200. .type = "mmc",
  201. .id = 0x37,
  202. .manufacturer = "KingMax",
  203. },
  204. {
  205. .type = "mmc",
  206. .id = 0x44,
  207. .manufacturer = "SanDisk",
  208. },
  209. {
  210. .type = "mmc",
  211. .id = 0x2c,
  212. .manufacturer = "Kingston",
  213. },
  214. {
  215. .type = "mmc",
  216. .id = 0x70,
  217. .manufacturer = "Kingston",
  218. },
  219. };
  220. /* Command line parsing functions */
  221. void usage(void)
  222. {
  223. printf("Usage: print mmc [-h] [-v] <device path ...>\n");
  224. printf("\n");
  225. printf("Options:\n");
  226. printf("\t-h\tShow this help.\n");
  227. printf("\t-v\tEnable verbose mode.\n");
  228. }
  229. int parse_opts(int argc, char **argv, struct config *config)
  230. {
  231. int c;
  232. while ((c = getopt(argc, argv, "hv")) != -1) {
  233. switch (c) {
  234. case 'h':
  235. usage();
  236. return -1;
  237. case 'v':
  238. config->verbose = true;
  239. break;
  240. case '?':
  241. fprintf(stderr,
  242. "Unknown option '%c' encountered.\n\n", c);
  243. usage();
  244. return -1;
  245. case ':':
  246. fprintf(stderr,
  247. "Argument for option '%c' missing.\n\n", c);
  248. usage();
  249. return -1;
  250. default:
  251. fprintf(stderr,
  252. "Unimplemented option '%c' encountered.\n", c);
  253. break;
  254. }
  255. }
  256. if (optind >= argc) {
  257. fprintf(stderr, "Expected mmc directory arguments.\n\n");
  258. usage();
  259. return -1;
  260. }
  261. config->dir = strdup(argv[optind]);
  262. return 0;
  263. }
  264. int parse_ids(struct config *config)
  265. {
  266. unsigned int ids_cnt = sizeof(database) / sizeof(struct ids_database);
  267. unsigned int value;
  268. char **ids;
  269. char *type;
  270. int i;
  271. for (i = 0; i < ids_cnt; i++) {
  272. type = database[i].type;
  273. if (!strcmp(type, "mmc")) {
  274. ids = config->mmc_ids;
  275. } else if (!strcmp(type, "sd")) {
  276. ids = config->sd_ids;
  277. } else {
  278. fprintf(stderr,
  279. "MMC/SD id parse error, unknown type: '%s'.\n",
  280. type);
  281. return -1;
  282. }
  283. value = database[i].id;
  284. if (value >= IDS_MAX) {
  285. fprintf(stderr,
  286. "MMC/SD id parse error, id out of range.\n");
  287. return -1;
  288. }
  289. if (ids[value]) {
  290. fprintf(stderr,
  291. "Duplicate entries: type='%s', id='0x%1x'.\n",
  292. type, value);
  293. return -1;
  294. }
  295. ids[value] = database[i].manufacturer;
  296. }
  297. return 0;
  298. }
  299. /* MMC/SD file parsing functions */
  300. char *read_file(char *name)
  301. {
  302. char line[4096];
  303. char *preparsed, *start = line;
  304. int len;
  305. FILE *f;
  306. f = fopen(name, "r");
  307. if (!f) {
  308. fprintf(stderr, "Could not open MMC/SD file '%s'.\n", name);
  309. return NULL;
  310. }
  311. preparsed = fgets(line, sizeof(line), f);
  312. if (!preparsed) {
  313. if (ferror(f))
  314. fprintf(stderr, "Could not read MMC/SD file '%s'.\n",
  315. name);
  316. else
  317. fprintf(stderr,
  318. "Could not read data from MMC/SD file '%s'.\n",
  319. name);
  320. if (fclose(f))
  321. fprintf(stderr, "Could not close MMC/SD file '%s'.\n",
  322. name);
  323. return NULL;
  324. }
  325. if (fclose(f)) {
  326. fprintf(stderr, "Could not close MMC/SD file '%s'.\n", name);
  327. return NULL;
  328. }
  329. line[sizeof(line) - 1] = '\0';
  330. len = strlen(line);
  331. while (len > 0 && isspace(line[len - 1]))
  332. len--;
  333. while (len > 0 && isspace(*start)) {
  334. start++;
  335. len--;
  336. }
  337. memmove(line, start, len);
  338. line[len] = '\0';
  339. return strdup(line);
  340. }
  341. /* Hexadecimal string parsing functions */
  342. char *to_binstr(char *hexstr)
  343. {
  344. char *bindigits[] = {
  345. "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
  346. "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111",
  347. };
  348. char *binstr, *tail;
  349. binstr = calloc(strlen(hexstr) * 4 + 1, sizeof(char));
  350. if (!binstr)
  351. return NULL;
  352. tail = binstr;
  353. while (hexstr && *hexstr != '\0') {
  354. if (!isxdigit(*hexstr)) {
  355. free(binstr);
  356. return NULL;
  357. }
  358. if (isdigit(*hexstr))
  359. strcat(tail, bindigits[*hexstr - '0']);
  360. else if (islower(*hexstr))
  361. strcat(tail, bindigits[*hexstr - 'a' + 10]);
  362. else
  363. strcat(tail, bindigits[*hexstr - 'A' + 10]);
  364. hexstr++;
  365. tail += 4;
  366. }
  367. return binstr;
  368. }
  369. void bin_to_unsigned(unsigned int *u, char *binstr, int width)
  370. {
  371. *u = 0;
  372. assert(width <= 32);
  373. while (binstr && *binstr != '\0' && width > 0) {
  374. *u <<= 1;
  375. *u |= *binstr == '0' ? 0 : 1;
  376. binstr++;
  377. width--;
  378. }
  379. }
  380. void bin_to_ascii(char *a, char *binstr, int width)
  381. {
  382. assert(width % 8 == 0);
  383. *a = '\0';
  384. while (binstr && *binstr != '\0' && width > 0) {
  385. unsigned int u;
  386. char c[2] = { '\0', '\0' };
  387. char *s = &c[0];
  388. bin_to_unsigned(&u, binstr, 8);
  389. c[0] = u;
  390. strcat(a, s);
  391. binstr += 8;
  392. width -= 8;
  393. }
  394. }
  395. void parse_bin(char *hexstr, char *fmt, ...)
  396. {
  397. va_list args;
  398. char *origstr;
  399. char *binstr;
  400. unsigned long width = 0;
  401. binstr = to_binstr(hexstr);
  402. origstr = binstr;
  403. va_start(args, fmt);
  404. while (binstr && fmt && *fmt != '\0') {
  405. if (isdigit(*fmt)) {
  406. char *rest;
  407. errno = 0;
  408. width = strtoul(fmt, &rest, 10);
  409. if (width == ULONG_MAX && errno != 0)
  410. fprintf(stderr, "strtoul()");
  411. fmt = rest;
  412. } else if (*fmt == 'u') {
  413. unsigned int *u = va_arg(args, unsigned int *);
  414. if (u)
  415. bin_to_unsigned(u, binstr, width);
  416. binstr += width;
  417. width = 0;
  418. fmt++;
  419. } else if (*fmt == 'r') {
  420. binstr += width;
  421. width = 0;
  422. fmt++;
  423. } else if (*fmt == 'a') {
  424. char *c = va_arg(args, char *);
  425. if (c)
  426. bin_to_ascii(c, binstr, width);
  427. binstr += width;
  428. width = 0;
  429. fmt++;
  430. } else {
  431. fmt++;
  432. }
  433. }
  434. va_end(args);
  435. free(origstr);
  436. }
  437. /* MMC/SD information parsing functions */
  438. void print_sd_cid(struct config *config, char *cid)
  439. {
  440. static const char *months[] = {
  441. "jan", "feb", "mar", "apr", "may", "jun",
  442. "jul", "aug", "sep", "oct", "nov", "dec",
  443. "invalid0", "invalid1", "invalid2", "invalid3",
  444. };
  445. unsigned int mid;
  446. char oid[3];
  447. char pnm[6];
  448. unsigned int prv_major;
  449. unsigned int prv_minor;
  450. unsigned int psn;
  451. unsigned int mdt_month;
  452. unsigned int mdt_year;
  453. unsigned int crc;
  454. parse_bin(cid, "8u16a40a4u4u32u4r8u4u7u1r",
  455. &mid, &oid[0], &pnm[0], &prv_major, &prv_minor, &psn,
  456. &mdt_year, &mdt_month, &crc);
  457. oid[2] = '\0';
  458. pnm[5] = '\0';
  459. if (config->verbose) {
  460. printf("======SD/CID======\n");
  461. printf("\tMID: 0x%02x (", mid);
  462. if (config->sd_ids[mid])
  463. printf("%s)\n", config->sd_ids[mid]);
  464. else
  465. printf("Unlisted)\n");
  466. printf("\tOID: %s\n", oid);
  467. printf("\tPNM: %s\n", pnm);
  468. printf("\tPRV: 0x%01x%01x ", prv_major, prv_minor);
  469. printf("(%d.%d)\n", prv_major, prv_minor);
  470. printf("\tPSN: 0x%08x\n", psn);
  471. printf("\tMDT: 0x%02x%01x %d %s\n", mdt_year, mdt_month,
  472. 2000 + mdt_year, months[mdt_month]);
  473. printf("\tCRC: 0x%02x\n", crc);
  474. } else {
  475. if (config->sd_ids[mid])
  476. printf("manufacturer: '%s' '%s'\n",
  477. config->sd_ids[mid], oid);
  478. else
  479. printf("manufacturer: 'Unlisted' '%s'\n", oid);
  480. printf("product: '%s' %d.%d\n", pnm, prv_major, prv_minor);
  481. printf("serial: 0x%08x\n", psn);
  482. printf("manfacturing date: %d %s\n", 2000 + mdt_year,
  483. months[mdt_month]);
  484. }
  485. }
  486. void print_mmc_cid(struct config *config, char *cid)
  487. {
  488. static const char *months[] = {
  489. "jan", "feb", "mar", "apr", "may", "jun",
  490. "jul", "aug", "sep", "oct", "nov", "dec",
  491. "invalid0", "invalid1", "invalid2", "invalid3",
  492. };
  493. unsigned int mid;
  494. unsigned int cbx;
  495. unsigned int oid;
  496. char pnm[7];
  497. unsigned int prv_major;
  498. unsigned int prv_minor;
  499. unsigned int psn;
  500. unsigned int mdt_month;
  501. unsigned int mdt_year;
  502. unsigned int crc;
  503. parse_bin(cid, "8u6r2u8u48a4u4u32u4u4u7u1r",
  504. &mid, &cbx, &oid, &pnm[0], &psn, &prv_major, &prv_minor,
  505. &mdt_year, &mdt_month, &crc);
  506. pnm[6] = '\0';
  507. if (config->verbose) {
  508. printf("======MMC/CID======\n");
  509. printf("\tMID: 0x%02x (", mid);
  510. if (config->mmc_ids[mid])
  511. printf("%s)\n", config->mmc_ids[mid]);
  512. else
  513. printf("Unlisted)\n");
  514. printf("\tCBX: 0x%01x (", cbx);
  515. switch (cbx) {
  516. case 0:
  517. printf("card)\n");
  518. break;
  519. case 1:
  520. printf("BGA)\n");
  521. break;
  522. case 2:
  523. printf("PoP)\n");
  524. break;
  525. case 3:
  526. printf("reserved)\n");
  527. break;
  528. }
  529. printf("\tOID: 0x%01x\n", oid);
  530. printf("\tPNM: %s\n", pnm);
  531. printf("\tPRV: 0x%01x%01x ", prv_major, prv_minor);
  532. printf("(%d.%d)\n", prv_major, prv_minor);
  533. printf("\tPSN: 0x%08x\n", psn);
  534. printf("\tMDT: 0x%01x%01x %d %s\n", mdt_month, mdt_year,
  535. 1997 + mdt_year, months[mdt_month]);
  536. printf("\tCRC: 0x%02x\n", crc);
  537. } else {
  538. if (config->mmc_ids[mid])
  539. printf("manufacturer: '%s' '%c'\n",
  540. config->mmc_ids[mid], oid);
  541. else
  542. printf("manufacturer: 'Unlisted' '%c'\n", oid);
  543. printf("product: '%s' %d.%d\n", pnm, prv_major, prv_minor);
  544. printf("serial: 0x%08x\n", psn);
  545. printf("manfacturing date: %d %s\n", 1997 + mdt_year,
  546. months[mdt_month]);
  547. }
  548. }
  549. void print_sd_csd(struct config *config, char *csd)
  550. {
  551. unsigned int csd_structure;
  552. unsigned int taac_timevalue;
  553. unsigned int taac_timeunit;
  554. unsigned int nsac;
  555. unsigned int tran_speed_timevalue;
  556. unsigned int tran_speed_transferrateunit;
  557. unsigned int ccc;
  558. unsigned int read_bl_len;
  559. unsigned int read_bl_partial;
  560. unsigned int write_blk_misalign;
  561. unsigned int read_blk_misalign;
  562. unsigned int dsr_imp;
  563. unsigned int c_size;
  564. unsigned int vdd_r_curr_min;
  565. unsigned int vdd_r_curr_max;
  566. unsigned int vdd_w_curr_min;
  567. unsigned int vdd_w_curr_max;
  568. unsigned int c_size_mult;
  569. unsigned int erase_blk_en;
  570. unsigned int sector_size;
  571. unsigned int wp_grp_size;
  572. unsigned int wp_grp_enable;
  573. unsigned int r2w_factor;
  574. unsigned int write_bl_len;
  575. unsigned int write_bl_partial;
  576. unsigned int file_format_grp;
  577. unsigned int copy;
  578. unsigned int perm_write_protect;
  579. unsigned int tmp_write_protect;
  580. unsigned int file_format;
  581. unsigned int crc;
  582. unsigned int taac;
  583. unsigned int tran_speed;
  584. parse_bin(csd, "2u", &csd_structure);
  585. if (csd_structure == 0) {
  586. parse_bin(csd, "2u6r1r4u3u8u1r4u3u12u4u1u1u1u1u2r12u3u3u3u3u3u"
  587. "1u7u7u1u2r3u4u1u5r1u1u1u1u2u2r7u1r",
  588. NULL, &taac_timevalue, &taac_timeunit, &nsac,
  589. &tran_speed_timevalue,
  590. &tran_speed_transferrateunit, &ccc,
  591. &read_bl_len, &read_bl_partial,
  592. &write_blk_misalign, &read_blk_misalign,
  593. &dsr_imp, &c_size, &vdd_r_curr_min,
  594. &vdd_r_curr_max, &vdd_w_curr_min,
  595. &vdd_w_curr_max, &c_size_mult, &erase_blk_en,
  596. &sector_size, &wp_grp_size, &wp_grp_enable,
  597. &r2w_factor, &write_bl_len, &write_bl_partial,
  598. &file_format_grp, &copy, &perm_write_protect,
  599. &tmp_write_protect, &file_format, &crc);
  600. } else if (csd_structure == 1) {
  601. parse_bin(csd, "2u6r1r4u3u8u1r4u3u12u4u1u1u1u1u6r22u1r1u7u7u1u"
  602. "2r3u4u1u5r1u1u1u1u2u2r7u1r",
  603. NULL, &taac_timevalue, &taac_timeunit, &nsac,
  604. &tran_speed_timevalue,
  605. &tran_speed_transferrateunit, &ccc,
  606. &read_bl_len, &read_bl_partial,
  607. &write_blk_misalign, &read_blk_misalign,
  608. &dsr_imp, &c_size, &erase_blk_en, &sector_size,
  609. &wp_grp_size, &wp_grp_enable, &r2w_factor,
  610. &write_bl_len, &write_bl_partial,
  611. &file_format_grp, &copy, &perm_write_protect,
  612. &tmp_write_protect, &file_format, &crc);
  613. vdd_r_curr_min = 0;
  614. c_size_mult = 0;
  615. } else {
  616. printf("Unknown CSD structure: 0x%1x\n", csd_structure);
  617. return;
  618. }
  619. taac = taac_timevalue << 3 | taac_timeunit;
  620. tran_speed = tran_speed_timevalue << 3 | tran_speed_transferrateunit;
  621. if (config->verbose) {
  622. float value;
  623. unsigned long long blocks = 0;
  624. int block_size = 0;
  625. unsigned long long memory_capacity;
  626. printf("======SD/CSD======\n");
  627. printf("\tCSD_STRUCTURE: %d\n", csd_structure);
  628. printf("\tTAAC: 0x%02x (", taac);
  629. switch (taac_timevalue) {
  630. case 0x0:
  631. value = 0.0f;
  632. break;
  633. case 0x1:
  634. value = 1.0f;
  635. break;
  636. case 0x2:
  637. value = 1.2f;
  638. break;
  639. case 0x3:
  640. value = 1.3f;
  641. break;
  642. case 0x4:
  643. value = 1.5f;
  644. break;
  645. case 0x5:
  646. value = 2.0f;
  647. break;
  648. case 0x6:
  649. value = 2.5f;
  650. break;
  651. case 0x7:
  652. value = 3.0f;
  653. break;
  654. case 0x8:
  655. value = 3.5f;
  656. break;
  657. case 0x9:
  658. value = 4.0f;
  659. break;
  660. case 0xa:
  661. value = 4.5f;
  662. break;
  663. case 0xb:
  664. value = 5.0f;
  665. break;
  666. case 0xc:
  667. value = 5.5f;
  668. break;
  669. case 0xd:
  670. value = 6.0f;
  671. break;
  672. case 0xe:
  673. value = 7.0f;
  674. break;
  675. case 0xf:
  676. value = 8.0f;
  677. break;
  678. default:
  679. value = 0.0f;
  680. break;
  681. }
  682. switch (taac_timeunit) {
  683. case 0x0:
  684. printf("%.2fns)\n", value * 1.0f);
  685. break;
  686. case 0x1:
  687. printf("%.2fns)\n", value * 10.0f);
  688. break;
  689. case 0x2:
  690. printf("%.2fns)\n", value * 100.0f);
  691. break;
  692. case 0x3:
  693. printf("%.2fus)\n", value * 1.0f);
  694. break;
  695. case 0x4:
  696. printf("%.2fus)\n", value * 10.0f);
  697. break;
  698. case 0x5:
  699. printf("%.2fus)\n", value * 100.0f);
  700. break;
  701. case 0x6:
  702. printf("%.2fms)\n", value * 1.0f);
  703. break;
  704. case 0x7:
  705. printf("%.2fms)\n", value * 10.0f);
  706. break;
  707. }
  708. if (csd_structure == 1 && taac != 0x0e)
  709. printf("Warn: Invalid TAAC (should be 0x0e)\n");
  710. printf("\tNSAC: %d clocks\n", nsac);
  711. if (csd_structure == 1 && nsac != 0x00)
  712. printf("Warn: Invalid NSAC (should be 0x00)\n");
  713. printf("\tTRAN_SPEED: 0x%02x (", tran_speed);
  714. switch (tran_speed_timevalue) {
  715. case 0x0:
  716. value = 0.0f;
  717. break;
  718. case 0x1:
  719. value = 1.0f;
  720. break;
  721. case 0x2:
  722. value = 1.2f;
  723. break;
  724. case 0x3:
  725. value = 1.3f;
  726. break;
  727. case 0x4:
  728. value = 1.5f;
  729. break;
  730. case 0x5:
  731. value = 2.0f;
  732. break;
  733. case 0x6:
  734. value = 2.5f;
  735. break;
  736. case 0x7:
  737. value = 3.0f;
  738. break;
  739. case 0x8:
  740. value = 3.5f;
  741. break;
  742. case 0x9:
  743. value = 4.0f;
  744. break;
  745. case 0xa:
  746. value = 4.5f;
  747. break;
  748. case 0xb:
  749. value = 5.0f;
  750. break;
  751. case 0xc:
  752. value = 5.5f;
  753. break;
  754. case 0xd:
  755. value = 6.0f;
  756. break;
  757. case 0xe:
  758. value = 7.0f;
  759. break;
  760. case 0xf:
  761. value = 8.0f;
  762. break;
  763. default:
  764. value = 0.0f;
  765. break;
  766. }
  767. switch (tran_speed_transferrateunit) {
  768. case 0x0:
  769. printf("%.2fkbit/s)\n", value * 100.0f);
  770. break;
  771. case 0x1:
  772. printf("%.2fMbit/s)\n", value * 1.0f);
  773. break;
  774. case 0x2:
  775. printf("%.2fMbit/s)\n", value * 10.0f);
  776. break;
  777. case 0x3:
  778. printf("%.2fMbit/s)\n", value * 100.0f);
  779. break;
  780. default:
  781. printf("reserved)\n");
  782. break;
  783. }
  784. if (csd_structure == 0 &&
  785. (tran_speed != 0x32 && tran_speed != 0x5a))
  786. printf("Warn: Invalid TRAN_SPEED "
  787. "(should be 0x32 or 0x5a)\n");
  788. if (csd_structure == 1 && tran_speed != 0x32 &&
  789. tran_speed != 0x5a && tran_speed != 0x0b &&
  790. tran_speed != 0x2b)
  791. printf("Warn: Invalid TRAN_SPEED "
  792. "(should be 0x32, 0x5a, 0x0b or 0x2b\n");
  793. printf("\tCCC: 0x%03x (class: ", ccc);
  794. if (ccc & 0x800)
  795. printf("11, ");
  796. if (ccc & 0x400)
  797. printf("10, ");
  798. if (ccc & 0x200)
  799. printf("9, ");
  800. if (ccc & 0x100)
  801. printf("8, ");
  802. if (ccc & 0x080)
  803. printf("7, ");
  804. if (ccc & 0x040)
  805. printf("6, ");
  806. if (ccc & 0x020)
  807. printf("5, ");
  808. if (ccc & 0x010)
  809. printf("4, ");
  810. if (ccc & 0x008)
  811. printf("3, ");
  812. if (ccc & 0x004)
  813. printf("2, ");
  814. if (ccc & 0x002)
  815. printf("1, ");
  816. if (ccc & 0x001)
  817. printf("0, ");
  818. printf(" )\n");
  819. if (csd_structure == 0 &&
  820. (ccc != 0x5b5 && ccc != 0x7b5 && ccc != 0x5f5))
  821. printf("Warn: Invalid CCC (should be 0x5b5, "
  822. "0x7b5 or 0x5f5)\n");
  823. else if (csd_structure == 1 && ccc != 0x5b5 && ccc != 0x7b5)
  824. printf("Warn: Invalid CCC (should be 0x5b5 or 0x7b5)\n");
  825. printf("\tREAD_BL_LEN: 0x%01x (", read_bl_len);
  826. switch (read_bl_len) {
  827. case 0x9:
  828. printf("512 bytes)\n");
  829. break;
  830. case 0xa:
  831. printf("1024 bytes)\n");
  832. break;
  833. case 0xb:
  834. printf("2048 bytes)\n");
  835. break;
  836. default:
  837. printf("reserved bytes)\n");
  838. break;
  839. }
  840. if (csd_structure == 1 && read_bl_len != 0x9)
  841. printf("Warn: Invalid READ_BL_LEN (should be 0x9)\n");
  842. printf("\tREAD_BL_PARTIAL: 0x%01x\n", read_bl_partial);
  843. if (csd_structure == 0 && read_bl_partial != 0x01)
  844. printf("Warn: Invalid READ_BL_PARTIAL (should be 0x01)\n");
  845. else if (csd_structure == 1 && read_bl_partial != 0x00)
  846. printf("Warn: Invalid READ_BL_PARTIAL (should be 0x00)\n");
  847. printf("\tWRITE_BLK_MISALIGN: 0x%01x\n", write_blk_misalign);
  848. if (csd_structure == 1 && write_blk_misalign != 0x00)
  849. printf("Warn: Invalid WRITE_BLK_MISALIGN (should be 0x00)\n");
  850. printf("\tREAD_BLK_MISALIGN: 0x%01x\n", read_blk_misalign);
  851. if (csd_structure == 1 && read_blk_misalign != 0x00)
  852. printf("Warn: Invalid READ_BLK_MISALIGN (should be 0x00)\n");
  853. printf("\tDSR_IMP: 0x%01x\n", dsr_imp);
  854. if (csd_structure == 0) {
  855. int mult;
  856. int blocknr;
  857. int block_len;
  858. printf("\tC_SIZE: 0x%03x\n", c_size);
  859. printf("\tVDD_R_CURR_MIN: 0x%01x (", vdd_r_curr_min);
  860. switch (vdd_r_curr_min) {
  861. case 0x0:
  862. printf("0.5mA)\n");
  863. break;
  864. case 0x1:
  865. printf("1mA)\n");
  866. break;
  867. case 0x2:
  868. printf("5mA)\n");
  869. break;
  870. case 0x3:
  871. printf("10mA)\n");
  872. break;
  873. case 0x4:
  874. printf("25mA)\n");
  875. break;
  876. case 0x5:
  877. printf("35mA)\n");
  878. break;
  879. case 0x6:
  880. printf("60mA)\n");
  881. break;
  882. case 0x7:
  883. printf("100mA)\n");
  884. break;
  885. }
  886. printf("\tVDD_R_CURR_MAX: 0x%01x (", vdd_r_curr_max);
  887. switch (vdd_r_curr_max) {
  888. case 0x0:
  889. printf("1mA)\n");
  890. break;
  891. case 0x1:
  892. printf("5mA)\n");
  893. break;
  894. case 0x2:
  895. printf("10mA)\n");
  896. break;
  897. case 0x3:
  898. printf("25mA)\n");
  899. break;
  900. case 0x4:
  901. printf("35mA)\n");
  902. break;
  903. case 0x5:
  904. printf("45mA)\n");
  905. break;
  906. case 0x6:
  907. printf("80mA)\n");
  908. break;
  909. case 0x7:
  910. printf("200mA)\n");
  911. break;
  912. }
  913. printf("\tVDD_W_CURR_MIN: 0x%01x (", vdd_w_curr_min);
  914. switch (vdd_w_curr_min) {
  915. case 0x0:
  916. printf("0.5mA)\n");
  917. break;
  918. case 0x1:
  919. printf("1mA)\n");
  920. break;
  921. case 0x2:
  922. printf("5mA)\n");
  923. break;
  924. case 0x3:
  925. printf("10mA)\n");
  926. break;
  927. case 0x4:
  928. printf("25mA)\n");
  929. break;
  930. case 0x5:
  931. printf("35mA)\n");
  932. break;
  933. case 0x6:
  934. printf("60mA)\n");
  935. break;
  936. case 0x7:
  937. printf("100mA)\n");
  938. break;
  939. }
  940. printf("\tVDD_W_CURR_MAX: 0x%01x (", vdd_w_curr_max);
  941. switch (vdd_w_curr_max) {
  942. case 0x0:
  943. printf("1mA)\n");
  944. break;
  945. case 0x1:
  946. printf("5mA)\n");
  947. break;
  948. case 0x2:
  949. printf("10mA)\n");
  950. break;
  951. case 0x3:
  952. printf("25mA)\n");
  953. break;
  954. case 0x4:
  955. printf("35mA)\n");
  956. break;
  957. case 0x5:
  958. printf("45mA)\n");
  959. break;
  960. case 0x6:
  961. printf("80mA)\n");
  962. break;
  963. case 0x7:
  964. printf("200mA)\n");
  965. break;
  966. }
  967. printf("\tC_SIZE_MULT: 0x%01x\n", c_size_mult);
  968. mult = 1 << (c_size_mult + 2);
  969. blocknr = (c_size + 1) * mult;
  970. block_len = 1 << read_bl_len;
  971. blocks = blocknr;
  972. block_size = block_len;
  973. } else if (csd_structure == 1) {
  974. printf("\tC_SIZE: 0x%06x\n", c_size);
  975. printf("\tERASE_BLK_EN: 0x%01x\n", erase_blk_en);
  976. if (erase_blk_en != 0x01)
  977. printf("Warn: Invalid ERASE_BLK_EN (should be 0x01)\n");
  978. printf("\tSECTOR_SIZE: 0x%02x (Erasable sector: %d blocks)\n",
  979. sector_size, sector_size + 1);
  980. if (sector_size != 0x7f)
  981. printf("Warn: Invalid SECTOR_SIZE (should be 0x7f)\n");
  982. printf("\tWP_GRP_SIZE: 0x%02x (Write protect group: %d blocks)\n",
  983. wp_grp_size, wp_grp_size + 1);
  984. if (wp_grp_size != 0x00)
  985. printf("Warn: Invalid WP_GRP_SIZE (should be 0x00)\n");
  986. printf("\tWP_GRP_ENABLE: 0x%01x\n", wp_grp_enable);
  987. if (wp_grp_enable != 0x00)
  988. printf("Warn: Invalid WP_GRP_ENABLE (should be 0x00)\n");
  989. printf("\tR2W_FACTOR: 0x%01x (Write %d times read)\n",
  990. r2w_factor, r2w_factor);
  991. if (r2w_factor != 0x02)
  992. printf("Warn: Invalid R2W_FACTOR (should be 0x02)\n");
  993. printf("\tWRITE_BL_LEN: 0x%01x (", write_bl_len);
  994. switch (write_bl_len) {
  995. case 9:
  996. printf("512 bytes)\n");
  997. break;
  998. case 10:
  999. printf("1024 bytes)\n");
  1000. break;
  1001. case 11:
  1002. printf("2048 bytes)\n");
  1003. break;
  1004. default:
  1005. printf("reserved)\n");
  1006. break;
  1007. }
  1008. if (write_bl_len != 0x09)
  1009. printf("Warn: Invalid WRITE_BL_LEN (should be 0x09)\n");
  1010. printf("\tWRITE_BL_PARTIAL: 0x%01x\n", write_bl_partial);
  1011. if (write_bl_partial != 0x00)
  1012. printf("Warn: Invalid WRITE_BL_PARTIAL (should be 0x00)\n");
  1013. printf("\tFILE_FORMAT_GRP: 0x%01x\n", file_format_grp);
  1014. if (file_format_grp != 0x00)
  1015. printf("Warn: Invalid FILE_FORMAT_GRP (should be 0x00)\n");
  1016. printf("\tCOPY: 0x%01x\n", copy);
  1017. printf("\tPERM_WRITE_PROTECT: 0x%01x\n",
  1018. perm_write_protect);
  1019. printf("\tTMP_WRITE_PROTECT: 0x%01x\n",
  1020. tmp_write_protect);
  1021. printf("\tFILE_FORMAT: 0x%01x (",
  1022. file_format);
  1023. if (file_format_grp == 1) {
  1024. printf("reserved)\n");
  1025. } else {
  1026. switch (file_format) {
  1027. case 0:
  1028. printf("partition table)\n");
  1029. break;
  1030. case 1:
  1031. printf("no partition table)\n");
  1032. break;
  1033. case 2:
  1034. printf("Universal File Format)\n");
  1035. break;
  1036. case 3:
  1037. printf("Others/unknown)\n");
  1038. break;
  1039. }
  1040. }
  1041. if (file_format != 0x00)
  1042. printf("Warn: Invalid FILE_FORMAT (should be 0x00)\n");
  1043. printf("\tCRC: 0x%01x\n", crc);
  1044. memory_capacity = (c_size + 1) * 512ull * 1024ull;
  1045. block_size = 512;
  1046. blocks = memory_capacity / block_size;
  1047. }
  1048. memory_capacity = blocks * block_size;
  1049. printf("\tCAPACITY: ");
  1050. if (memory_capacity / (1024ull * 1024ull * 1024ull) > 0)
  1051. printf("%.2fGbyte",
  1052. memory_capacity / (1024.0 * 1024.0 * 1024.0));
  1053. else if (memory_capacity / (1024ull * 1024ull) > 0)
  1054. printf("%.2fMbyte", memory_capacity / (1024.0 * 1024.0));
  1055. else if (memory_capacity / (1024ull) > 0)
  1056. printf("%.2fKbyte", memory_capacity / (1024.0));
  1057. else
  1058. printf("%.2fbyte", memory_capacity * 1.0);
  1059. printf(" (%lld bytes, %lld sectors, %d bytes each)\n",
  1060. memory_capacity, blocks, block_size);
  1061. } else {
  1062. unsigned long long blocks = 0;
  1063. int block_size = 0;
  1064. unsigned long long memory_capacity;
  1065. printf("card classes: ");
  1066. if (ccc & 0x800)
  1067. printf("11 extension, ");
  1068. if (ccc & 0x400)
  1069. printf("10 switch, ");
  1070. if (ccc & 0x200)
  1071. printf("9 I/O mode, ");
  1072. if (ccc & 0x100)
  1073. printf("8 application specific, ");
  1074. if (ccc & 0x080)
  1075. printf("7 lock card, ");
  1076. if (ccc & 0x040)
  1077. printf("6 write protection, ");
  1078. if (ccc & 0x020)
  1079. printf("5 erase, ");
  1080. if (ccc & 0x010)
  1081. printf("4 block write, ");
  1082. if (ccc & 0x008)
  1083. printf("3 reserved, ");
  1084. if (ccc & 0x004)
  1085. printf("2 block read, ");
  1086. if (ccc & 0x002)
  1087. printf("1 reserved, ");
  1088. if (ccc & 0x001)
  1089. printf("0 basic, ");
  1090. printf("\b\b\n");
  1091. if (csd_structure == 0) {
  1092. int mult;
  1093. int blocknr;
  1094. int block_len;
  1095. mult = 1 << (c_size_mult + 2);
  1096. blocknr = (c_size + 1) * mult;
  1097. block_len = 1 << read_bl_len;
  1098. blocks = blocknr;
  1099. block_size = block_len;
  1100. } else if (csd_structure == 1) {
  1101. memory_capacity = (c_size + 1) * 512ull * 1024ull;
  1102. block_size = 512;
  1103. blocks = memory_capacity / block_size;
  1104. }
  1105. memory_capacity = blocks * block_size;
  1106. printf("capacity: ");
  1107. if (memory_capacity / (1024ull * 1024ull * 1024ull) > 0)
  1108. printf("%.2fGbyte",
  1109. memory_capacity / (1024.0 * 1024.0 * 1024.0));
  1110. else if (memory_capacity / (1024ull * 1024ull) > 0)
  1111. printf("%.2fMbyte", memory_capacity / (1024.0 * 1024.0));
  1112. else if (memory_capacity / (1024ull) > 0)
  1113. printf("%.2fKbyte", memory_capacity / (1024.0));
  1114. else
  1115. printf("%.2fbyte", memory_capacity * 1.0);
  1116. printf(" (%lld bytes, %lld sectors, %d bytes each)\n",
  1117. memory_capacity, blocks, block_size);
  1118. }
  1119. }
  1120. void print_mmc_csd(struct config *config, char *csd)
  1121. {
  1122. unsigned int csd_structure;
  1123. unsigned int spec_vers;
  1124. unsigned int taac_timevalue;
  1125. unsigned int taac_timeunit;
  1126. unsigned int nsac;
  1127. unsigned int tran_speed_timevalue;
  1128. unsigned int tran_speed_transferrateunit;
  1129. unsigned int ccc;
  1130. unsigned int read_bl_len;
  1131. unsigned int read_bl_partial;
  1132. unsigned int write_blk_misalign;
  1133. unsigned int read_blk_misalign;
  1134. unsigned int dsr_imp;
  1135. unsigned int c_size;
  1136. unsigned int vdd_r_curr_min;
  1137. unsigned int vdd_r_curr_max;
  1138. unsigned int vdd_w_curr_min;
  1139. unsigned int vdd_w_curr_max;
  1140. unsigned int c_size_mult;
  1141. unsigned int erase_grp_size;
  1142. unsigned int erase_grp_mult;
  1143. unsigned int wp_grp_size;
  1144. unsigned int wp_grp_enable;
  1145. unsigned int default_ecc;
  1146. unsigned int r2w_factor;
  1147. unsigned int write_bl_len;
  1148. unsigned int write_bl_partial;
  1149. unsigned int content_prot_app;
  1150. unsigned int file_format_grp;
  1151. unsigned int copy;
  1152. unsigned int perm_write_protect;
  1153. unsigned int tmp_write_protect;
  1154. unsigned int file_format;
  1155. unsigned int ecc;
  1156. unsigned int crc;
  1157. unsigned int taac;
  1158. unsigned int tran_speed;
  1159. parse_bin(csd, "2u4u2r1r4u3u8u1r4u3u12u4u1u1u1u1u2r12u3u3u3u3u3u"
  1160. "5u5u5u1u2u3u4u1u4r1u1u1u1u1u2u2u7u1r",
  1161. &csd_structure, &spec_vers, &taac_timevalue,
  1162. &taac_timeunit, &nsac, &tran_speed_timevalue,
  1163. &tran_speed_transferrateunit, &ccc, &read_bl_len,
  1164. &read_bl_partial, &write_blk_misalign,
  1165. &read_blk_misalign, &dsr_imp, &c_size,
  1166. &vdd_r_curr_min, &vdd_r_curr_max,
  1167. &vdd_w_curr_min, &vdd_w_curr_max, &c_size_mult,
  1168. &erase_grp_size, &erase_grp_mult, &wp_grp_size,
  1169. &wp_grp_enable, &default_ecc, &r2w_factor,
  1170. &write_bl_len, &write_bl_partial, &content_prot_app,
  1171. &file_format_grp, &copy, &perm_write_protect,
  1172. &tmp_write_protect, &file_format, &ecc, &crc);
  1173. taac = taac_timevalue << 3 | taac_timeunit;
  1174. tran_speed = tran_speed_timevalue << 3 | tran_speed_transferrateunit;
  1175. if (config->verbose) {
  1176. float value;
  1177. int mult;
  1178. int blocknr;
  1179. int block_len;
  1180. unsigned long long blocks = 0;
  1181. int block_size = 0;
  1182. unsigned long long memory_capacity;
  1183. printf("======MMC/CSD======\n");
  1184. printf("\tCSD_STRUCTURE: 0x%01x (", csd_structure);
  1185. switch (csd_structure) {
  1186. case 0x0:
  1187. printf("v1.0)\n");
  1188. break;
  1189. case 0x1:
  1190. printf("v1.1)\n");
  1191. break;
  1192. case 0x2:
  1193. printf("v1.2)\n");
  1194. break;
  1195. case 0x3:
  1196. printf("version in ext_csd)\n");
  1197. break;
  1198. }
  1199. printf("\tSPEC_VERS: 0x%01x (", spec_vers);
  1200. switch (spec_vers) {
  1201. case 0x0:
  1202. printf("v1.0-v1.2)\n");
  1203. break;
  1204. case 0x1:
  1205. printf("v1.4)\n");
  1206. break;
  1207. case 0x2:
  1208. printf("v2.0-v2.2)\n");
  1209. break;
  1210. case 0x3:
  1211. printf("v3.1-v3.31)\n");
  1212. break;
  1213. case 0x4:
  1214. printf("v4.0-v4.3)\n");
  1215. break;
  1216. default:
  1217. printf("reserved)\n");
  1218. break;
  1219. }
  1220. printf("\tTAAC: 0x%02x (", taac);
  1221. switch (taac_timevalue) {
  1222. case 0x0:
  1223. value = 0.0f;
  1224. break;
  1225. case 0x1:
  1226. value = 1.0f;
  1227. break;
  1228. case 0x2:
  1229. value = 1.2f;
  1230. break;
  1231. case 0x3:
  1232. value = 1.3f;
  1233. break;
  1234. case 0x4:
  1235. value = 1.5f;
  1236. break;
  1237. case 0x5:
  1238. value = 2.0f;
  1239. break;
  1240. case 0x6:
  1241. value = 2.5f;
  1242. break;
  1243. case 0x7:
  1244. value = 3.0f;
  1245. break;
  1246. case 0x8:
  1247. value = 3.5f;
  1248. break;
  1249. case 0x9:
  1250. value = 4.0f;
  1251. break;
  1252. case 0xa:
  1253. value = 4.5f;
  1254. break;
  1255. case 0xb:
  1256. value = 5.0f;
  1257. break;
  1258. case 0xc:
  1259. value = 5.5f;
  1260. break;
  1261. case 0xd:
  1262. value = 6.0f;
  1263. break;
  1264. case 0xe:
  1265. value = 7.0f;
  1266. break;
  1267. case 0xf:
  1268. value = 8.0f;
  1269. break;
  1270. default:
  1271. value = 0.0f;
  1272. break;
  1273. }
  1274. switch (taac_timeunit) {
  1275. case 0x0:
  1276. printf("%.2fns)\n", value * 1.0f);
  1277. break;
  1278. case 0x1:
  1279. printf("%.2fns)\n", value * 10.0f);
  1280. break;
  1281. case 0x2:
  1282. printf("%.2fns)\n", value * 100.0f);
  1283. break;
  1284. case 0x3:
  1285. printf("%.2fus)\n", value * 1.0f);
  1286. break;
  1287. case 0x4:
  1288. printf("%.2fus)\n", value * 10.0f);
  1289. break;
  1290. case 0x5:
  1291. printf("%.2fus)\n", value * 100.0f);
  1292. break;
  1293. case 0x6:
  1294. printf("%.2fms)\n", value * 1.0f);
  1295. break;
  1296. case 0x7:
  1297. printf("%.2fms)\n", value * 10.0f);
  1298. break;
  1299. }
  1300. printf("\tNSAC: %d clocks\n", nsac);
  1301. printf("\tTRAN_SPEED: 0x%02x (", tran_speed);
  1302. switch (tran_speed_timevalue) {
  1303. case 0x0:
  1304. value = 0.0f;
  1305. break;
  1306. case 0x1:
  1307. value = 1.0f;
  1308. break;
  1309. case 0x2:
  1310. value = 1.2f;
  1311. break;
  1312. case 0x3:
  1313. value = 1.3f;
  1314. break;
  1315. case 0x4:
  1316. value = 1.5f;
  1317. break;
  1318. case 0x5:
  1319. value = 2.0f;
  1320. break;
  1321. case 0x6:
  1322. value = 2.6f;
  1323. break;
  1324. case 0x7:
  1325. value = 3.0f;
  1326. break;
  1327. case 0x8:
  1328. value = 3.5f;
  1329. break;
  1330. case 0x9:
  1331. value = 4.0f;
  1332. break;
  1333. case 0xa:
  1334. value = 4.5f;
  1335. break;
  1336. case 0xb:
  1337. value = 5.2f;
  1338. break;
  1339. case 0xc:
  1340. value = 5.5f;
  1341. break;
  1342. case 0xd:
  1343. value = 6.0f;
  1344. break;
  1345. case 0xe:
  1346. value = 7.0f;
  1347. break;
  1348. case 0xf:
  1349. value = 8.0f;
  1350. break;
  1351. default:
  1352. value = 0.0f;
  1353. break;
  1354. }
  1355. switch (tran_speed_transferrateunit) {
  1356. case 0x0:
  1357. printf("%.2fKHz/s)\n", value * 100.0f);
  1358. break;
  1359. case 0x1:
  1360. printf("%.2fMHz/s)\n", value * 1.0f);
  1361. break;
  1362. case 0x2:
  1363. printf("%.2fMHz/s)\n", value * 10.0f);
  1364. break;
  1365. case 0x3:
  1366. printf("%.2fMHz/s)\n", value * 100.0f);
  1367. break;
  1368. default:
  1369. printf("reserved)\n");
  1370. break;
  1371. }
  1372. printf("\tCCC: 0x%03x (class: ", ccc);
  1373. if (ccc & 0x800)
  1374. printf("11, ");
  1375. if (ccc & 0x400)
  1376. printf("10, ");
  1377. if (ccc & 0x200)
  1378. printf("9, ");
  1379. if (ccc & 0x100)
  1380. printf("8, ");
  1381. if (ccc & 0x080)
  1382. printf("7, ");
  1383. if (ccc & 0x040)
  1384. printf("6, ");
  1385. if (ccc & 0x020)
  1386. printf("5, ");
  1387. if (ccc & 0x010)
  1388. printf("4, ");
  1389. if (ccc & 0x008)
  1390. printf("3, ");
  1391. if (ccc & 0x004)
  1392. printf("2, ");
  1393. if (ccc & 0x002)
  1394. printf("1, ");
  1395. if (ccc & 0x001)
  1396. printf("0, ");
  1397. printf(" )\n");
  1398. printf("\tREAD_BL_LEN: 0x%01x (", read_bl_len);
  1399. switch (read_bl_len) {
  1400. case 0x0:
  1401. printf("1 byte)\n");
  1402. break;
  1403. case 0x1:
  1404. printf("2 byte)\n");
  1405. break;
  1406. case 0x2:
  1407. printf("4 byte)\n");
  1408. break;
  1409. case 0x3:
  1410. printf("8 byte)\n");
  1411. break;
  1412. case 0x4:
  1413. printf("16 byte)\n");
  1414. break;
  1415. case 0x5:
  1416. printf("32 byte)\n");
  1417. break;
  1418. case 0x6:
  1419. printf("64 byte)\n");
  1420. break;
  1421. case 0x7:
  1422. printf("128 byte)\n");
  1423. break;
  1424. case 0x8:
  1425. printf("256 byte)\n");
  1426. break;
  1427. case 0x9:
  1428. printf("512 bytes)\n");
  1429. break;
  1430. case 0xa:
  1431. printf("1024 bytes)\n");
  1432. break;
  1433. case 0xb:
  1434. printf("2048 bytes)\n");
  1435. break;
  1436. case 0xc:
  1437. printf("4096 bytes)\n");
  1438. break;
  1439. case 0xd:
  1440. printf("8192 bytes)\n");
  1441. break;
  1442. case 0xe:
  1443. printf("16K bytes)\n");
  1444. break;
  1445. default:
  1446. printf("reserved bytes)\n");
  1447. break;
  1448. }
  1449. printf("\tREAD_BL_PARTIAL: 0x%01x (", read_bl_partial);
  1450. switch (read_bl_partial) {
  1451. case 0x0:
  1452. printf("only 512 byte and READ_BL_LEN block size)\n");
  1453. break;
  1454. case 0x1:
  1455. printf("less than READ_BL_LEN block size can be used)\n");
  1456. break;
  1457. }
  1458. printf("\tWRITE_BLK_MISALIGN: 0x%01x (", write_blk_misalign);
  1459. switch (write_blk_misalign) {
  1460. case 0x0:
  1461. printf("writes across block boundaries are invalid)\n");
  1462. break;
  1463. case 0x1:
  1464. printf("writes across block boundaries are allowed)\n");
  1465. break;
  1466. }
  1467. printf("\tREAD_BLK_MISALIGN: 0x%01x (", read_blk_misalign);
  1468. switch (read_blk_misalign) {
  1469. case 0x0:
  1470. printf("reads across block boundaries are invalid)\n");
  1471. break;
  1472. case 0x1:
  1473. printf("reads across block boundaries are allowed)\n");
  1474. break;
  1475. }
  1476. printf("\tDSR_IMP: 0x%01x (", dsr_imp);
  1477. switch (dsr_imp) {
  1478. case 0x0:
  1479. printf("configurable driver stage not available)\n");
  1480. break;
  1481. case 0x1:
  1482. printf("configurable driver state available)\n");
  1483. break;
  1484. }
  1485. printf("\tC_SIZE: 0x%03x\n", c_size);
  1486. printf("\tVDD_R_CURR_MIN: 0x%01x (", vdd_r_curr_min);
  1487. switch (vdd_r_curr_min) {
  1488. case 0x0:
  1489. printf("0.5mA)\n");
  1490. break;
  1491. case 0x1:
  1492. printf("1mA)\n");
  1493. break;
  1494. case 0x2:
  1495. printf("5mA)\n");
  1496. break;
  1497. case 0x3:
  1498. printf("10mA)\n");
  1499. break;
  1500. case 0x4:
  1501. printf("25mA)\n");
  1502. break;
  1503. case 0x5:
  1504. printf("35mA)\n");
  1505. break;
  1506. case 0x6:
  1507. printf("60mA)\n");
  1508. break;
  1509. case 0x7:
  1510. printf("100mA)\n");
  1511. break;
  1512. }
  1513. printf("\tVDD_R_CURR_MAX: 0x%01x (", vdd_r_curr_max);
  1514. switch (vdd_r_curr_max) {
  1515. case 0x0:
  1516. printf("1mA)\n");
  1517. break;
  1518. case 0x1:
  1519. printf("5mA)\n");
  1520. break;
  1521. case 0x2:
  1522. printf("10mA)\n");
  1523. break;
  1524. case 0x3:
  1525. printf("25mA)\n");
  1526. break;
  1527. case 0x4:
  1528. printf("35mA)\n");
  1529. break;
  1530. case 0x5:
  1531. printf("45mA)\n");
  1532. break;
  1533. case 0x6:
  1534. printf("80mA)\n");
  1535. break;
  1536. case 0x7:
  1537. printf("200mA)\n");
  1538. break;
  1539. }
  1540. printf("\tVDD_W_CURR_MIN: 0x%01x (", vdd_w_curr_min);
  1541. switch (vdd_w_curr_min) {
  1542. case 0x0:
  1543. printf("0.5mA)\n");
  1544. break;
  1545. case 0x1:
  1546. printf("1mA)\n");
  1547. break;
  1548. case 0x2:
  1549. printf("5mA)\n");
  1550. break;
  1551. case 0x3:
  1552. printf("10mA)\n");
  1553. break;
  1554. case 0x4:
  1555. printf("25mA)\n");
  1556. break;
  1557. case 0x5:
  1558. printf("35mA)\n");
  1559. break;
  1560. case 0x6:
  1561. printf("60mA)\n");
  1562. break;
  1563. case 0x7:
  1564. printf("100mA)\n");
  1565. break;
  1566. }
  1567. printf("\tVDD_W_CURR_MAX: 0x%01x (", vdd_w_curr_max);
  1568. switch (vdd_w_curr_max) {
  1569. case 0x0:
  1570. printf("1mA)\n");
  1571. break;
  1572. case 0x1:
  1573. printf("5mA)\n");
  1574. break;
  1575. case 0x2:
  1576. printf("10mA)\n");
  1577. break;
  1578. case 0x3:
  1579. printf("25mA)\n");
  1580. break;
  1581. case 0x4:
  1582. printf("35mA)\n");
  1583. break;
  1584. case 0x5:
  1585. printf("45mA)\n");
  1586. break;
  1587. case 0x6:
  1588. printf("80mA)\n");
  1589. break;
  1590. case 0x7:
  1591. printf("200mA)\n");
  1592. break;
  1593. }
  1594. printf("\tC_SIZE_MULT: 0x%01x\n", c_size_mult);
  1595. printf("\tERASE_GRP_SIZE: 0x%02x\n", erase_grp_size);
  1596. printf("\tERASE_GRP_MULT: 0x%02x (%d write blocks/erase group)\n",
  1597. erase_grp_mult, (erase_grp_size + 1) *
  1598. (erase_grp_mult + 1));
  1599. printf("\tWP_GRP_SIZE: 0x%02x (%d blocks/write protect group)\n",
  1600. wp_grp_size, wp_grp_size + 1);
  1601. printf("\tWP_GRP_ENABLE: 0x%01x\n", wp_grp_enable);
  1602. printf("\tDEFAULT_ECC: 0x%01x (", default_ecc);
  1603. switch (default_ecc) {
  1604. case 0:
  1605. printf("none)\n");
  1606. break;
  1607. case 1:
  1608. printf("BCH)\n");
  1609. break;
  1610. default:
  1611. printf("reserved)\n");
  1612. break;
  1613. }
  1614. printf("\tR2W_FACTOR: 0x%01x (Write %d times read)\n",
  1615. r2w_factor, r2w_factor);
  1616. printf("\tWRITE_BL_LEN: 0x%01x (", write_bl_len);
  1617. switch (write_bl_len) {
  1618. case 0x0:
  1619. printf("1 byte)\n");
  1620. break;
  1621. case 0x1:
  1622. printf("2 byte)\n");
  1623. break;
  1624. case 0x2:
  1625. printf("4 byte)\n");
  1626. break;
  1627. case 0x3:
  1628. printf("8 byte)\n");
  1629. break;
  1630. case 0x4:
  1631. printf("16 byte)\n");
  1632. break;
  1633. case 0x5:
  1634. printf("32 byte)\n");
  1635. break;
  1636. case 0x6:
  1637. printf("64 byte)\n");
  1638. break;
  1639. case 0x7:
  1640. printf("128 byte)\n");
  1641. break;
  1642. case 0x8:
  1643. printf("256 byte)\n");
  1644. break;
  1645. case 0x9:
  1646. printf("512 bytes)\n");
  1647. break;
  1648. case 0xa:
  1649. printf("1024 bytes)\n");
  1650. break;
  1651. case 0xb:
  1652. printf("2048 bytes)\n");
  1653. break;
  1654. case 0xc:
  1655. printf("4096 bytes)\n");
  1656. break;
  1657. case 0xd:
  1658. printf("8192 bytes)\n");
  1659. break;
  1660. case 0xe:
  1661. printf("16K bytes)\n");
  1662. break;
  1663. default:
  1664. printf("reserved bytes)\n");
  1665. break;
  1666. }
  1667. printf("\tWRITE_BL_PARTIAL: 0x%01x (", write_bl_partial);
  1668. switch (write_bl_partial) {
  1669. case 0x0:
  1670. printf("only 512 byte and WRITE_BL_LEN block size)\n");
  1671. break;
  1672. case 0x1:
  1673. printf("less than WRITE_BL_LEN block size can be used)\n");
  1674. break;
  1675. }
  1676. printf("\tCONTENT_PROT_APP: 0x%01x\n", content_prot_app);
  1677. printf("\tFILE_FORMAT_GRP: 0x%01x\n", file_format_grp);
  1678. if (file_format_grp != 0)
  1679. printf("Warn: Invalid FILE_FORMAT_GRP\n");
  1680. printf("\tCOPY: 0x%01x\n", copy);
  1681. printf("\tPERM_WRITE_PROTECT: 0x%01x\n", perm_write_protect);
  1682. printf("\tTMP_WRITE_PROTECT: 0x%01x\n", tmp_write_protect);
  1683. printf("\tFILE_FORMAT: 0x%01x (", file_format);
  1684. if (file_format != 0)
  1685. printf("Warn: Invalid FILE_FORMAT\n");
  1686. if (file_format_grp == 1) {
  1687. printf("reserved)\n");
  1688. } else {
  1689. switch (file_format) {
  1690. case 0:
  1691. printf("partition table)\n");
  1692. break;
  1693. case 1:
  1694. printf("no partition table)\n");
  1695. break;
  1696. case 2:
  1697. printf("Universal File Format)\n");
  1698. break;
  1699. case 3:
  1700. printf("Others/unknown)\n");
  1701. break;
  1702. }
  1703. }
  1704. printf("\tECC: 0x%01x (", ecc);
  1705. switch (ecc) {
  1706. case 0:
  1707. printf("none)\n");
  1708. break;
  1709. case 1:
  1710. printf("BCH(542,512))\n");
  1711. break;
  1712. default:
  1713. printf("reserved)\n");
  1714. break;
  1715. }
  1716. printf("\tCRC: 0x%01x\n", crc);
  1717. mult = 1 << (c_size_mult + 2);
  1718. blocknr = (c_size + 1) * mult;
  1719. block_len = 1 << read_bl_len;
  1720. blocks = blocknr;
  1721. block_size = block_len;
  1722. memory_capacity = blocks * block_size;
  1723. printf("\tCAPACITY: ");
  1724. if (memory_capacity / (1024ull * 1024ull * 1024ull) > 0)
  1725. printf("%.2fGbyte",
  1726. memory_capacity / (1024.0 * 1024.0 * 1024.0));
  1727. else if (memory_capacity / (1024ull * 1024ull) > 0)
  1728. printf("%.2fMbyte", memory_capacity / (1024.0 * 1024.0));
  1729. else if (memory_capacity / (1024ull) > 0)
  1730. printf("%.2fKbyte", memory_capacity / (1024.0));
  1731. else
  1732. printf("%.2fbyte", memory_capacity * 1.0);
  1733. printf(" (%lld bytes, %lld sectors, %d bytes each)\n",
  1734. memory_capacity, blocks, block_size);
  1735. } else {
  1736. int mult;
  1737. int blocknr;
  1738. int block_len;
  1739. unsigned long long blocks = 0;
  1740. int block_size = 0;
  1741. unsigned long long memory_capacity;
  1742. printf("version: ");
  1743. switch (spec_vers) {
  1744. case 0x0:
  1745. printf("MMC v1.0-v1.2\n");
  1746. break;
  1747. case 0x1:
  1748. printf("MMC v1.4\n");
  1749. break;
  1750. case 0x2:
  1751. printf("MMC v2.0-v2.2\n");
  1752. break;
  1753. case 0x3:
  1754. printf("MMC v3.1-v3.31\n");
  1755. break;
  1756. case 0x4:
  1757. printf("MMC v4.0-v4.3\n");
  1758. break;
  1759. default:
  1760. printf("reserved\n");
  1761. break;
  1762. }
  1763. printf("card classes: ");
  1764. if (ccc & 0x800)
  1765. printf("11, ");
  1766. if (ccc & 0x400)
  1767. printf("10, ");
  1768. if (ccc & 0x200)
  1769. printf("9, ");
  1770. if (ccc & 0x100)
  1771. printf("8, ");
  1772. if (ccc & 0x080)
  1773. printf("7, ");
  1774. if (ccc & 0x040)
  1775. printf("6, ");
  1776. if (ccc & 0x020)
  1777. printf("5, ");
  1778. if (ccc & 0x010)
  1779. printf("4, ");
  1780. if (ccc & 0x008)
  1781. printf("3, ");
  1782. if (ccc & 0x004)
  1783. printf("2, ");
  1784. if (ccc & 0x002)
  1785. printf("1, ");
  1786. if (ccc & 0x001)
  1787. printf("0, ");
  1788. printf("\b\b\n");
  1789. mult = 1 << (c_size_mult + 2);
  1790. blocknr = (c_size + 1) * mult;
  1791. block_len = 1 << read_bl_len;
  1792. blocks = blocknr;
  1793. block_size = block_len;
  1794. memory_capacity = blocks * block_size;
  1795. printf("capacity: ");
  1796. if (memory_capacity / (1024ull * 1024ull * 1024ull) > 0)
  1797. printf("%.2fGbyte",
  1798. memory_capacity / (1024.0 * 1024.0 * 1024.0));
  1799. else if (memory_capacity / (1024ull * 1024ull) > 0)
  1800. printf("%.2fMbyte", memory_capacity / (1024.0 * 1024.0));
  1801. else if (memory_capacity / (1024ull) > 0)
  1802. printf("%.2fKbyte", memory_capacity / (1024.0));
  1803. else
  1804. printf("%.2fbyte", memory_capacity * 1.0);
  1805. printf(" (%lld bytes, %lld sectors, %d bytes each)\n",
  1806. memory_capacity, blocks, block_size);
  1807. }
  1808. }
  1809. char *speed_class_speed(unsigned char id, bool ddr)
  1810. {
  1811. if (ddr) {
  1812. switch (id) {
  1813. case 0x00: return "<4.8MB/s";
  1814. case 0x08: return " 4.8MB/s";
  1815. case 0x0a: return " 6.0MB/s";
  1816. case 0x0f: return " 9.0MB/s";
  1817. case 0x14: return "12.0MB/s";
  1818. case 0x1e: return "18.0MB/s";
  1819. case 0x28: return "24.0MB/s";
  1820. case 0x32: return "30.0MB/s";
  1821. case 0x3c: return "36.0MB/s";
  1822. case 0x46: return "42.0MB/s";
  1823. case 0x50: return "48.0MB/s";
  1824. case 0x64: return "60.0MB/s";
  1825. case 0x78: return "72.0MB/s";
  1826. case 0x8c: return "84.0MB/s";
  1827. case 0xa0: return "96.0MB/s";
  1828. default: return "??.?MB/s";
  1829. }
  1830. } else {
  1831. switch (id) {
  1832. case 0x00: return "<2.4MB/s";
  1833. case 0x08: return " 2.4MB/s";
  1834. case 0x0a: return " 3.0MB/s";
  1835. case 0x0f: return " 4.5MB/s";
  1836. case 0x14: return " 6.0MB/s";
  1837. case 0x1e: return " 9.0MB/s";
  1838. case 0x28: return "12.0MB/s";
  1839. case 0x32: return "15.0MB/s";
  1840. case 0x3c: return "18.0MB/s";
  1841. case 0x46: return "21.0MB/s";
  1842. case 0x50: return "24.0MB/s";
  1843. case 0x64: return "30.0MB/s";
  1844. case 0x78: return "36.0MB/s";
  1845. case 0x8c: return "42.0MB/s";
  1846. case 0xa0: return "48.0MB/s";
  1847. default: return "??.?MB/s";
  1848. }
  1849. }
  1850. }
  1851. char speed_class_name(unsigned char id)
  1852. {
  1853. switch (id) {
  1854. case 0x00: return '?';
  1855. case 0x08: return 'A';
  1856. case 0x0a: return 'B';
  1857. case 0x0f: return 'C';
  1858. case 0x14: return 'D';
  1859. case 0x1e: return 'E';
  1860. case 0x28: return 'F';
  1861. case 0x32: return 'G';
  1862. case 0x3c: return 'H';
  1863. case 0x46: return 'J';
  1864. case 0x50: return 'K';
  1865. case 0x64: return 'M';
  1866. case 0x78: return 'O';
  1867. case 0x8c: return 'R';
  1868. case 0xa0: return 'T';
  1869. default: return '?';
  1870. }
  1871. }
  1872. char *power_class_consumption(unsigned int id, bool volt360)
  1873. {
  1874. if (volt360) {
  1875. switch (id) {
  1876. case 0x0: return "100-200mA";
  1877. case 0x1: return "120-220mA";
  1878. case 0x2: return "150-250mA";
  1879. case 0x3: return "180-280mA";
  1880. case 0x4: return "200-300mA";
  1881. case 0x5: return "220-320mA";
  1882. case 0x6: return "250-350mA";
  1883. case 0x7: return "300-400mA";
  1884. case 0x8: return "350-450mA";
  1885. case 0x9: return "400-500mA";
  1886. case 0xa: return "450-550mA";
  1887. default: return "reserved";
  1888. }
  1889. } else {
  1890. switch (id) {
  1891. case 0x0: return "65-130mA";
  1892. case 0x1: return "70-140mA";
  1893. case 0x2: return "80-160mA";
  1894. case 0x3: return "90-180mA";
  1895. case 0x4: return "100-200mA";
  1896. case 0x5: return "120-220mA";
  1897. case 0x6: return "140-240mA";
  1898. case 0x7: return "160-260mA";
  1899. case 0x8: return "180-280mA";
  1900. case 0x9: return "200-300mA";
  1901. case 0xa: return "250-350mA";
  1902. default: return "reserved";
  1903. }
  1904. }
  1905. }
  1906. char *sleep_consumption(unsigned int id)
  1907. {
  1908. switch (id) {
  1909. case 0x00: return "not defined";
  1910. case 0x01: return "2uA";
  1911. case 0x02: return "4uA";
  1912. case 0x03: return "8uA";
  1913. case 0x04: return "16uA";
  1914. case 0x05: return "32uA";
  1915. case 0x06: return "64uA";
  1916. case 0x07: return "128uA";
  1917. case 0x08: return "0.256mA";
  1918. case 0x09: return "0.512mA";
  1919. case 0x0a: return "1.024mA";
  1920. case 0x0b: return "2.048mA";
  1921. case 0x0c: return "4.096mA";
  1922. case 0x0d: return "8.192mA";
  1923. default: return "reserved";
  1924. }
  1925. }
  1926. void print_sd_scr(struct config *config, char *scr)
  1927. {
  1928. unsigned int scr_structure;
  1929. unsigned int sd_spec;
  1930. unsigned int data_stat_after_erase;
  1931. unsigned int sd_security;
  1932. unsigned int sd_bus_widths;
  1933. unsigned int sd_spec3;
  1934. unsigned int ex_security;
  1935. unsigned int cmd_support;
  1936. parse_bin(scr, "4u4u1u3u4u1u4u9r2u32r",
  1937. &scr_structure, &sd_spec, &data_stat_after_erase,
  1938. &sd_security, &sd_bus_widths, &sd_spec3,
  1939. &ex_security, &cmd_support);
  1940. if (config->verbose) {
  1941. printf("======SD/SCR======\n");
  1942. printf("\tSCR_STRUCTURE: 0x%01x (", scr_structure);
  1943. switch (scr_structure) {
  1944. case 0:
  1945. printf("SCR v1.0)\n");
  1946. break;
  1947. default:
  1948. printf("reserved)\n");
  1949. break;
  1950. }
  1951. printf("\tSD_SPEC: 0x%01x (", sd_spec);
  1952. switch (sd_spec) {
  1953. case 0:
  1954. printf("SD v1.0/1.01)\n");
  1955. break;
  1956. case 1:
  1957. printf("SD v1.10)\n");
  1958. break;
  1959. case 2:
  1960. printf("SD v2.00/v3.0x)\n");
  1961. break;
  1962. case 3:
  1963. printf("SD v4.00)\n");
  1964. break;
  1965. default:
  1966. printf("reserved)\n");
  1967. break;
  1968. }
  1969. printf("\tDATA_STAT_AFTER_ERASE: 0x%01x\n",
  1970. data_stat_after_erase);
  1971. printf("\tSD_SECURITY: 0x%01x (", sd_security);
  1972. switch (sd_security) {
  1973. case 0:
  1974. printf("no security)\n");
  1975. break;
  1976. case 1:
  1977. printf("not used)\n");
  1978. break;
  1979. case 2:
  1980. printf("SDSC card/security v1.01)\n");
  1981. break;
  1982. case 3:
  1983. printf("SDHC card/security v2.00)\n");
  1984. break;
  1985. case 4:
  1986. printf("SDXC card/security v3.xx)\n");
  1987. break;
  1988. default:
  1989. printf("reserved)\n");
  1990. break;
  1991. }
  1992. printf("\tSD_BUS_WIDTHS: 0x%01x (", sd_bus_widths);
  1993. if (BITS(sd_bus_widths, 2, 2))
  1994. printf("4bit, ");
  1995. if (BITS(sd_bus_widths, 0, 0))
  1996. printf("1bit, ");
  1997. printf(" bus)\n");
  1998. printf("\tSD_SPEC3: 0x%01x (", sd_spec3);
  1999. if (sd_spec >= 2) {
  2000. switch (sd_spec3) {
  2001. case 0:
  2002. printf("SD v2.00)\n");
  2003. break;
  2004. case 1:
  2005. printf("SD v3.0x)\n");
  2006. break;
  2007. }
  2008. } else {
  2009. printf("SD 1.xx)\n");
  2010. }
  2011. printf("\tEX_SECURITY: 0x%01x\n", ex_security);
  2012. printf("\tCMD_SUPPORT: 0x%01x (", cmd_support);
  2013. if (BITS(cmd_support, 1, 1))
  2014. printf("CMD23 ");
  2015. if (BITS(cmd_support, 0, 0))
  2016. printf("CMD20 ");
  2017. printf(" )\n");
  2018. } else {
  2019. printf("version: ");
  2020. switch (sd_spec) {
  2021. case 0:
  2022. printf("SD 1.0/1.01\n");
  2023. break;
  2024. case 1:
  2025. printf("SD 1.10\n");
  2026. break;
  2027. case 2:
  2028. switch (sd_spec3) {
  2029. case 0:
  2030. printf("SD 2.00\n");
  2031. break;
  2032. case 1:
  2033. printf("SD 3.0x\n");
  2034. break;
  2035. default:
  2036. printf("unknown\n");
  2037. break;
  2038. }
  2039. break;
  2040. case 3:
  2041. printf("SD 4.00\n");
  2042. break;
  2043. default:
  2044. printf("unknown\n");
  2045. break;
  2046. }
  2047. printf("bus widths: ");
  2048. if (BITS(sd_bus_widths, 2, 2))
  2049. printf("4bit, ");
  2050. if (BITS(sd_bus_widths, 0, 0))
  2051. printf("1bit, ");
  2052. printf("\b\b\n");
  2053. }
  2054. }
  2055. /* MMC/SD interface processing functions */
  2056. void print_info(struct config *config, char *type,
  2057. char *cid, char *csd, char *scr, char *ext_csd)
  2058. {
  2059. printf("type: '%s'\n", type);
  2060. if (!strcmp(type, "SD") && cid)
  2061. print_sd_cid(config, cid);
  2062. else if (!strcmp(type, "MMC") && cid)
  2063. print_mmc_cid(config, cid);
  2064. if (!strcmp(type, "SD") && scr)
  2065. print_sd_scr(config, scr);
  2066. if (!strcmp(type, "MMC") && csd)
  2067. print_mmc_csd(config, csd);
  2068. else if (!strcmp(type, "SD") && csd)
  2069. print_sd_csd(config, csd);
  2070. }
  2071. int process_dir(struct config *config, enum REG_TYPE reg)
  2072. {
  2073. char *type = NULL, *cid = NULL, *csd = NULL, *scr = NULL, *ext_csd = NULL;
  2074. int ret = 0;
  2075. if (chdir(config->dir) < 0) {
  2076. fprintf(stderr,
  2077. "MMC/SD information directory '%s' does not exist.\n",
  2078. config->dir);
  2079. return -1;
  2080. }
  2081. type = read_file("type");
  2082. if (!type) {
  2083. fprintf(stderr,
  2084. "Could not read card interface type in directory '%s'.\n",
  2085. config->dir);
  2086. return -1;
  2087. }
  2088. if (strcmp(type, "MMC") && strcmp(type, "SD")) {
  2089. fprintf(stderr, "Unknown type: '%s'\n", type);
  2090. ret = -1;
  2091. goto err;
  2092. }
  2093. switch (reg) {
  2094. case CID:
  2095. cid = read_file("cid");
  2096. if (!cid) {
  2097. fprintf(stderr,
  2098. "Could not read card identity in directory '%s'.\n",
  2099. config->dir);
  2100. ret = -1;
  2101. goto err;
  2102. }
  2103. break;
  2104. case CSD:
  2105. csd = read_file("csd");
  2106. if (!csd) {
  2107. fprintf(stderr,
  2108. "Could not read card specific data in "
  2109. "directory '%s'.\n", config->dir);
  2110. ret = -1;
  2111. goto err;
  2112. }
  2113. break;
  2114. case SCR:
  2115. if (!strcmp(type, "SD")) {
  2116. scr = read_file("scr");
  2117. if (!scr) {
  2118. fprintf(stderr, "Could not read SD card "
  2119. "configuration in directory '%s'.\n",
  2120. config->dir);
  2121. ret = -1;
  2122. goto err;
  2123. }
  2124. }
  2125. break;
  2126. case EXT_CSD:
  2127. if (!strcmp(type, "MMC")) {
  2128. ext_csd = read_file("ext_csd");
  2129. if (!ext_csd) {
  2130. fprintf(stderr, "Could not read extra specific "
  2131. "data in directory '%s'.\n",
  2132. config->dir);
  2133. ret = -1;
  2134. goto err;
  2135. }
  2136. }
  2137. break;
  2138. default:
  2139. goto err;
  2140. }
  2141. print_info(config, type, cid, csd, scr, ext_csd);
  2142. err:
  2143. free(ext_csd);
  2144. free(scr);
  2145. free(csd);
  2146. free(cid);
  2147. free(type);
  2148. return ret;
  2149. }
  2150. int lsmmc_main(struct config *config, int argc, char **argv)
  2151. {
  2152. int ret;
  2153. memset(config, 0, sizeof(*config));
  2154. config->mmc_ids = calloc(IDS_MAX, sizeof(char *));
  2155. config->sd_ids = calloc(IDS_MAX, sizeof(char *));
  2156. if (!config->mmc_ids || !config->sd_ids) {
  2157. fprintf(stderr, "Could not allocate memory for lsmmc.\n");
  2158. return -1;
  2159. }
  2160. ret = parse_opts(argc, argv, config);
  2161. if (ret)
  2162. return ret;
  2163. return parse_ids(config);
  2164. }
  2165. void lsmmc_free(struct config *config)
  2166. {
  2167. free(config->mmc_ids);
  2168. free(config->sd_ids);
  2169. free(config->dir);
  2170. }
  2171. int do_read_csd(int argc, char **argv)
  2172. {
  2173. struct config config;
  2174. int ret;
  2175. if (argc != 2 && argc != 3) {
  2176. fprintf(stderr, "Usage: Print CSD data from <device path>.\n");
  2177. exit(1);
  2178. }
  2179. ret = lsmmc_main(&config, argc, argv);
  2180. if (ret)
  2181. goto out;
  2182. if (config.dir)
  2183. ret = process_dir(&config, CSD);
  2184. out:
  2185. lsmmc_free(&config);
  2186. return ret;
  2187. }
  2188. int do_read_cid(int argc, char **argv)
  2189. {
  2190. struct config config;
  2191. int ret;
  2192. if (argc != 2 && argc != 3) {
  2193. fprintf(stderr, "Usage: Print CID data from <device path>.\n");
  2194. exit(1);
  2195. }
  2196. ret = lsmmc_main(&config, argc, argv);
  2197. if (ret)
  2198. goto out;
  2199. if (config.dir)
  2200. ret = process_dir(&config, CID);
  2201. out:
  2202. lsmmc_free(&config);
  2203. return ret;
  2204. }
  2205. int do_read_scr(int argc, char **argv)
  2206. {
  2207. struct config config;
  2208. int ret;
  2209. if (argc != 2 && argc != 3) {
  2210. fprintf(stderr, "Usage: Print SCR data from <device path>.\n");
  2211. exit(1);
  2212. }
  2213. ret = lsmmc_main(&config, argc, argv);
  2214. if (ret)
  2215. goto out;
  2216. if (config.dir)
  2217. ret = process_dir(&config, SCR);
  2218. out:
  2219. lsmmc_free(&config);
  2220. return ret;
  2221. }