Fix MAD manipulation for Mifare Classic 4K.

- Allocate large blocks on Mifare Classic 4K in unit tests;
  - Constraints sector number in mad_set_aid();
  - Fix location of AID storage in mad_set_aid() (wrong variable name and offset, ECOPYPASTETOOFAST);
  - New API function mad_sector_reserved();
  - Use mad_sector_reserved() to avoid trying to use reserved sectors.
This commit is contained in:
Romain Tartiere 2010-07-01 21:44:40 +00:00
parent 541292505a
commit f83918ee41
5 changed files with 36 additions and 4 deletions

View file

@ -137,6 +137,7 @@ MifareSectorNumber mad_get_card_publisher_sector (Mad mad);
int mad_set_card_publisher_sector (Mad mad, MifareSectorNumber cps);
int mad_get_aid (Mad mad, MifareSectorNumber sector, MadAid *aid);
int mad_set_aid (Mad mad, MifareSectorNumber sector, MadAid aid);
bool mad_sector_reserved (MifareSectorNumber sector);
void mad_free (Mad mad);
MifareSectorNumber *mifare_application_alloc (Mad mad, MadAid aid, size_t size);

View file

@ -391,7 +391,7 @@ mad_get_aid(Mad mad, MifareSectorNumber sector, MadAid *aid)
int
mad_set_aid(Mad mad, MifareSectorNumber sector, MadAid aid)
{
if (sector > 0x27) {
if ((sector < 1) || (sector == 0x10) || (sector > 0x27)) {
errno = EINVAL;
return -1;
}
@ -401,8 +401,8 @@ mad_set_aid(Mad mad, MifareSectorNumber sector, MadAid aid)
errno = EINVAL;
return -1;
}
mad->sector_0x00.aids[sector - 0x0f - 1].function_cluster_code = aid.function_cluster_code;
mad->sector_0x00.aids[sector - 0x0f - 1].application_code = aid.application_code;
mad->sector_0x10.aids[sector - 0x0f - 2].function_cluster_code = aid.function_cluster_code;
mad->sector_0x10.aids[sector - 0x0f - 2].application_code = aid.application_code;
} else {
mad->sector_0x00.aids[sector - 1].function_cluster_code = aid.function_cluster_code;
mad->sector_0x00.aids[sector - 1].application_code = aid.application_code;
@ -411,6 +411,12 @@ mad_set_aid(Mad mad, MifareSectorNumber sector, MadAid aid)
return 0;
}
bool
mad_sector_reserved (MifareSectorNumber sector)
{
return ((0x00 == sector) || (0x10 == sector));
}
/*
* Free memory allocated by mad_new() and mad_read().
*/

View file

@ -114,6 +114,8 @@ mifare_application_alloc (Mad mad, MadAid aid, size_t size)
sector = FIRST_SECTOR;
MifareSectorNumber s_max = (mad_get_version (mad) == 1) ? 15 : 31;
while ((s > 0) && (sector <= s_max)) {
if (mad_sector_reserved (sector))
continue;
mad_get_aid (mad, sector, &sector_aid);
if (0 == aidcmp (sector_aid, free_aid)) {
sector_map[sector] = 1;