1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
22 // Except as contained in this notice, the name of Dallas Semiconductor
23 // shall not be used except as stated in the Dallas Semiconductor
25 //---------------------------------------------------------------------------
27 // owFile.C: Rudimentary level functions for reading and writing TMEX
28 // files on NVRAM iButton using the packet level functions.
33 // 1.02 -> 1.03 Removed caps in #includes for Linux capatibility
34 // 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
40 //--------------------------------------------------------------------------
41 // Read a TMEX file return it in the provided buffer.
43 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
44 // OpenCOM to indicate the port number.
45 // 'filename' - pointer to five byte filename to read where the
46 // the first four bytes are the name and the fifth is
47 // the extension (0 to 101 decimal).
48 // 'buf' - pointer to a buffer to place the file information into.
49 // This may need to be as large as 7084 bytes.
51 // Supported devices: All devices supported by owReadPacketStd
53 // Returns: >=0 success, number of bytes in the buffer
54 // <0 failed to read the file (error code)
56 int owReadFile(int portnum, uchar *filename, uchar *buf)
58 uchar dirpg=0,pgbuf[32],filepg=0,pglen;
61 // loop read directory pages until the file entry is found
64 // read a directory page
65 pglen = owReadPacketStd(portnum,TRUE,dirpg,pgbuf);
67 // check for reading error
71 // if this is the first page make sure this is a directory
74 ((pgbuf[0] != 0xAA) || (pgbuf[1] != 0) || (pglen < 7)))
79 // loop through each file entry in directory page (page 0 exception)
80 for (i = (dirpg == 0) ? 7 : 0; i < 28; i += 7)
83 if ((filename[0] == pgbuf[i]) &&
84 (filename[1] == pgbuf[i+1]) &&
85 (filename[2] == pgbuf[i+2]) &&
86 (filename[3] == pgbuf[i+3]) &&
87 (filename[4] == (pgbuf[i+4] & 0x7F)) )
89 // get the file starting page number
95 // get the next directory page (from page pointer)
96 dirpg = pgbuf[pglen-1];
98 while (dirpg && (filepg == 0));
100 // check if file found
104 // loop to read the file pages
108 pglen = owReadPacketStd(portnum,TRUE,filepg,pgbuf);
110 // check result of read
114 // append the page data to the buffer
115 for (i = 0; i < (pglen - 1); i++)
116 buf[bufcnt++] = pgbuf[i];
118 // get the next file page (from page pointer)
119 filepg = pgbuf[pglen-1];
123 // return the number of data bytes read
128 //--------------------------------------------------------------------------
129 // Format and Write a TMEX file.
130 // Any previous files will be removed in the Format operation.
131 // The file length 'fllen' can be up to:
132 // 420 bytes for a DS1993
133 // 1736 bytes for a DS1995
134 // 7084 bytes for a DS1996.
136 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
137 // OpenCOM to indicate the port number.
138 // 'filename' - pointer to five byte filename to write where the
139 // the first four bytes are the name and the fifth is
140 // the extension (0 to 101 decimal).
141 // 'buf' - pointer to a buffer containing the file information to write.
143 // Supported devices: DS1993, DS1994, DS1995, DS1996
145 // Returns: TRUE(1) success, device formated and file written
146 // <0 failed to read the file (error code)
149 int owFormatWriteFile(int portnum, uchar *filename, int fllen, uchar *buf)
151 uchar dummydir[] = { 0xAA, 0, 0x80, 0x01, 0, 0, 0, 0 },
152 newdir[] = { 0xAA, 0, 0x80, 0x01, 0, 0, 0, ' ', ' ', ' ', ' ', 0, 1, 1, 0 },
153 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 },
154 bmpg2[] = { 0,0,0,0,0 }, pgbuf[32];
155 int i,numdirpgs,flpg,bmpg1len,bmpg2len,cntleft,pos,numpgs;
157 // calculate the number of pages needed to write the file
158 numpgs = (fllen / 28) + ((fllen % 28) ? 1 : 0);
160 // put the file in the newdirectory
161 for(i = 0; i < 5; i++)
162 newdir[i+7] = filename[i];
163 newdir[13] = (uchar)numpgs;
165 // set the directory pages for formatting device depending on the device type
166 switch (SerialNum[portnum][0]) //jpe
169 // check for file too big
172 // set the bitmap in the directory page
173 for (i = 0; i < numpgs; i++)
174 bitacc(WRITE_FUNCTION,1,i+1,&newdir[3]);
177 newdir[12] = (uchar)flpg;
180 // check for file too big
183 // set to external bitmap file
185 // set the bitmap in the first (and only) bitmap page
186 for (i = 0; i < numpgs; i++)
187 bitacc(WRITE_FUNCTION,1,i+2,&bmpg1[0]);
190 newdir[12] = (uchar)flpg; // startpage
192 newdir[3] = 0; // remove local bitmap
193 newdir[5] = 1; // bitmap start page
194 newdir[6] = 1; // bitmap number of pages
197 // check for file too big
200 // set to external bitmap file
202 // set the 3rd bitmap page in the bitmap
203 bitacc(WRITE_FUNCTION,1,2,&bmpg1[0]);
205 // set the bitmap in the first and second bitmap page
206 for (i = 0; i < numpgs; i++)
209 bitacc(WRITE_FUNCTION,1,i+3,&bmpg1[0]);
211 bitacc(WRITE_FUNCTION,1,i-221,&bmpg2[0]);
215 newdir[12] = (uchar)flpg; // startpage
218 newdir[3] = 0; // remove local bitmap
219 newdir[5] = 1; // bitmap start page
220 newdir[6] = 2; // bitmap number of pages
226 // write a dummy directory in page 0 in case we get interrupted
227 if (!owWritePacketStd(portnum,0,dummydir,8,FALSE,FALSE))
230 // loop to write the file in contiguous pages start with flpg
231 cntleft = fllen; // count of bytes left to write
232 pos = 0; // current position in the buffer to write
235 // get a page of data to write
236 for (i = 0; i < ((cntleft > 28) ? 28 : cntleft); i++)
237 pgbuf[i] = buf[pos++];
239 // adjust the bytes left
242 // set the next page pointer
243 pgbuf[i] = (cntleft == 0) ? 0 : flpg+1;
245 // write the page and check to result
246 if (!owWritePacketStd(portnum,flpg,pgbuf,i+1,FALSE,FALSE))
253 // now write the second bitmap page if needed
255 if (!owWritePacketStd(portnum,2,bmpg2,bmpg2len,FALSE,FALSE))
258 // now write the first bitmap page if needed
260 if (!owWritePacketStd(portnum,1,bmpg1,bmpg1len,FALSE,FALSE))
263 // now write the directory page
264 if (!owWritePacketStd(portnum,0,newdir,15,FALSE,FALSE))
267 // success file written