| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* Copyright © 2010 - Maxim Levitsky |
| 4 |
* |
| 5 |
* mtd_probe is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* mtd_probe is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with mtd_probe; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 18 |
* Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <mtd/mtd-user.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#include "alloc-util.h" |
| 27 |
#include "log.h" |
| 28 |
#include "mtd_probe.h" |
| 29 |
|
| 30 |
static const uint8_t cis_signature[] = { |
| 31 |
0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20 |
| 32 |
}; |
| 33 |
|
| 34 |
int probe_smart_media(int mtd_fd, mtd_info_t* info) { |
| 35 |
int sector_size; |
| 36 |
int block_size; |
| 37 |
int size_in_megs; |
| 38 |
int spare_count; |
| 39 |
_cleanup_free_ uint8_t *cis_buffer = NULL; |
| 40 |
int offset; |
| 41 |
int cis_found = 0; |
| 42 |
|
| 43 |
cis_buffer = malloc(SM_SECTOR_SIZE); |
| 44 |
if (!cis_buffer) |
| 45 |
return log_oom(); |
| 46 |
|
| 47 |
if (info->type != MTD_NANDFLASH) |
| 48 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 49 |
"Not marked MTD_NANDFLASH."); |
| 50 |
|
| 51 |
sector_size = info->writesize; |
| 52 |
block_size = info->erasesize; |
| 53 |
size_in_megs = info->size / (1024 * 1024); |
| 54 |
|
| 55 |
if (!IN_SET(sector_size, SM_SECTOR_SIZE, SM_SMALL_PAGE)) |
| 56 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 57 |
"Unexpected sector size: %i", sector_size); |
| 58 |
|
| 59 |
switch (size_in_megs) { |
| 60 |
case 1: |
| 61 |
case 2: |
| 62 |
spare_count = 6; |
| 63 |
break; |
| 64 |
case 4: |
| 65 |
spare_count = 12; |
| 66 |
break; |
| 67 |
default: |
| 68 |
spare_count = 24; |
| 69 |
} |
| 70 |
|
| 71 |
for (offset = 0; offset < block_size * spare_count; offset += sector_size) { |
| 72 |
(void) lseek(mtd_fd, offset, SEEK_SET); |
| 73 |
|
| 74 |
if (read(mtd_fd, cis_buffer, SM_SECTOR_SIZE) == SM_SECTOR_SIZE) { |
| 75 |
cis_found = 1; |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (!cis_found) |
| 81 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 82 |
"CIS not found"); |
| 83 |
|
| 84 |
if (memcmp(cis_buffer, cis_signature, sizeof(cis_signature)) != 0 && |
| 85 |
memcmp(cis_buffer + SM_SMALL_PAGE, cis_signature, sizeof(cis_signature)) != 0) |
| 86 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 87 |
"CIS signature didn't match"); |
| 88 |
|
| 89 |
printf("MTD_FTL=smartmedia\n"); |
| 90 |
return 0; |
| 91 |
} |