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