Message ID | 20230926100006.1789276-3-avri.altman@wdc.com |
---|---|
State | Superseded |
Headers | show |
Series | Revamp CSD register parsing | expand |
On Tue, 26 Sept 2023 at 12:02, Avri Altman <avri.altman@wdc.com> wrote: > > We used to have this odd way of printing the manufacturer name. To that > end we cached the entire database beforehand which is completely > useless. > > While at it, get rid of some more redundant code. > > Signed-off-by: Avri Altman <avri.altman@wdc.com> I tried building this for arm64 but get the following errors/warnings: lsmmc.c: In function ‘get_manufacturer’: lsmmc.c:56:38: error: division ‘sizeof (struct ids_database *) / sizeof (struct ids_database)’ does not compute the number of array elements [-Werror=sizeof-pointer-div] 56 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | ^ lsmmc.c:287:25: note: in expansion of macro ‘ARRAY_SIZE’ 287 | unsigned int ids_cnt = ARRAY_SIZE(db); | ^~~~~~~~~~ lsmmc.c:286:23: note: first ‘sizeof’ operand was declared here 286 | struct ids_database *db = config->bus == MMC ? mmc_database : sd_database; | ^~ cc1: all warnings being treated as errors make: *** [Makefile:38: lsmmc.o] Error 1 Can you have a look? Kind regards Uffe > --- > lsmmc.c | 150 ++++++++++++++------------------------------------------ > 1 file changed, 37 insertions(+), 113 deletions(-) > > diff --git a/lsmmc.c b/lsmmc.c > index 85779bb..7e3165a 100644 > --- a/lsmmc.c > +++ b/lsmmc.c > @@ -53,16 +53,19 @@ > #define MASK(high, low) (MASKTOBIT0(high) & ~MASKTOBIT0(low - 1)) > #define BITS(value, high, low) (((value) & MASK((high), (low))) >> (low)) > #define IDS_MAX 256 > +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) > + > +enum bus_type { > + MMC = 1, > + SD, > +}; > > struct config { > char *idsfile; > char *dir; > bool verbose; > - int interfaces; > - char **interface; > - char **mmc_ids; > - char **sd_ids; > > + enum bus_type bus; > char *type; > char *cid; > char *csd; > @@ -78,189 +81,157 @@ enum REG_TYPE { > }; > > struct ids_database { > - char *type; > int id; > char *manufacturer; > }; > > -struct ids_database database[] = { > +static struct ids_database sd_database[] = { > { > - .type = "sd", > .id = 0x01, > .manufacturer = "Panasonic", > }, > { > - .type = "sd", > .id = 0x02, > .manufacturer = "Toshiba/Kingston/Viking", > }, > { > - .type = "sd", > .id = 0x03, > .manufacturer = "SanDisk", > }, > { > - .type = "sd", > .id = 0x08, > .manufacturer = "Silicon Power", > }, > { > - .type = "sd", > .id = 0x18, > .manufacturer = "Infineon", > }, > { > - .type = "sd", > .id = 0x1b, > .manufacturer = "Transcend/Samsung", > }, > { > - .type = "sd", > .id = 0x1c, > .manufacturer = "Transcend", > }, > { > - .type = "sd", > .id = 0x1d, > .manufacturer = "Corsair/AData", > }, > { > - .type = "sd", > .id = 0x1e, > .manufacturer = "Transcend", > }, > { > - .type = "sd", > .id = 0x1f, > .manufacturer = "Kingston", > }, > { > - .type = "sd", > .id = 0x27, > .manufacturer = "Delkin/Phison", > }, > { > - .type = "sd", > .id = 0x28, > .manufacturer = "Lexar", > }, > { > - .type = "sd", > .id = 0x30, > .manufacturer = "SanDisk", > }, > { > - .type = "sd", > .id = 0x31, > .manufacturer = "Silicon Power", > }, > { > - .type = "sd", > .id = 0x33, > .manufacturer = "STMicroelectronics", > }, > { > - .type = "sd", > .id = 0x41, > .manufacturer = "Kingston", > }, > { > - .type = "sd", > .id = 0x6f, > .manufacturer = "STMicroelectronics", > }, > { > - .type = "sd", > .id = 0x74, > .manufacturer = "Transcend", > }, > { > - .type = "sd", > .id = 0x76, > .manufacturer = "Patriot", > }, > { > - .type = "sd", > .id = 0x82, > .manufacturer = "Gobe/Sony", > }, > { > - .type = "sd", > .id = 0x89, > .manufacturer = "Unknown", > }, > +}; > + > +static struct ids_database mmc_database[] = { > { > - .type = "mmc", > .id = 0x00, > .manufacturer = "SanDisk", > }, > { > - .type = "mmc", > .id = 0x02, > .manufacturer = "Kingston/SanDisk", > }, > { > - .type = "mmc", > .id = 0x03, > .manufacturer = "Toshiba", > }, > { > - .type = "mmc", > .id = 0x05, > .manufacturer = "Unknown", > }, > { > - .type = "mmc", > .id = 0x06, > .manufacturer = "Unknown", > }, > { > - .type = "mmc", > .id = 0x11, > .manufacturer = "Toshiba", > }, > { > - .type = "mmc", > .id = 0x13, > .manufacturer = "Micron", > }, > { > - .type = "mmc", > .id = 0x15, > .manufacturer = "Samsung/SanDisk/LG", > }, > { > - .type = "mmc", > .id = 0x37, > .manufacturer = "KingMax", > }, > { > - .type = "mmc", > .id = 0x44, > .manufacturer = "ATP", > }, > { > - .type = "mmc", > .id = 0x45, > .manufacturer = "SanDisk Corporation", > }, > { > - .type = "mmc", > .id = 0x2c, > .manufacturer = "Kingston", > }, > { > - .type = "mmc", > .id = 0x70, > .manufacturer = "Kingston", > }, > { > - .type = "mmc", > .id = 0xfe, > .manufacturer = "Micron", > }, > }; > > + > /* Command line parsing functions */ > void usage(void) > { > @@ -310,47 +281,18 @@ int parse_opts(int argc, char **argv, struct config *config) > return 0; > } > > -int parse_ids(struct config *config) > +static char *get_manufacturer(struct config *config, unsigned int manid) > { > - unsigned int ids_cnt = sizeof(database) / sizeof(struct ids_database); > - unsigned int value; > - char **ids; > - char *type; > + struct ids_database *db = config->bus == MMC ? mmc_database : sd_database; > + unsigned int ids_cnt = ARRAY_SIZE(db); > int i; > > for (i = 0; i < ids_cnt; i++) { > - type = database[i].type; > - > - if (!strcmp(type, "mmc")) { > - ids = config->mmc_ids; > - } else if (!strcmp(type, "sd")) { > - ids = config->sd_ids; > - } else { > - fprintf(stderr, > - "MMC/SD id parse error, unknown type: '%s'.\n", > - type); > - return -1; > - } > - > - value = database[i].id; > - > - if (value >= IDS_MAX) { > - fprintf(stderr, > - "MMC/SD id parse error, id out of range.\n"); > - return -1; > - } > - > - if (ids[value]) { > - fprintf(stderr, > - "Duplicate entries: type='%s', id='0x%1x'.\n", > - type, value); > - return -1; > - } > - > - ids[value] = database[i].manufacturer; > + if (db[i].id == manid) > + return db[i].manufacturer; > } > > - return 0; > + return NULL; > } > > /* MMC/SD file parsing functions */ > @@ -538,6 +480,7 @@ void print_sd_cid(struct config *config, char *cid) > unsigned int mdt_month; > unsigned int mdt_year; > unsigned int crc; > + char *manufacturer = NULL; > > parse_bin(cid, "8u16a40a4u4u32u4r8u4u7u1r", > &mid, &oid[0], &pnm[0], &prv_major, &prv_minor, &psn, > @@ -546,12 +489,14 @@ void print_sd_cid(struct config *config, char *cid) > oid[2] = '\0'; > pnm[5] = '\0'; > > + manufacturer = get_manufacturer(config, mid); > + > if (config->verbose) { > printf("======SD/CID======\n"); > > printf("\tMID: 0x%02x (", mid); > - if (config->sd_ids[mid]) > - printf("%s)\n", config->sd_ids[mid]); > + if (manufacturer) > + printf("%s)\n", manufacturer); > else > printf("Unlisted)\n"); > > @@ -564,9 +509,9 @@ void print_sd_cid(struct config *config, char *cid) > 2000 + mdt_year, months[mdt_month]); > printf("\tCRC: 0x%02x\n", crc); > } else { > - if (config->sd_ids[mid]) > + if (manufacturer) > printf("manufacturer: '%s' '%s'\n", > - config->sd_ids[mid], oid); > + manufacturer, oid); > else > printf("manufacturer: 'Unlisted' '%s'\n", oid); > > @@ -594,6 +539,7 @@ void print_mmc_cid(struct config *config, char *cid) > unsigned int mdt_month; > unsigned int mdt_year; > unsigned int crc; > + char *manufacturer = NULL; > > parse_bin(cid, "8u6r2u8u48a4u4u32u4u4u7u1r", > &mid, &cbx, &oid, &pnm[0], &prv_major, &prv_minor, &psn, > @@ -601,12 +547,14 @@ void print_mmc_cid(struct config *config, char *cid) > > pnm[6] = '\0'; > > + manufacturer = get_manufacturer(config, mid); > + > if (config->verbose) { > printf("======MMC/CID======\n"); > > printf("\tMID: 0x%02x (", mid); > - if (config->mmc_ids[mid]) > - printf("%s)\n", config->mmc_ids[mid]); > + if (manufacturer) > + printf("%s)\n", manufacturer); > else > printf("Unlisted)\n"); > > @@ -635,9 +583,9 @@ void print_mmc_cid(struct config *config, char *cid) > 1997 + mdt_year, months[mdt_month]); > printf("\tCRC: 0x%02x\n", crc); > } else { > - if (config->mmc_ids[mid]) > + if (manufacturer) > printf("manufacturer: 0x%02x (%s) oid: 0x%01x\n", > - mid, config->mmc_ids[mid], oid); > + mid, manufacturer, oid); > else > printf("manufacturer: 0x%02x (Unlisted) oid: 0x%01x\n", mid, oid); > > @@ -2308,6 +2256,8 @@ int process_dir(struct config *config, enum REG_TYPE reg) > goto err; > } > > + config->bus = strcmp(type, "MMC") ? SD : MMC; > + > switch (reg) { > case CID: > cid = read_file("cid"); > @@ -2369,45 +2319,19 @@ err: > return ret; > } > > -int lsmmc_main(struct config *config, int argc, char **argv) > -{ > - int ret; > - > - config->mmc_ids = calloc(IDS_MAX, sizeof(char *)); > - config->sd_ids = calloc(IDS_MAX, sizeof(char *)); > - if (!config->mmc_ids || !config->sd_ids) { > - fprintf(stderr, "Could not allocate memory for lsmmc.\n"); > - return -1; > - } > - > - ret = parse_opts(argc, argv, config); > - if (ret) > - return ret; > - > - return parse_ids(config); > -} > - > -void lsmmc_free(struct config *config) > -{ > - free(config->mmc_ids); > - free(config->sd_ids); > - free(config->dir); > -} > - > static int do_read_reg(int argc, char **argv, enum REG_TYPE reg) > { > struct config cfg = {}; > int ret; > > - ret = lsmmc_main(&cfg, argc, argv); > + ret = parse_opts(argc, argv, &cfg); > if (ret) > - goto out; > + return ret; > > if (cfg.dir) > ret = process_dir(&cfg, reg); > > -out: > - lsmmc_free(&cfg); > + free(cfg.dir); > > return ret; > > -- > 2.42.0 >
> On Tue, 26 Sept 2023 at 12:02, Avri Altman <avri.altman@wdc.com> wrote: > > > > We used to have this odd way of printing the manufacturer name. To that > > end we cached the entire database beforehand which is completely > > useless. > > > > While at it, get rid of some more redundant code. > > > > Signed-off-by: Avri Altman <avri.altman@wdc.com> > > I tried building this for arm64 but get the following errors/warnings: > > lsmmc.c: In function ‘get_manufacturer’: > lsmmc.c:56:38: error: division ‘sizeof (struct ids_database *) / > sizeof (struct ids_database)’ does not compute the number of array > elements [-Werror=sizeof-pointer-div] > 56 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) > | ^ > lsmmc.c:287:25: note: in expansion of macro ‘ARRAY_SIZE’ > 287 | unsigned int ids_cnt = ARRAY_SIZE(db); > | ^~~~~~~~~~ > lsmmc.c:286:23: note: first ‘sizeof’ operand was declared here > 286 | struct ids_database *db = config->bus == MMC ? mmc_database : > sd_database; > | ^~ > cc1: all warnings being treated as errors > make: *** [Makefile:38: lsmmc.o] Error 1 > > Can you have a look? Yes. Thanks. Avri > > Kind regards > Uffe > > > --- > > lsmmc.c | 150 ++++++++++++++------------------------------------------ > > 1 file changed, 37 insertions(+), 113 deletions(-) > > > > diff --git a/lsmmc.c b/lsmmc.c > > index 85779bb..7e3165a 100644 > > --- a/lsmmc.c > > +++ b/lsmmc.c > > @@ -53,16 +53,19 @@ > > #define MASK(high, low) (MASKTOBIT0(high) & ~MASKTOBIT0(low > - 1)) > > #define BITS(value, high, low) (((value) & MASK((high), (low))) >> (low)) > > #define IDS_MAX 256 > > +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) > > + > > +enum bus_type { > > + MMC = 1, > > + SD, > > +}; > > > > struct config { > > char *idsfile; > > char *dir; > > bool verbose; > > - int interfaces; > > - char **interface; > > - char **mmc_ids; > > - char **sd_ids; > > > > + enum bus_type bus; > > char *type; > > char *cid; > > char *csd; > > @@ -78,189 +81,157 @@ enum REG_TYPE { > > }; > > > > struct ids_database { > > - char *type; > > int id; > > char *manufacturer; > > }; > > > > -struct ids_database database[] = { > > +static struct ids_database sd_database[] = { > > { > > - .type = "sd", > > .id = 0x01, > > .manufacturer = "Panasonic", > > }, > > { > > - .type = "sd", > > .id = 0x02, > > .manufacturer = "Toshiba/Kingston/Viking", > > }, > > { > > - .type = "sd", > > .id = 0x03, > > .manufacturer = "SanDisk", > > }, > > { > > - .type = "sd", > > .id = 0x08, > > .manufacturer = "Silicon Power", > > }, > > { > > - .type = "sd", > > .id = 0x18, > > .manufacturer = "Infineon", > > }, > > { > > - .type = "sd", > > .id = 0x1b, > > .manufacturer = "Transcend/Samsung", > > }, > > { > > - .type = "sd", > > .id = 0x1c, > > .manufacturer = "Transcend", > > }, > > { > > - .type = "sd", > > .id = 0x1d, > > .manufacturer = "Corsair/AData", > > }, > > { > > - .type = "sd", > > .id = 0x1e, > > .manufacturer = "Transcend", > > }, > > { > > - .type = "sd", > > .id = 0x1f, > > .manufacturer = "Kingston", > > }, > > { > > - .type = "sd", > > .id = 0x27, > > .manufacturer = "Delkin/Phison", > > }, > > { > > - .type = "sd", > > .id = 0x28, > > .manufacturer = "Lexar", > > }, > > { > > - .type = "sd", > > .id = 0x30, > > .manufacturer = "SanDisk", > > }, > > { > > - .type = "sd", > > .id = 0x31, > > .manufacturer = "Silicon Power", > > }, > > { > > - .type = "sd", > > .id = 0x33, > > .manufacturer = "STMicroelectronics", > > }, > > { > > - .type = "sd", > > .id = 0x41, > > .manufacturer = "Kingston", > > }, > > { > > - .type = "sd", > > .id = 0x6f, > > .manufacturer = "STMicroelectronics", > > }, > > { > > - .type = "sd", > > .id = 0x74, > > .manufacturer = "Transcend", > > }, > > { > > - .type = "sd", > > .id = 0x76, > > .manufacturer = "Patriot", > > }, > > { > > - .type = "sd", > > .id = 0x82, > > .manufacturer = "Gobe/Sony", > > }, > > { > > - .type = "sd", > > .id = 0x89, > > .manufacturer = "Unknown", > > }, > > +}; > > + > > +static struct ids_database mmc_database[] = { > > { > > - .type = "mmc", > > .id = 0x00, > > .manufacturer = "SanDisk", > > }, > > { > > - .type = "mmc", > > .id = 0x02, > > .manufacturer = "Kingston/SanDisk", > > }, > > { > > - .type = "mmc", > > .id = 0x03, > > .manufacturer = "Toshiba", > > }, > > { > > - .type = "mmc", > > .id = 0x05, > > .manufacturer = "Unknown", > > }, > > { > > - .type = "mmc", > > .id = 0x06, > > .manufacturer = "Unknown", > > }, > > { > > - .type = "mmc", > > .id = 0x11, > > .manufacturer = "Toshiba", > > }, > > { > > - .type = "mmc", > > .id = 0x13, > > .manufacturer = "Micron", > > }, > > { > > - .type = "mmc", > > .id = 0x15, > > .manufacturer = "Samsung/SanDisk/LG", > > }, > > { > > - .type = "mmc", > > .id = 0x37, > > .manufacturer = "KingMax", > > }, > > { > > - .type = "mmc", > > .id = 0x44, > > .manufacturer = "ATP", > > }, > > { > > - .type = "mmc", > > .id = 0x45, > > .manufacturer = "SanDisk Corporation", > > }, > > { > > - .type = "mmc", > > .id = 0x2c, > > .manufacturer = "Kingston", > > }, > > { > > - .type = "mmc", > > .id = 0x70, > > .manufacturer = "Kingston", > > }, > > { > > - .type = "mmc", > > .id = 0xfe, > > .manufacturer = "Micron", > > }, > > }; > > > > + > > /* Command line parsing functions */ > > void usage(void) > > { > > @@ -310,47 +281,18 @@ int parse_opts(int argc, char **argv, struct > config *config) > > return 0; > > } > > > > -int parse_ids(struct config *config) > > +static char *get_manufacturer(struct config *config, unsigned int manid) > > { > > - unsigned int ids_cnt = sizeof(database) / sizeof(struct ids_database); > > - unsigned int value; > > - char **ids; > > - char *type; > > + struct ids_database *db = config->bus == MMC ? mmc_database : > sd_database; > > + unsigned int ids_cnt = ARRAY_SIZE(db); > > int i; > > > > for (i = 0; i < ids_cnt; i++) { > > - type = database[i].type; > > - > > - if (!strcmp(type, "mmc")) { > > - ids = config->mmc_ids; > > - } else if (!strcmp(type, "sd")) { > > - ids = config->sd_ids; > > - } else { > > - fprintf(stderr, > > - "MMC/SD id parse error, unknown type: '%s'.\n", > > - type); > > - return -1; > > - } > > - > > - value = database[i].id; > > - > > - if (value >= IDS_MAX) { > > - fprintf(stderr, > > - "MMC/SD id parse error, id out of range.\n"); > > - return -1; > > - } > > - > > - if (ids[value]) { > > - fprintf(stderr, > > - "Duplicate entries: type='%s', id='0x%1x'.\n", > > - type, value); > > - return -1; > > - } > > - > > - ids[value] = database[i].manufacturer; > > + if (db[i].id == manid) > > + return db[i].manufacturer; > > } > > > > - return 0; > > + return NULL; > > } > > > > /* MMC/SD file parsing functions */ > > @@ -538,6 +480,7 @@ void print_sd_cid(struct config *config, char *cid) > > unsigned int mdt_month; > > unsigned int mdt_year; > > unsigned int crc; > > + char *manufacturer = NULL; > > > > parse_bin(cid, "8u16a40a4u4u32u4r8u4u7u1r", > > &mid, &oid[0], &pnm[0], &prv_major, &prv_minor, &psn, > > @@ -546,12 +489,14 @@ void print_sd_cid(struct config *config, char > *cid) > > oid[2] = '\0'; > > pnm[5] = '\0'; > > > > + manufacturer = get_manufacturer(config, mid); > > + > > if (config->verbose) { > > printf("======SD/CID======\n"); > > > > printf("\tMID: 0x%02x (", mid); > > - if (config->sd_ids[mid]) > > - printf("%s)\n", config->sd_ids[mid]); > > + if (manufacturer) > > + printf("%s)\n", manufacturer); > > else > > printf("Unlisted)\n"); > > > > @@ -564,9 +509,9 @@ void print_sd_cid(struct config *config, char *cid) > > 2000 + mdt_year, months[mdt_month]); > > printf("\tCRC: 0x%02x\n", crc); > > } else { > > - if (config->sd_ids[mid]) > > + if (manufacturer) > > printf("manufacturer: '%s' '%s'\n", > > - config->sd_ids[mid], oid); > > + manufacturer, oid); > > else > > printf("manufacturer: 'Unlisted' '%s'\n", oid); > > > > @@ -594,6 +539,7 @@ void print_mmc_cid(struct config *config, char > *cid) > > unsigned int mdt_month; > > unsigned int mdt_year; > > unsigned int crc; > > + char *manufacturer = NULL; > > > > parse_bin(cid, "8u6r2u8u48a4u4u32u4u4u7u1r", > > &mid, &cbx, &oid, &pnm[0], &prv_major, &prv_minor, &psn, > > @@ -601,12 +547,14 @@ void print_mmc_cid(struct config *config, char > *cid) > > > > pnm[6] = '\0'; > > > > + manufacturer = get_manufacturer(config, mid); > > + > > if (config->verbose) { > > printf("======MMC/CID======\n"); > > > > printf("\tMID: 0x%02x (", mid); > > - if (config->mmc_ids[mid]) > > - printf("%s)\n", config->mmc_ids[mid]); > > + if (manufacturer) > > + printf("%s)\n", manufacturer); > > else > > printf("Unlisted)\n"); > > > > @@ -635,9 +583,9 @@ void print_mmc_cid(struct config *config, char > *cid) > > 1997 + mdt_year, months[mdt_month]); > > printf("\tCRC: 0x%02x\n", crc); > > } else { > > - if (config->mmc_ids[mid]) > > + if (manufacturer) > > printf("manufacturer: 0x%02x (%s) oid: 0x%01x\n", > > - mid, config->mmc_ids[mid], oid); > > + mid, manufacturer, oid); > > else > > printf("manufacturer: 0x%02x (Unlisted) oid: 0x%01x\n", mid, > oid); > > > > @@ -2308,6 +2256,8 @@ int process_dir(struct config *config, enum > REG_TYPE reg) > > goto err; > > } > > > > + config->bus = strcmp(type, "MMC") ? SD : MMC; > > + > > switch (reg) { > > case CID: > > cid = read_file("cid"); > > @@ -2369,45 +2319,19 @@ err: > > return ret; > > } > > > > -int lsmmc_main(struct config *config, int argc, char **argv) > > -{ > > - int ret; > > - > > - config->mmc_ids = calloc(IDS_MAX, sizeof(char *)); > > - config->sd_ids = calloc(IDS_MAX, sizeof(char *)); > > - if (!config->mmc_ids || !config->sd_ids) { > > - fprintf(stderr, "Could not allocate memory for lsmmc.\n"); > > - return -1; > > - } > > - > > - ret = parse_opts(argc, argv, config); > > - if (ret) > > - return ret; > > - > > - return parse_ids(config); > > -} > > - > > -void lsmmc_free(struct config *config) > > -{ > > - free(config->mmc_ids); > > - free(config->sd_ids); > > - free(config->dir); > > -} > > - > > static int do_read_reg(int argc, char **argv, enum REG_TYPE reg) > > { > > struct config cfg = {}; > > int ret; > > > > - ret = lsmmc_main(&cfg, argc, argv); > > + ret = parse_opts(argc, argv, &cfg); > > if (ret) > > - goto out; > > + return ret; > > > > if (cfg.dir) > > ret = process_dir(&cfg, reg); > > > > -out: > > - lsmmc_free(&cfg); > > + free(cfg.dir); > > > > return ret; > > > > -- > > 2.42.0 > >
diff --git a/lsmmc.c b/lsmmc.c index 85779bb..7e3165a 100644 --- a/lsmmc.c +++ b/lsmmc.c @@ -53,16 +53,19 @@ #define MASK(high, low) (MASKTOBIT0(high) & ~MASKTOBIT0(low - 1)) #define BITS(value, high, low) (((value) & MASK((high), (low))) >> (low)) #define IDS_MAX 256 +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +enum bus_type { + MMC = 1, + SD, +}; struct config { char *idsfile; char *dir; bool verbose; - int interfaces; - char **interface; - char **mmc_ids; - char **sd_ids; + enum bus_type bus; char *type; char *cid; char *csd; @@ -78,189 +81,157 @@ enum REG_TYPE { }; struct ids_database { - char *type; int id; char *manufacturer; }; -struct ids_database database[] = { +static struct ids_database sd_database[] = { { - .type = "sd", .id = 0x01, .manufacturer = "Panasonic", }, { - .type = "sd", .id = 0x02, .manufacturer = "Toshiba/Kingston/Viking", }, { - .type = "sd", .id = 0x03, .manufacturer = "SanDisk", }, { - .type = "sd", .id = 0x08, .manufacturer = "Silicon Power", }, { - .type = "sd", .id = 0x18, .manufacturer = "Infineon", }, { - .type = "sd", .id = 0x1b, .manufacturer = "Transcend/Samsung", }, { - .type = "sd", .id = 0x1c, .manufacturer = "Transcend", }, { - .type = "sd", .id = 0x1d, .manufacturer = "Corsair/AData", }, { - .type = "sd", .id = 0x1e, .manufacturer = "Transcend", }, { - .type = "sd", .id = 0x1f, .manufacturer = "Kingston", }, { - .type = "sd", .id = 0x27, .manufacturer = "Delkin/Phison", }, { - .type = "sd", .id = 0x28, .manufacturer = "Lexar", }, { - .type = "sd", .id = 0x30, .manufacturer = "SanDisk", }, { - .type = "sd", .id = 0x31, .manufacturer = "Silicon Power", }, { - .type = "sd", .id = 0x33, .manufacturer = "STMicroelectronics", }, { - .type = "sd", .id = 0x41, .manufacturer = "Kingston", }, { - .type = "sd", .id = 0x6f, .manufacturer = "STMicroelectronics", }, { - .type = "sd", .id = 0x74, .manufacturer = "Transcend", }, { - .type = "sd", .id = 0x76, .manufacturer = "Patriot", }, { - .type = "sd", .id = 0x82, .manufacturer = "Gobe/Sony", }, { - .type = "sd", .id = 0x89, .manufacturer = "Unknown", }, +}; + +static struct ids_database mmc_database[] = { { - .type = "mmc", .id = 0x00, .manufacturer = "SanDisk", }, { - .type = "mmc", .id = 0x02, .manufacturer = "Kingston/SanDisk", }, { - .type = "mmc", .id = 0x03, .manufacturer = "Toshiba", }, { - .type = "mmc", .id = 0x05, .manufacturer = "Unknown", }, { - .type = "mmc", .id = 0x06, .manufacturer = "Unknown", }, { - .type = "mmc", .id = 0x11, .manufacturer = "Toshiba", }, { - .type = "mmc", .id = 0x13, .manufacturer = "Micron", }, { - .type = "mmc", .id = 0x15, .manufacturer = "Samsung/SanDisk/LG", }, { - .type = "mmc", .id = 0x37, .manufacturer = "KingMax", }, { - .type = "mmc", .id = 0x44, .manufacturer = "ATP", }, { - .type = "mmc", .id = 0x45, .manufacturer = "SanDisk Corporation", }, { - .type = "mmc", .id = 0x2c, .manufacturer = "Kingston", }, { - .type = "mmc", .id = 0x70, .manufacturer = "Kingston", }, { - .type = "mmc", .id = 0xfe, .manufacturer = "Micron", }, }; + /* Command line parsing functions */ void usage(void) { @@ -310,47 +281,18 @@ int parse_opts(int argc, char **argv, struct config *config) return 0; } -int parse_ids(struct config *config) +static char *get_manufacturer(struct config *config, unsigned int manid) { - unsigned int ids_cnt = sizeof(database) / sizeof(struct ids_database); - unsigned int value; - char **ids; - char *type; + struct ids_database *db = config->bus == MMC ? mmc_database : sd_database; + unsigned int ids_cnt = ARRAY_SIZE(db); int i; for (i = 0; i < ids_cnt; i++) { - type = database[i].type; - - if (!strcmp(type, "mmc")) { - ids = config->mmc_ids; - } else if (!strcmp(type, "sd")) { - ids = config->sd_ids; - } else { - fprintf(stderr, - "MMC/SD id parse error, unknown type: '%s'.\n", - type); - return -1; - } - - value = database[i].id; - - if (value >= IDS_MAX) { - fprintf(stderr, - "MMC/SD id parse error, id out of range.\n"); - return -1; - } - - if (ids[value]) { - fprintf(stderr, - "Duplicate entries: type='%s', id='0x%1x'.\n", - type, value); - return -1; - } - - ids[value] = database[i].manufacturer; + if (db[i].id == manid) + return db[i].manufacturer; } - return 0; + return NULL; } /* MMC/SD file parsing functions */ @@ -538,6 +480,7 @@ void print_sd_cid(struct config *config, char *cid) unsigned int mdt_month; unsigned int mdt_year; unsigned int crc; + char *manufacturer = NULL; parse_bin(cid, "8u16a40a4u4u32u4r8u4u7u1r", &mid, &oid[0], &pnm[0], &prv_major, &prv_minor, &psn, @@ -546,12 +489,14 @@ void print_sd_cid(struct config *config, char *cid) oid[2] = '\0'; pnm[5] = '\0'; + manufacturer = get_manufacturer(config, mid); + if (config->verbose) { printf("======SD/CID======\n"); printf("\tMID: 0x%02x (", mid); - if (config->sd_ids[mid]) - printf("%s)\n", config->sd_ids[mid]); + if (manufacturer) + printf("%s)\n", manufacturer); else printf("Unlisted)\n"); @@ -564,9 +509,9 @@ void print_sd_cid(struct config *config, char *cid) 2000 + mdt_year, months[mdt_month]); printf("\tCRC: 0x%02x\n", crc); } else { - if (config->sd_ids[mid]) + if (manufacturer) printf("manufacturer: '%s' '%s'\n", - config->sd_ids[mid], oid); + manufacturer, oid); else printf("manufacturer: 'Unlisted' '%s'\n", oid); @@ -594,6 +539,7 @@ void print_mmc_cid(struct config *config, char *cid) unsigned int mdt_month; unsigned int mdt_year; unsigned int crc; + char *manufacturer = NULL; parse_bin(cid, "8u6r2u8u48a4u4u32u4u4u7u1r", &mid, &cbx, &oid, &pnm[0], &prv_major, &prv_minor, &psn, @@ -601,12 +547,14 @@ void print_mmc_cid(struct config *config, char *cid) pnm[6] = '\0'; + manufacturer = get_manufacturer(config, mid); + if (config->verbose) { printf("======MMC/CID======\n"); printf("\tMID: 0x%02x (", mid); - if (config->mmc_ids[mid]) - printf("%s)\n", config->mmc_ids[mid]); + if (manufacturer) + printf("%s)\n", manufacturer); else printf("Unlisted)\n"); @@ -635,9 +583,9 @@ void print_mmc_cid(struct config *config, char *cid) 1997 + mdt_year, months[mdt_month]); printf("\tCRC: 0x%02x\n", crc); } else { - if (config->mmc_ids[mid]) + if (manufacturer) printf("manufacturer: 0x%02x (%s) oid: 0x%01x\n", - mid, config->mmc_ids[mid], oid); + mid, manufacturer, oid); else printf("manufacturer: 0x%02x (Unlisted) oid: 0x%01x\n", mid, oid); @@ -2308,6 +2256,8 @@ int process_dir(struct config *config, enum REG_TYPE reg) goto err; } + config->bus = strcmp(type, "MMC") ? SD : MMC; + switch (reg) { case CID: cid = read_file("cid"); @@ -2369,45 +2319,19 @@ err: return ret; } -int lsmmc_main(struct config *config, int argc, char **argv) -{ - int ret; - - config->mmc_ids = calloc(IDS_MAX, sizeof(char *)); - config->sd_ids = calloc(IDS_MAX, sizeof(char *)); - if (!config->mmc_ids || !config->sd_ids) { - fprintf(stderr, "Could not allocate memory for lsmmc.\n"); - return -1; - } - - ret = parse_opts(argc, argv, config); - if (ret) - return ret; - - return parse_ids(config); -} - -void lsmmc_free(struct config *config) -{ - free(config->mmc_ids); - free(config->sd_ids); - free(config->dir); -} - static int do_read_reg(int argc, char **argv, enum REG_TYPE reg) { struct config cfg = {}; int ret; - ret = lsmmc_main(&cfg, argc, argv); + ret = parse_opts(argc, argv, &cfg); if (ret) - goto out; + return ret; if (cfg.dir) ret = process_dir(&cfg, reg); -out: - lsmmc_free(&cfg); + free(cfg.dir); return ret;
We used to have this odd way of printing the manufacturer name. To that end we cached the entire database beforehand which is completely useless. While at it, get rid of some more redundant code. Signed-off-by: Avri Altman <avri.altman@wdc.com> --- lsmmc.c | 150 ++++++++++++++------------------------------------------ 1 file changed, 37 insertions(+), 113 deletions(-)