1 #define DEBUG_OW_FILE 0
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
22 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 // OTHER DEALINGS IN THE SOFTWARE.
26 // Except as contained in this notice, the name of Dallas Semiconductor
27 // shall not be used except as stated in the Dallas Semiconductor
29 //---------------------------------------------------------------------------
31 // owFile.C: Rudimentary level functions for reading and writing TMEX
32 // files on NVRAM iButton using the packet level functions.
37 // 1.02 -> 1.03 Removed caps in #includes for Linux capatibility
38 // 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
44 //--------------------------------------------------------------------------
45 // Read a TMEX file return it in the provided buffer.
47 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
48 // OpenCOM to indicate the port number.
49 // 'filename' - pointer to five byte filename to read where the
50 // the first four bytes are the name and the fifth is
51 // the extension (0 to 101 decimal).
52 // 'buf' - pointer to a buffer to place the file information into.
53 // This may need to be as large as 7084 bytes.
55 // Supported devices: All devices supported by owReadPacketStd
57 // Returns: >=0 success, number of bytes in the buffer
58 // <0 failed to read the file (error code)
60 int owReadFile(int portnum, uchar *filename, uchar *buf)
62 uchar dirpg=0,pgbuf[32],filepg=0;
67 printf ("owReadFile: %s\n", filename);
69 // loop read directory pages until the file entry is found
72 // read a directory page
73 pglen = owReadPacketStd(portnum,TRUE,dirpg,pgbuf);
75 // check for reading error
79 // if this is the first page make sure this is a directory
82 ((pgbuf[0] != 0xAA) || (pgbuf[1] != 0) || (pglen < 7)))
87 // loop through each file entry in directory page (page 0 exception)
88 for (i = (dirpg == 0) ? 7 : 0; i < 28; i += 7)
91 if ((filename[0] == pgbuf[i]) &&
92 (filename[1] == pgbuf[i+1]) &&
93 (filename[2] == pgbuf[i+2]) &&
94 (filename[3] == pgbuf[i+3]) &&
95 (filename[4] == (pgbuf[i+4] & 0x7F)) )
97 // get the file starting page number
100 printf ("owReadFile: file %s starts at %d\n", filename, filepg);
106 // get the next directory page (from page pointer)
107 dirpg = pgbuf[pglen-1];
109 while (dirpg && (filepg == 0));
111 // check if file found
115 // loop to read the file pages
119 pglen = owReadPacketStd(portnum,TRUE,filepg,pgbuf);
121 // check result of read
126 // append the page data to the buffer
127 for (i = 0; i < (pglen - 1); i++)
128 buf[bufcnt++] = pgbuf[i];
130 // get the next file page (from page pointer)
132 filepg = pgbuf[pglen-1];
136 // return the number of data bytes read
141 //--------------------------------------------------------------------------
142 // Format and Write a TMEX file.
143 // Any previous files will be removed in the Format operation.
144 // The file length 'fllen' can be up to:
145 // 420 bytes for a DS1993
146 // 1736 bytes for a DS1995
147 // 7084 bytes for a DS1996.
149 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
150 // OpenCOM to indicate the port number.
151 // 'filename' - pointer to five byte filename to write where the
152 // the first four bytes are the name and the fifth is
153 // the extension (0 to 101 decimal).
154 // 'buf' - pointer to a buffer containing the file information to write.
156 // Supported devices: DS1993, DS1994, DS1995, DS1996
158 // Returns: TRUE(1) success, device formated and file written
159 // <0 failed to read the file (error code)
162 int owFormatWriteFile(int portnum, uchar *filename, int fllen, uchar *buf)
164 uchar dummydir[] = { 0xAA, 0, 0x80, 0x01, 0, 0, 0, 0 },
165 newdir[] = { 0xAA, 0, 0x80, 0x01, 0, 0, 0, ' ', ' ', ' ', ' ', 0, 1, 1, 0 },
166 bmpg1[] = { 0x03,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x02 },
167 bmpg2[] = { 0,0,0,0,0 }, pgbuf[32];
168 int i,numdirpgs,flpg,bmpg1len,bmpg2len,cntleft,pos,numpgs;
171 printf ("owFormatWriteFile: %s %d\n", filename, fllen);
173 // calculate the number of pages needed to write the file
174 numpgs = (fllen / 28) + ((fllen % 28) ? 1 : 0);
176 // put the file in the newdirectory
177 for(i = 0; i < 5; i++)
178 newdir[i+7] = filename[i];
179 newdir[13] = (uchar)numpgs;
181 // set the directory pages for formatting device depending on the device type
182 switch (SerialNum[portnum][0]) //jpe
185 // check for file too big
188 // set the bitmap in the directory page
189 for (i = 0; i < numpgs; i++)
190 bitacc(WRITE_FUNCTION,1,i+1,&newdir[3]);
193 newdir[12] = (uchar)flpg;
196 // check for file too big
199 // set to external bitmap file
201 // set the bitmap in the first (and only) bitmap page
202 for (i = 0; i < numpgs; i++)
203 bitacc(WRITE_FUNCTION,1,i+2,&bmpg1[0]);
206 newdir[12] = (uchar)flpg; // startpage
208 newdir[3] = 0; // remove local bitmap
209 newdir[5] = 1; // bitmap start page
210 newdir[6] = 1; // bitmap number of pages
213 // check for file too big
216 // set to external bitmap file
218 // set the 3rd bitmap page in the bitmap
219 bitacc(WRITE_FUNCTION,1,2,&bmpg1[0]);
221 // set the bitmap in the first and second bitmap page
222 for (i = 0; i < numpgs; i++)
225 bitacc(WRITE_FUNCTION,1,i+3,&bmpg1[0]);
227 bitacc(WRITE_FUNCTION,1,i-221,&bmpg2[0]);
231 newdir[12] = (uchar)flpg; // startpage
234 newdir[3] = 0; // remove local bitmap
235 newdir[5] = 1; // bitmap start page
236 newdir[6] = 2; // bitmap number of pages
242 // write a dummy directory in page 0 in case we get interrupted
243 if (!owWritePacketStd(portnum,0,dummydir,8,FALSE,FALSE))
246 // loop to write the file in contiguous pages start with flpg
247 cntleft = fllen; // count of bytes left to write
248 pos = 0; // current position in the buffer to write
251 // get a page of data to write
252 for (i = 0; i < ((cntleft > 28) ? 28 : cntleft); i++)
253 pgbuf[i] = buf[pos++];
255 // adjust the bytes left
258 // set the next page pointer
259 pgbuf[i] = (cntleft == 0) ? 0 : flpg+1;
261 // write the page and check to result
262 if (!owWritePacketStd(portnum,flpg,pgbuf,i+1,FALSE,FALSE))
269 // now write the second bitmap page if needed
271 if (!owWritePacketStd(portnum,2,bmpg2,bmpg2len,FALSE,FALSE))
274 // now write the first bitmap page if needed
276 if (!owWritePacketStd(portnum,1,bmpg1,bmpg1len,FALSE,FALSE))
279 // now write the directory page
280 if (!owWritePacketStd(portnum,0,newdir,15,FALSE,FALSE))
283 // success file written