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