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

1
NEWS
View file

@ -4,4 +4,5 @@ Changes between 0.1.0 and x.x.x [xx XXX xxxx]
list.
*) The mifare_application_alloc() size parameter is now expressed in bytes
and not in sectors.
*) New API function mad_sector_reserved().

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;

View file

@ -36,12 +36,34 @@ test_mifare_classic_application (void)
cut_assert_not_null (s_found, cut_message ("mifare_application_alloc() failed"));
for (int i = 0; i < 3; i++) {
cut_assert_equal_int (s_alloc[i], s_found[i], cut_message ("Allocated and found blocks don't match"));
cut_assert_equal_int (s_alloc[i], s_found[i], cut_message ("Allocated and found blocks don't match at position %d", i));
}
cut_assert_equal_int (0, s_alloc[3], cut_message ("Invalid size"));
cut_assert_equal_int (0, s_found[3], cut_message ("Invalid size"));
mifare_application_free (mad, aid);
free (s_alloc);
free (s_found);
s_found = mifare_application_find (mad, aid);
cut_assert_null (s_found, cut_message ("mifare_application_free() failed"));
s_alloc = mifare_application_alloc (mad, aid, 15*16 + 1*16 + 1);
cut_assert_not_null (s_alloc, cut_message ("mifare_application_alloc() failed"));
s_found = mifare_application_find (mad, aid);
cut_assert_not_null (s_found, cut_message ("mifare_application_alloc() failed"));
for (int i = 0; i < 3; i++) {
cut_assert_equal_int (s_alloc[i], s_found[i], cut_message ("Allocated and found blocks don't match at position %d", i));
}
cut_assert_equal_int (0, s_alloc[3], cut_message ("Invalid size"));
cut_assert_equal_int (0, s_found[3], cut_message ("Invalid size"));
mifare_application_free (mad, aid);
free (s_alloc);