source: svn/zas_dstar/hal/platform/dstdddev.c @ 22

Last change on this file since 22 was 22, checked in by phkim, 11 years ago
  1. phkim
  2. newcon3sk 를 kctv 로 브랜치 함
File size: 22.1 KB
Line 
1/****************************************************************************
2 *-Copyright (c) 2004 Digital Stream Technologies Inc.  All Rights Reserved.
3 *
4 * Module:      dstdddev.c
5 * Author:              Jun-ku Park, hwatk@dstreamtech.com
6 * Description: DST HAL [Device] Platform/Project Specific Source
7 *
8 * notes: hwatk20040609
9 *
10 *      hwatk/20041116, DHL_DEV_NFLASHSetMode() ÇÔ¼ö Ãß°¡.
11 *
12 * TO DO LIST
13 *
14 ***************************************************************************/
15
16#include "dsthalcommon.h"
17#include "dsthaldev.h"
18#include "dstdddev.h"
19
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <fcntl.h>
23#include <unistd.h>
24#include <sys/mount.h>
25#include <string.h>
26
27#include <sys/types.h>
28#include <unistd.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31
32#include <sys/ioctl.h> 
33#ifndef USE_CYGWIN
34#include <linux/version.h>
35#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 00))
36#   include <mtd/mtd-user.h>
37#else
38#   include <linux/mtd/mtd.h>
39#endif
40#include <sys/sysinfo.h>
41#endif
42#include <errno.h>
43
44#ifdef DMALLOC
45#include <dmalloc.h>
46#endif
47
48
49/******************************************************************************
50 * Global variable declaration
51 ******************************************************************************/
52int gddDevDbgLvl = 2;
53
54/******************************************************************************
55 * Imported variable declaration
56 ******************************************************************************/
57
58/******************************************************************************
59 * Imported function declaration
60 ******************************************************************************/
61
62/******************************************************************************
63 * Local definitions
64 ******************************************************************************/
65
66/******************************************************************************
67 * Local typedefs
68 ******************************************************************************/
69
70/******************************************************************************
71 * Local variables declaration
72 ******************************************************************************/
73static int NFLASH_WRITE_SIZE = NFLASH_WRITE_SIZE_NORMAL_NOR;
74static int NFLASH_WRITE_ALIGN = NFLASH_WRITE_ALIGN_NORMAL_NOR;
75static int NFLASH_ERASE_ALIGN = NFLASH_ERASE_ALIGN_NORMAL_NOR;
76
77/******************************************************************************
78 * Local function prototypes
79 ******************************************************************************/
80
81/****************************************************************************
82
83        NAND Flash DST Device Driver
84
85        DD_DEV_NFLASHInit
86                - To be done for DD_DEV_NFLASHInit
87                        . Initialize the bus for NFLASH.
88                        . Setup reserved area for NFLASH.
89        DD_DEV_NFLASHStart
90                - DO NOTHING
91        DD_DEV_NFLASHStop
92                - DO NOTHING
93        DD_DEV_NFLASHClose
94                - DO NOTHING
95        DD_DEV_NFLASHRead
96                - Read data from NAND Flash.
97        DD_DEV_NFLASHWrite
98                - Write data to NAND Flash.
99        DD_DEV_NFLASHErase
100                - ÇöÀç¿¡´Â Erase¸¦ ¼±Åÿµ¿ªÀ¸·Î ¼öÇàÇϰí ÀÖÀ¸³ª,
101                ¸¸¾à Sector (¶Ç´Â Erase Block) ´ÜÀ§·Î Erase¸¦ ¼öÇàÇØ¾ß ÇÑ´Ù¸é,
102                À̸¦ °í·ÁÇÏ¿© EraseµÇ¾î¾ß ÇÑ´Ù.
103       
104 ***************************************************************************/
105static int get_freemem(DS_U32 len)
106{
107        int retSize;
108#ifndef USE_CYGWIN
109        struct sysinfo info;
110       
111        if ( len == 0 ) {
112                sysinfo(&info);
113        } else {
114                info.freeram = len;
115        }
116               
117        if ( 64*1024 <= info.freeram )
118                retSize = 64*1024;
119        else if ( 32*1024 <= info.freeram)
120                retSize = 32*1024;
121        else if ( 16*1024 <= info.freeram )
122                retSize = 16*1024;
123        else if ( 8*1024 <= info.freeram )
124                retSize = 8*1024;
125        else if ( 4*1024 <= info.freeram )
126                retSize = 4*1024;
127        else if ( 2*1024 <= info.freeram )
128                retSize = 2*1024;
129        else
130                retSize = -1;
131
132//      printf("retSize = 0x%x, info.freeram = 0x%x, freehigh = 0x%x\n", retSize, info.freeram, info.freehigh );
133#else
134    retSize = 64*1024;
135#endif
136        return retSize;
137}
138
139static int NFlashInit(NFLASHID Id, DS_U32 size)
140{
141        int fd;
142        off_t pos;
143#if defined(USE_NFLASH_FILE)
144        DS_U8 buf[256];
145        int i, retval;
146        FILE *fp;
147       
148        //fd = open(NANDFLASH_FILE, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR|S_IXUSR);
149        fp = fopen(NANDFLASH_FILE, "r+b");
150        if ( !fp )
151        {
152            fp = fopen(NANDFLASH_FILE, "w+b");
153            if ( !fp )
154                return -1;
155        }
156       
157        fd = fileno(fp);
158
159        if ( fd < 0 ) return fd;
160       
161        pos = lseek( fd, 0, SEEK_SET );
162        if ( pos != 0L ) return -1;
163
164        for (i=0; i<256; i++)
165                buf[i] = 0xFF;
166
167        pos = lseek( fd, 0, SEEK_END );
168        if ( pos == 0L ) {
169                for (i=0; i<(size/256); i++)
170                {
171                        retval = write( fd, &buf[0], 256 );
172                        if ( retval != 256 ) return -1;
173                }
174        }
175#else
176        DS_U8 mtdname[256];
177
178        /* make mtd pathname */
179        strcpy( (char *)&mtdname[0], ( char *)"/dev/mtd" );
180#if 0
181        mtdname[strlen(mtdname)] = 0;
182        mtdname[strlen(mtdname)-1] = Id;
183
184#endif 
185        fd = open( (char *)mtdname, O_RDWR, S_IRUSR|S_IWUSR);
186        if ( fd < 0 ) {
187                DHL_DbgPrintf( 0, DHLDBG_DEV, "ERROR, LINE=%d\n", __LINE__);
188                return fd;
189        }
190        pos = lseek( fd, 0, SEEK_SET );
191        if ( pos != 0L ) {
192                DHL_DbgPrintf( 0, DHLDBG_DEV, "ERROR, LINE=%d\n", __LINE__);
193                return -1;
194        }
195#endif
196
197        return fd;     
198}
199
200static int NFlashRead(int NFlashHandle, DS_U8 *pBuffer, DS_U32 offset, DS_U32 len)
201{
202        int RetVal = len;
203        int totalLen=0, remainLen, readSize;
204        off_t pos;
205        /* Argument Checking */
206        if ( NFlashHandle < 0 ) {
207                DHL_DbgPrintf(1,DHLDBG_DEV,"Invalid Handle!\n");
208                return -1;
209        }
210       
211        /* Set Position */
212        pos = lseek( NFlashHandle, offset, SEEK_SET );
213        if ( pos != offset ) {
214                DHL_DbgPrintf(1,DHLDBG_DEV,"lseek()\n");
215                return -1;     
216        }
217       
218        /* Read from file */
219        readSize = get_freemem(0);
220       
221        if ( readSize < 0 ) {
222                DHL_DbgPrintf(0,DHLDBG_DEV," Out of Memory\n");
223                return -1;
224        }
225       
226        if ( readSize >= len ) {
227                RetVal = read( NFlashHandle, pBuffer, len );
228                totalLen = RetVal;
229                if ( RetVal <= 0 ) {
230                        perror("read from nand flash");
231                        DHL_DbgPrintf(1,DHLDBG_DEV,"read()\n");
232                        return -1;
233                }
234        } else {
235                remainLen = len;
236                totalLen = 0;
237                do {
238                        if ( remainLen < readSize ) {
239                                RetVal = read( NFlashHandle, &pBuffer[totalLen], remainLen );
240                        } else {
241                                RetVal = read( NFlashHandle, &pBuffer[totalLen], readSize );
242                        }
243                        if ( RetVal <= 0 ) {
244                                if ( errno == ENOMEM ) {
245                                        readSize = get_freemem( readSize - 1 );
246                                        if ( readSize < 0 ) {
247                                                DHL_DbgPrintf(0,DHLDBG_DEV," Out of Memory\n");
248                                                return -1;
249                                        }
250                                } else {
251                                        perror("read from nand flash");
252                                        get_freemem(0);
253                                        return -1;
254                                }
255                        } else {
256                                totalLen += RetVal;
257                                remainLen -= RetVal;
258                        }
259                } while ( totalLen < len );
260        }               
261       
262        return totalLen;
263}
264
265#if (!defined(USE_CYGWIN) && !defined(USE_NFLASH_FILE))
266static struct mtd_info_user mtd;
267#endif
268static DS_U8 writeBuffer[64*1024];
269static int NFlashWrite(int NFlashHandle, DS_U8 *pBuffer, DS_U32 offset, DS_U32 len)
270{
271        int retVal = len;
272        int totalLen, remainLen;
273        off_t pos;
274
275        /* Argument Checking */
276        if ( NFlashHandle < 0 ) return -1;
277       
278        /* Set Position */
279        pos = lseek( NFlashHandle, offset, SEEK_SET );
280        if ( pos != offset ) {
281                DHL_DbgPrintf(1,DHLDBG_DEV,"lseek()\n");
282                return -1;     
283        }
284       
285        remainLen = len;
286        totalLen = 0;
287        do {
288                /*
289                 * ½á ³Ö¾î¾ß ÇÒ ¹ÙÀÌÆ®°¡ NFLASH_WRITE_SIZEÀÌÇÏÀΰ¡?
290                 */
291                if ( remainLen < NFLASH_WRITE_SIZE ) {
292                        /* ÀÌÇÏÀ̸é, ¹öÆÛ¸¦ »ý¼ºÇÏ¿© NFLASH_WRITE_SIZE Writing */
293                        memset( &writeBuffer[0], 0xFF, NFLASH_WRITE_SIZE );
294                        memcpy( &writeBuffer[0], &pBuffer[totalLen], remainLen );
295                        retVal = write( NFlashHandle, &writeBuffer[0], NFLASH_WRITE_SIZE );
296                        totalLen += remainLen;
297                        remainLen = 0;
298                } else {
299                        /* ÀÌ»óÀ̸é, ´Ü¼øÈ÷ NFLASH_WRITE_SIZE Writing */
300                        retVal = write( NFlashHandle, &pBuffer[totalLen], NFLASH_WRITE_SIZE );
301                        totalLen += NFLASH_WRITE_SIZE;
302                        remainLen -= NFLASH_WRITE_SIZE;
303                }
304                if ( retVal <= 0 ) {
305                        perror("write()");
306                        DHL_DbgPrintf(1,DHLDBG_DEV," offset=0x%lx, RetVal = %d, TotalLen = 0x%x\n", pos, retVal, totalLen);
307                        return -1;
308                }
309        } while ( totalLen < len );
310       
311        return totalLen;
312}
313
314#if defined(USE_NFLASH_FILE)
315        static DS_U8 erasebuffer[256];
316#endif
317char ebuf[64*1024] = {0};   // hwatk/070628, moved from local variable. (Display flickering problem on flash erase.)
318static int NFlashErase(int NFlashHandle, DS_U32 offset, DS_U32 len)
319{
320#if defined(USE_NFLASH_FILE)
321        int RetVal = len, RemainLen, EraseLen;
322        off_t pos;
323        int i;
324#else
325        int i;
326        int RemainLen = 0;
327        int EraseLen =0;       
328        struct erase_info_user erase;
329#endif
330
331        /* Argument Checking */
332        if ( NFlashHandle < 0 ) return -1;
333
334#if defined(USE_NFLASH_FILE)
335        /* Set Position */
336        pos = lseek( NFlashHandle, offset, SEEK_SET );
337        if ( pos != offset ) {
338                DHL_DbgPrintf(1,DHLDBG_DEV,"lseek()\n");
339                return -1;     
340        }       
341       
342        for (i=0; i<256; i++)
343                erasebuffer[i] = 0xFF;
344               
345        RemainLen = len;
346        while ( RemainLen )
347        {
348                if ( len > 256 )
349                        EraseLen = 256;
350                else
351                        EraseLen = len;
352                       
353                /* Read from file */
354                RetVal = write( NFlashHandle, &erasebuffer[0], EraseLen );
355                if ( RetVal == 0 ) {
356                        DHL_DbgPrintf(1,DHLDBG_DEV,"write()\n");
357                        return -1;
358                }
359                RemainLen -= EraseLen;
360        }
361        return (len - RemainLen);
362#else
363        /* set space. */
364        if(mtd.type == 0x03){
365        erase.start = offset;
366        erase.length = len;
367       
368        if ( ioctl(NFlashHandle, MEMERASE, &erase) < 0 ) 
369                return -1;
370       
371        return len;
372        }else{
373                        /* set space. */
374                EraseLen = 0x10000;
375                RemainLen = len;
376       
377                erase.start = offset;
378                erase.length = EraseLen;
379               
380                while ( RemainLen )
381                {
382       
383                        /* Read from file */
384        //      RetVal = write( NFlashHandle, &erasebuffer[0], EraseLen );
385        #if 1
386                if ( ioctl(NFlashHandle, MEMUNLOCK, &erase) < 0 ) {
387                        printf("|%s:%d| ERROR. offset=0x%08lX, len=0x%08lX\n", __FUNCTION__, __LINE__, (unsigned long int)offset, (unsigned long int)len);
388                        perror("ioctl()\n");
389                        return -1;
390                }       
391        #endif 
392                        if ( ioctl(NFlashHandle, MEMERASE, &erase) < 0 ) {
393                                printf("|%s:%d| ERROR. offset=0x%08lX, len=0x%08lX\n", __FUNCTION__, __LINE__, (unsigned long int)offset, (unsigned long int)len);
394                        return -1;
395                        }       
396       
397                        RemainLen -= EraseLen;
398                        if(RemainLen > 0){
399                                erase.start = offset + EraseLen;
400                                erase.length = EraseLen;
401                        }else{
402                                RemainLen = 0;
403                                break;
404                        }
405                }
406        //      static int NFlashWrite(int NFlashHandle, DS_U8 *pBuffer, DS_U32 offset, DS_U32 len)
407
408                for(i=0;i<EraseLen;i++){
409                        ebuf[i] = 0xff;
410                }
411               
412                RemainLen = len;
413               
414                while(RemainLen){
415                        i = NFlashWrite(NFlashHandle,(unsigned char *)ebuf,offset,EraseLen);
416                        if(i != EraseLen){
417                                printf("|%s:%d| ERROR. offset=0x%08lX, len=0x%08lX\n", __FUNCTION__, __LINE__, (unsigned long int)offset, (unsigned long int)len);
418                                return i;
419                        }
420                        RemainLen -= EraseLen;
421                       
422                        if(RemainLen <= 0){
423                                RemainLen = 0;
424                                break;
425                        }
426                }
427                return (len - RemainLen);
428        }
429#endif
430}
431
432static int NFlashInfo(P_DST_DEV_NFLASH pNFLASH, DS_U32 *Size, DS_U32 *Start, DS_U32 *End)
433{
434        DS_U32 sz=0, off=0, end=0;
435       
436#if (!defined(USE_CYGWIN) && !defined(USE_NFLASH_FILE))
437        int err;
438
439        err = ioctl (pNFLASH->nflash_fd,MEMGETINFO,&mtd);
440        if (err) {
441                DHL_DbgPrintf( 0, DHLDBG_DEV, "ERROR: Line=%d\n", __LINE__);
442                return (err);
443        }
444       
445        if(mtd.type == 0x4)
446        {
447                NFLASH_WRITE_SIZE = NFLASH_WRITE_SIZE_NORMAL_NAND;
448                NFLASH_WRITE_ALIGN = NFLASH_WRITE_ALIGN_NORMAL_NAND;
449                NFLASH_ERASE_ALIGN = NFLASH_ERASE_ALIGN_NORMAL_NAND;
450        }
451        else if(mtd.type == 0x3)
452    {
453                NFLASH_WRITE_SIZE = NFLASH_WRITE_SIZE_NORMAL_NOR;
454                NFLASH_WRITE_ALIGN = NFLASH_WRITE_ALIGN_NORMAL_NOR;
455                NFLASH_ERASE_ALIGN = NFLASH_ERASE_ALIGN_NORMAL_NOR;
456        }               
457        sz = mtd.size;
458        off = 0;
459        end = sz;
460#else
461    sz = 0x100000;
462    off = 0;   
463    end = sz;
464#endif
465
466        *Size = sz;
467        *Start = off;
468        *End = end;
469        DHL_DbgPrintf( 0, DHLDBG_DEV, "Flash%d: Start = 0x%08lX, End = 0x%08lX, Size = 0x%08lX\n", pNFLASH->NFlashId-1, off, end, sz);
470
471        return 0;       
472}
473
474static int nflash_fd;
475DHL_RESULT DD_DEV_NFLASHInit( P_DST_DEV_NFLASH pNFLASH, DS_U32 Size, DS_U32 Start, DS_U32 End )
476{
477        DHL_RESULT dhlResult = DHL_OK;
478        DS_U32 offset, end, sz;
479       
480
481        if ( NFLASH_WRITE_SIZE == 0 )
482                NFLASH_WRITE_SIZE = NFLASH_WRITE_SIZE_NORMAL_NOR;
483        if ( NFLASH_WRITE_ALIGN == 0 )
484                NFLASH_WRITE_ALIGN = NFLASH_WRITE_ALIGN_NORMAL_NOR;
485        if ( NFLASH_ERASE_ALIGN == 0 )
486                NFLASH_ERASE_ALIGN = NFLASH_ERASE_ALIGN_NORMAL_NOR;
487
488//#if defined(USE_NFLASH_FILE)  hwatk, 2004.06.30
489        pNFLASH->nflash_fd = NFlashInit(pNFLASH->NFlashId, Size);
490        if ( pNFLASH->nflash_fd < 0 )
491                return DHL_FAIL;
492       
493        nflash_fd = pNFLASH->nflash_fd;
494//#endif
495
496        if ( Size == 0 ) {
497                NFlashInfo( pNFLASH, &sz, &offset, &end );
498                pNFLASH->uSize = sz;
499                pNFLASH->uAvlStart = offset;
500                pNFLASH->uAvlEnd = end;
501        } else {
502                NFlashInfo( pNFLASH, &sz, &offset, &end );
503                pNFLASH->uSize = Size;
504                pNFLASH->uAvlStart = Start;
505                pNFLASH->uAvlEnd = End;
506        }
507
508        return dhlResult;       
509}
510
511DHL_RESULT DD_DEV_NFLASHStart( P_DST_DEV_NFLASH pNFLASH )
512{
513        DHL_RESULT dhlResult = DHL_OK;
514       
515        /* DO NOTHING */
516       
517        return dhlResult;       
518}
519
520DHL_RESULT DD_DEV_NFLASHStop( P_DST_DEV_NFLASH pNFLASH )
521{
522        DHL_RESULT dhlResult = DHL_OK;
523       
524        /* DO NOTHING */
525       
526        return dhlResult;       
527}
528
529DHL_RESULT DD_DEV_NFLASHClose(P_DST_DEV_NFLASH pNFLASH )
530{
531        DHL_RESULT dhlResult = DHL_OK;
532       
533        /* DO NOTHING */
534       
535        return dhlResult;       
536}
537
538int DD_DEV_NFLASHRead(P_DST_DEV_NFLASH pNFLASH, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len )
539{
540        int RetVal = Len;
541        RetVal = NFlashRead( pNFLASH->nflash_fd, pBuffer, Address, Len );
542       
543        return RetVal;
544}
545
546int DD_DEV_NFLASHWrite(P_DST_DEV_NFLASH pNFLASH, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len )
547{
548        int RetVal = Len;
549//      if ( (Len & NFLASH_WRITE_ALIGN) || (Address & NFLASH_WRITE_ALIGN) ) {
550        if ( (Address & NFLASH_WRITE_ALIGN) ) {
551                return -1;
552        }
553        RetVal = NFlashWrite( pNFLASH->nflash_fd, pBuffer, Address, Len );
554               
555        return RetVal;
556}
557
558DHL_RESULT DD_DEV_NFLASHErase( P_DST_DEV_NFLASH pNFLASH, DS_U32 Address, DS_U32 Len )
559{
560        DHL_RESULT dhlResult = DHL_OK;
561        int RetVal = Len;
562
563        if ( Len & NFLASH_ERASE_ALIGN ) {
564                return DHL_DEV_FAIL_NOTALIGN;
565        }
566       
567        RetVal = NFlashErase( pNFLASH->nflash_fd, Address, Len );
568        if ( RetVal <= 0 ) dhlResult = DHL_FAIL;
569
570        return dhlResult;
571}
572
573
574/****************************************************************************
575
576        Serial Flash DST Device Driver
577
578        DD_DEV_SFLASHInit
579                - To be done for DD_DEV_SFLASHInit
580                        . Initialize the bus for SFLASH.
581                        . Setup reserved area for SFLASH.
582        DD_DEV_SFLASHStart
583                - DO NOTHING
584        DD_DEV_SFLASHStop
585                - DO NOTHING
586        DD_DEV_SFLASHClose
587                - DO NOTHING
588        DD_DEV_SFLASHRead
589                - Read data from NAND Flash.
590        DD_DEV_SFLASHWrite
591                - Write data to NAND Flash.
592        DD_DEV_SFLASHErase
593                - ÇöÀç¿¡´Â Erase¸¦ ¼±Åÿµ¿ªÀ¸·Î ¼öÇàÇϰí ÀÖÀ¸³ª,
594                ¸¸¾à Sector (¶Ç´Â Erase Block) ´ÜÀ§·Î Erase¸¦ ¼öÇàÇØ¾ß ÇÑ´Ù¸é,
595                À̸¦ °í·ÁÇÏ¿© EraseµÇ¾î¾ß ÇÑ´Ù.
596
597 ***************************************************************************/
598#if defined(USE_SFLASH_FILE)
599static int SFLASHInit(DS_U32 size)
600{
601        int fd;
602        off_t pos;
603        DS_U8 buf[256];
604        int i, retval;
605       
606        fd = open(SERIALFLASH_FILE, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR|S_IXUSR);
607        if ( fd < 0 ) return fd;
608       
609        pos = lseek( fd, 0, SEEK_SET );
610        if ( pos != 0L ) return -1;
611
612        for (i=0; i<256; i++)
613                buf[i] = 0xFF;
614
615        pos = lseek( fd, 0, SEEK_END );
616        if ( pos == 0L ) {
617                for (i=0; i<(size/256); i++)
618                {
619                        retval = write( fd, &buf[0], 256 );
620                        if ( retval != 256 ) return -1;
621                }
622        }
623       
624        return fd;     
625}
626
627static int SFLASHRead(int SFLASHHandle, DS_U8 *pBuffer, DS_U32 offset, DS_U32 len)
628{
629        int RetVal = len;
630        off_t pos;
631       
632        /* Argument Checking */
633        if ( SFLASHHandle < 0 ) return -1;
634       
635        /* Set Position */
636        pos = lseek( SFLASHHandle, offset, SEEK_SET );
637        if ( pos != offset ) {
638                DHL_DbgPrintf(1,DHLDBG_DEV,"lseek()\n");
639                return -1;     
640        }
641       
642        /* Read from file */
643        RetVal = read( SFLASHHandle, pBuffer, len );
644        if ( RetVal == 0 ) {
645                DHL_DbgPrintf(1,DHLDBG_DEV,"read()\n");
646                return -1;
647        }
648       
649        return RetVal; 
650}
651
652static int SFLASHWrite(int SFLASHHandle, DS_U8 *pBuffer, DS_U32 offset, DS_U32 len)
653{
654        int RetVal = len;
655        off_t pos;
656
657        /* Argument Checking */
658        if ( SFLASHHandle < 0 ) return -1;
659       
660        /* Set Position */
661        pos = lseek( SFLASHHandle, offset, SEEK_SET );
662        if ( pos != offset ) {
663                DHL_DbgPrintf(1,DHLDBG_DEV,"lseek()\n");
664                return -1;     
665        }       
666       
667        /* Write to file */
668        RetVal = write( SFLASHHandle, pBuffer, len );
669        if ( RetVal == 0 ) {
670                DHL_DbgPrintf(1,DHLDBG_DEV,"write()\n");
671                return -1;
672        }
673       
674        return RetVal;
675}
676
677static DS_U8 erasebuffer[256];
678static int SFLASHErase(int SFLASHHandle, DS_U32 offset, DS_U32 len)
679{
680        int RetVal = len, RemainLen, EraseLen;
681        off_t pos;
682        int i;
683
684        /* Argument Checking */
685        if ( SFLASHHandle < 0 ) return -1;
686       
687        /* Set Position */
688        pos = lseek( SFLASHHandle, offset, SEEK_SET );
689        if ( pos != offset ) {
690                DHL_DbgPrintf(1,DHLDBG_DEV,"lseek()\n");
691                return -1;     
692        }       
693       
694        for (i=0; i<256; i++)
695                erasebuffer[i] = 0xFF;
696               
697        RemainLen = len;
698        while ( RemainLen )
699        {
700                if ( len > 256 )
701                        EraseLen = 256;
702                else
703                        EraseLen = len;
704                       
705                /* Read from file */
706                RetVal = write( SFLASHHandle, &erasebuffer[0], EraseLen );
707                if ( RetVal == 0 ) {
708                        DHL_DbgPrintf(1,DHLDBG_DEV,"write()\n");
709                        return -1;
710                }
711                RemainLen -= EraseLen;
712        }
713       
714        return (len - RemainLen);
715}
716
717#endif
718
719DHL_RESULT DD_DEV_SFLASHInit( P_DST_DEV_SFLASH pSFLASH, DS_U32 Size, DS_U32 Start, DS_U32 End )
720{
721        DHL_RESULT dhlResult = DHL_OK;
722       
723        pSFLASH->uSize = Size;
724        pSFLASH->uAvlStart = Start;
725        pSFLASH->uAvlEnd = End;
726
727#if defined(USE_SFLASH_FILE)
728        pSFLASH->sflash_fd = SFLASHInit(Size);
729        if ( pSFLASH->sflash_fd < 0 )
730                return DHL_FAIL;
731#endif
732
733        return dhlResult;       
734}
735
736DHL_RESULT DD_DEV_SFLASHStart( P_DST_DEV_SFLASH pSFLASH )
737{
738        DHL_RESULT dhlResult = DHL_OK;
739       
740        /* DO NOTHING */
741       
742        return dhlResult;       
743}
744
745DHL_RESULT DD_DEV_SFLASHStop( P_DST_DEV_SFLASH pSFLASH )
746{
747        DHL_RESULT dhlResult = DHL_OK;
748       
749        /* DO NOTHING */
750       
751        return dhlResult;       
752}
753
754DHL_RESULT DD_DEV_SFLASHClose(P_DST_DEV_SFLASH pSFLASH )
755{
756        DHL_RESULT dhlResult = DHL_OK;
757       
758        /* DO NOTHING */
759       
760        return dhlResult;       
761}
762
763int DD_DEV_SFLASHRead(P_DST_DEV_SFLASH pSFLASH, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len )
764{
765        int RetVal = Len;
766
767#if defined(USE_SFLASH_FILE)
768        RetVal = SFLASHRead( pSFLASH->sflash_fd, pBuffer, Address, Len );
769#endif
770       
771        return RetVal;
772}
773
774int DD_DEV_SFLASHWrite(P_DST_DEV_SFLASH pSFLASH, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len )
775{
776        int RetVal = Len;
777       
778#if defined(USE_SFLASH_FILE)
779        RetVal = SFLASHWrite( pSFLASH->sflash_fd, pBuffer, Address, Len );
780#endif
781               
782        return RetVal;
783}
784
785DHL_RESULT DD_DEV_SFLASHErase( P_DST_DEV_SFLASH pSFLASH, DS_U32 Address, DS_U32 Len )
786{
787        DHL_RESULT dhlResult = DHL_OK;
788        int RetVal = Len;
789       
790#if defined(USE_SFLASH_FILE)
791        RetVal = SFLASHErase( pSFLASH->sflash_fd, Address, Len );
792        if ( RetVal <= 0 ) dhlResult = DHL_FAIL;
793#endif
794
795        return dhlResult;
796}
797
798
799#if 0
800___CAS_Functions___()
801#endif
802#define FRM_CMD_CLASS                                   0x90
803
804/* Initial Settings */
805#define FRM_INS_CMD_INT                         0x30
806
807/* Basic Processing */
808#define FRM_INS_CMD_ECM                         0x34
809#define FRM_INS_CMD_EMM                         0x36
810
811/* Display Card ID */
812#define FRM_INS_CMD_IDI                         0x32
813
814#define FRM_INS_CMD_CLA                         0x32
815
816#define SCI_MAX                                                 0x3F
817
818DHL_RESULT DD_DEV_InitSmartCard(void)
819{
820        DHL_RESULT dhlResult = DHL_OK;
821        return dhlResult;
822}
823
824
825DHL_RESULT DD_DEV_GetCARDStatus(DHL_DEV_CARD_STATUS *Status)
826{
827        DHL_RESULT dhlResult = DHL_OK;
828        *Status = DHL_DEV_CARD_ACTIVATED;
829        return dhlResult;       
830}
831
832DS_S32 DD_DEV_WriteCARDBlock( DS_U8 *data )
833{
834        return 0;       
835}
836
837DS_S32 DD_DEV_ReadCARDBlock( DS_U8 PCB, DS_U8 *data )
838{
839        return 0;               
840}
841
842DHL_RESULT DD_CA_InitMULTI2(DS_U8 *SystemKey, DS_U8 *CBC)
843{
844        DHL_RESULT dhlResult = DHL_OK;
845        return dhlResult;                       
846}
847DHL_RESULT DD_CA_ResetMULTI2(void)
848{
849        DHL_RESULT dhlResult = DHL_OK;
850        return dhlResult;               
851}
852
853DHL_RESULT DD_CA_SetMULTI2Key(DS_U8 Slot, DS_U8 *EvenData, DS_U8 *OddData)
854{
855        DHL_RESULT dhlResult = DHL_OK;
856        return dhlResult;                       
857}
858
859DHL_RESULT DD_CA_SetMULTI2Pid(DS_U16 Pid, DS_U32 *slot, DS_BOOL enable)
860{
861        DHL_RESULT dhlResult = DHL_OK;
862        return dhlResult;                               
863}
864
865
866
867
868#if 0
869___DEBUG_Functions___()
870#endif
871
872
873int f_read(DS_U32 offset, DS_U32 len)
874{
875        DS_U8 buf[128*1024] = {0};
876        int retVal = 0;
877        off_t pos;
878        int i;
879       
880        for(i=0;i<len;i++){
881                buf[i] = 0;
882        }
883               
884        pos = lseek( nflash_fd, offset, SEEK_SET );
885       
886        retVal = read( nflash_fd, buf, len );
887       
888        printf("retVal is %d\n",retVal);
889       
890        return len;
891}
892
893int f_write(DS_U32 offset, DS_U32 len, char data)
894{
895        DS_U8 buf[64*1024] = {0};
896        int retVal = 0;
897        off_t pos;
898        int i;
899       
900        for(i=0;i<len;i++){
901                buf[i] = data;
902        }
903               
904        pos = lseek( nflash_fd, offset, SEEK_SET );
905       
906        retVal = write( nflash_fd, buf,len );   
907       
908        printf("retVal is %d\n",retVal);
909
910        return len;
911}
912
913#if 0
914int f_erase(DS_U32 offset, DS_U32 len)
915{
916        int i;
917        struct erase_info_user erase;
918
919        /* set space. */
920        erase.start = offset;
921        erase.length = len;
922
923        if ( ioctl(nflash_fd, MEMUNLOCK, &erase) < 0 ) {
924                printf("|%s:%d| ERROR. offset=0x%08lX, len=0x%08lX\n", __FUNCTION__, __LINE__, (unsigned long int)offset, (unsigned long int)len);
925                perror("ioctl()\n");
926                return -1;
927        }       
928               
929        printf("MEMGETINFO       =0x%08lX\n", (unsigned long int)MEMGETINFO       );
930        printf("MEMERASE         =0x%08lX\n", (unsigned long int)MEMERASE         );
931        //printf("MEMWRITEOOB      =0x%08lX\n", (unsigned long int)MEMWRITEOOB      );
932        //printf("MEMREADOOB       =0x%08lX\n", (unsigned long int)MEMREADOOB       );
933        printf("MEMLOCK          =0x%08lX\n", (unsigned long int)MEMLOCK          );
934        printf("MEMUNLOCK        =0x%08lX\n", (unsigned long int)MEMUNLOCK        );
935        printf("MEMGETREGIONCOUNT=0x%08lX\n", (unsigned long int)MEMGETREGIONCOUNT);
936        printf("MEMGETREGIONINFO =0x%08lX\n", (unsigned long int)MEMGETREGIONINFO );
937        //printf("MEMSETOOBSEL     =0x%08lX\n", (DS_U32)MEMSETOOBSEL     );
938        //printf("MEMGETOOBSEL     =0x%08lX\n", (DS_U32)MEMGETOOBSEL     );
939        //printf("MEMGETBADBLOCK   =0x%08lX\n", (DS_U32)MEMGETBADBLOCK   );
940        //printf("MEMSETBADBLOCK   =0x%08lX\n", (DS_U32)MEMSETBADBLOCK   );
941
942        if ( ioctl(nflash_fd, MEMERASE, &erase) < 0 ) {
943                printf("|%s:%d| ERROR. offset=0x%08lX, len=0x%08lX\n", __FUNCTION__, __LINE__, (unsigned long int)offset, (unsigned long int)len);
944                perror("ioctl()\n");
945                return -1;
946        }       
947       
948        i = f_write(offset,len,0xff);
949        printf("return %d\n",i);
950       
951        return len;
952}
953#endif
954
955//int f_erase(DS_U32 offset, DS_U32 len)
956int f_erase_test(DS_U32 offset, DS_U32 len)
957{
958        int i = 0;
959        i = NFlashErase(nflash_fd, offset, len);
960        printf("i : %d\n", i);
961        return i;
962}
963
964//inf f_read(DS_U32 offset, DS_U32 len)
965int f_read_test(DS_U32 offset, DS_U32 len)
966{
967        int c = 0;
968        int i = 0;
969        DS_U8 buf[128*1024] = {0};
970        c = NFlashRead(nflash_fd, buf, offset, len);
971        printf("c : %d\n", c);
972       
973        for(i=0;i<len;i++){
974                printf("0x%x",buf[i]);
975        }       
976        return i;
977}
978
979int f_write_test(DS_U32 offset, DS_U32 len,char data)
980{
981        int c = 0;
982        int i = 0;
983        DS_U8 buf[128*1024] = {0};
984
985        printf("data is %x\n",data);   
986        for(i=0;i<len;i++){
987                buf[i] = data;
988        }
989//static int NFlashWrite(int NFlashHandle, DS_U8 *pBuffer, DS_U32 offset, DS_U32 len)   
990        c = NFlashWrite(nflash_fd, buf, offset, len);
991       
992        printf("c : %d\n", c);
993       
994        return i;
995}
Note: See TracBrowser for help on using the repository browser.