2017-11-18 17:14:42 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2010-06-23 22:20:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 - Maxim Levitsky
|
|
|
|
*
|
|
|
|
* mtd_probe is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mtd_probe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with mtd_probe; if not, write to the Free Software
|
2012-01-05 22:41:45 +01:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
2010-06-23 22:20:29 +02:00
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <mtd/mtd-user.h>
|
|
|
|
#include <stdint.h>
|
2010-06-23 22:20:29 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-06-23 22:29:58 +02:00
|
|
|
#include <string.h>
|
2010-06-23 22:20:29 +02:00
|
|
|
#include <sys/stat.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <sys/types.h>
|
2010-06-23 22:20:29 +02:00
|
|
|
#include <unistd.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
|
2010-06-23 22:20:29 +02:00
|
|
|
#include "mtd_probe.h"
|
|
|
|
|
|
|
|
static const uint8_t cis_signature[] = {
|
2012-01-10 01:34:15 +01:00
|
|
|
0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
|
2010-06-23 22:20:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-06-23 22:29:58 +02:00
|
|
|
void probe_smart_media(int mtd_fd, mtd_info_t* info)
|
|
|
|
{
|
2012-04-04 05:18:14 +02:00
|
|
|
int sector_size;
|
|
|
|
int block_size;
|
|
|
|
int size_in_megs;
|
|
|
|
int spare_count;
|
2012-01-10 01:34:15 +01:00
|
|
|
char* cis_buffer = malloc(SM_SECTOR_SIZE);
|
2012-04-04 05:18:14 +02:00
|
|
|
int offset;
|
|
|
|
int cis_found = 0;
|
2010-06-23 22:29:58 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
if (!cis_buffer)
|
|
|
|
return;
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
if (info->type != MTD_NANDFLASH)
|
|
|
|
goto exit;
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2012-04-04 05:18:14 +02:00
|
|
|
sector_size = info->writesize;
|
|
|
|
block_size = info->erasesize;
|
|
|
|
size_in_megs = info->size / (1024 * 1024);
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2017-09-29 09:58:22 +02:00
|
|
|
if (!IN_SET(sector_size, SM_SECTOR_SIZE, SM_SMALL_PAGE))
|
2012-01-10 01:34:15 +01:00
|
|
|
goto exit;
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
switch(size_in_megs) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
spare_count = 6;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
spare_count = 12;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
spare_count = 24;
|
|
|
|
break;
|
|
|
|
}
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
for (offset = 0 ; offset < block_size * spare_count ;
|
|
|
|
offset += sector_size) {
|
|
|
|
lseek(mtd_fd, SEEK_SET, offset);
|
2016-02-23 18:52:52 +01:00
|
|
|
if (read(mtd_fd, cis_buffer, SM_SECTOR_SIZE) == SM_SECTOR_SIZE) {
|
2012-01-10 01:34:15 +01:00
|
|
|
cis_found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
if (!cis_found)
|
|
|
|
goto exit;
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
if (memcmp(cis_buffer, cis_signature, sizeof(cis_signature)) != 0 &&
|
|
|
|
(memcmp(cis_buffer + SM_SMALL_PAGE, cis_signature,
|
|
|
|
sizeof(cis_signature)) != 0))
|
|
|
|
goto exit;
|
2010-06-23 22:20:29 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
printf("MTD_FTL=smartmedia\n");
|
|
|
|
free(cis_buffer);
|
2017-12-22 13:24:40 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-06-23 22:20:29 +02:00
|
|
|
exit:
|
2012-01-10 01:34:15 +01:00
|
|
|
free(cis_buffer);
|
|
|
|
return;
|
2010-06-23 22:20:29 +02:00
|
|
|
}
|