├── .gitignore ├── LICENSE ├── README.md └── i2csfp.c /.gitignore: -------------------------------------------------------------------------------- 1 | i2csfp 2 | 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # i2csfp 2 | 3 | USE WITH CAUTION!!! 4 | 5 | ## Features 6 | 7 | 1. i2cdump 8 | 2. eeprom dump, showing different pages 9 | 3. edit eeprom data 10 | 4. read/write using protocols: byte, c22m, c22r, c45, rollball 11 | 5. Extract rollball password 12 | 6. Brute force attack eeprom 13 | 14 | It can edit eeprom data of the OEM modules so it can match another string, so you do not need to add another quirk if the same hardware already has a quirk but uses different id string. 15 | 16 | I've only briefly tested it: 17 | 18 | [ericwoud/i2csfp (github.com)](https://github.com/ericwoud/i2csfp) 19 | 20 | It is still in beta-stage. 21 | 22 | I have added pre-build static executables to the release page here. 23 | [https://github.com/ericwoud/i2csfp/releases/tag/0.1](https://github.com/ericwoud/i2csfp/releases/tag/0.1) 24 | 25 | One could do: 26 | ``` 27 | i2csfp sfp-X eepromfix -V Turris -N RTSFP-2.5G -E 0x1e 28 | ``` 29 | For example, to use the OEM module, using the newly added patch for the Turris module, although I still have to try the result myself ;) 30 | 31 | Because of this code in linux kernel, 32 | ``` 33 | bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id) 34 | { 35 | if (id->base.e1000_base_t) 36 | return true; 37 | 38 | if (id->base.phys_id != SFF8024_ID_DWDM_SFP) { 39 | switch (id->base.extended_cc) { 40 | case SFF8024_ECC_10GBASE_T_SFI: 41 | case SFF8024_ECC_10GBASE_T_SR: 42 | case SFF8024_ECC_5GBASE_T: 43 | case SFF8024_ECC_2_5GBASE_T: 44 | return true; 45 | } 46 | } 47 | 48 | return false; 49 | } 50 | ``` 51 | We need to set extended_cc to 0x1e for 2.5G module phy to be recognised. 52 | 53 | ## Build with 54 | ``` 55 | gcc -Wall -o i2csfp i2csfp.c --static 56 | ``` 57 | 58 | ## Usage 59 | ```p 60 | Usage: i2csfp I2CBUS command ... 61 | I2CBUS is one of: 62 | sfp-X for exclusive access (use restore when done) 63 | /dev/i2c-X for shared acces with sfp cage 64 | Command one of: 65 | listsfps Lists active sfp cages 66 | i2cdump Same as the i2cdump command 67 | eepromdump Same as i2cdump, including pages at 0x51 68 | eepromfix Edit eeprom contents 69 | restore Restores sfp cage after exclusive access 70 | byte Byte access on i2c address 71 | c22m Clause 22 MARVELL access on i2c address 72 | c22r Clause 22 ROLLBALL at 0x56 (read-only?) 73 | c45 Clause 45 access on i2c address 74 | rollball Rollball protocol (Clause 45 via 0x51) 75 | gpio Get/set gpio input/ouput 76 | rbpassword Extract Rollball eeprom password 77 | bruteforce Find password using brute force 78 | 79 | i2csfp I2CBUS i2cdump BUS-ADDRESS 80 | BUS-ADDRESS is an integer 0x00 - 0x7f 81 | 82 | i2csfp I2CBUS eepromdump [LASTPAGE] 83 | LASTPAGE is the last page number to show, default 3 84 | 85 | i2csfp I2CBUS eepromfix [-p PASSWORD] [-e EXTCC] [-v VDNAME] [-n VDPN] 86 | -p PASSWORD specify password, without this option uses rbpassword 87 | -V VDNAME specify vendor name 88 | -N VDPN specify vendor pn 89 | -E EXTCC specify extended cc 90 | 91 | i2csfp I2CBUS byte read|write [-p PASSWORD] [-v] BUS-ADDRESS REGISTER [VALUE] 92 | -p PASSWORD specify password 93 | -v verify write 94 | BUS-ADDRESS is an integer 0x00 - 0x7f 95 | REGISTER is an integer 0x00 - 0x7f 96 | VALUE is an integer 0x00 - 0xff 97 | 98 | i2csfp I2CBUS c22m read|write BUS-ADDRESS REGISTER [VALUE] 99 | BUS-ADDRESS is an integer 0x00 - 0x7f 100 | REGISTER is an integer 0x00 - 0x1f 101 | VALUE is an integer 0x00 - 0xffff 102 | 103 | i2csfp I2CBUS c22r read|write BUS-ADDRESS REGISTER [VALUE] 104 | BUS-ADDRESS is an integer 0x00 - 0x7f 105 | REGISTER is an integer 0x00 - 0x1f 106 | VALUE is an integer 0x00 - 0xffff 107 | 108 | i2csfp I2CBUS c45 read|write BUS-ADDRESS DEVAD REGISTER [VALUE] 109 | BUS-ADDRESS is an integer 0x00 - 0x7f 110 | DEVAD is an integer 0x00 - 0x1f 111 | REGISTER is an integer 0x00 - 0xffff 112 | VALUE is an integer 0x00 - 0xffff 113 | 114 | i2csfp I2CBUS rollball read|write DEVAD REGISTER [VALUE] 115 | DEVAD is an integer 0x00 - 0x1f 116 | REGISTER is an integer 0x00 - 0xffff 117 | VALUE is an integer 0x00 - 0xffff 118 | 119 | i2csfp I2CBUS gpio GPIONAME [on|off] 120 | GPIONAME [on|off] one the outputs: tx-disable, rate-select0, rate-select1 121 | GPIONAME one the inputs: mod-def0, los, tx-fault 122 | 123 | i2csfp I2CBUS rbpassword 124 | 125 | i2csfp I2CBUS bruteforce [-p] [MIN] [MAX] 126 | Runs brute force attack on sfp module 127 | -p specify password to start with (last 2 bytes zeroed) 128 | -E specify which attack: 1 (0x50) or 2 (0x56), default 1 129 | MIN is the first byte to try 0x00 - 0xff, default 0x00 130 | MAX is the last byte to try 0x00 - 0xff, default 0xff 131 | ``` 132 | -------------------------------------------------------------------------------- /i2csfp.c: -------------------------------------------------------------------------------- 1 | //SPDX-License-Identifier: GPL-2.0 2 | /* 3 | * i2csfp.c - A user-space program to access sfp module via i2c. 4 | * Copyright (C) 2024 Eric Woudstra 5 | * 6 | * 7 | * Protocol functions based on mdio-i2c.c from linux kernel: 8 | * Copyright (C) 2015-2016 Russell King 9 | * Copyright (C) 2021 Marek Behun 10 | * 11 | * gcc -Wall -o i2csfp i2csfp.c --static 12 | */ 13 | 14 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 15 | 16 | #define EEPROMDELAY 50000 // microseconds 17 | 18 | #define SIZEOFPATH 256 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | int file = -1; 36 | char * i2cname; 37 | char i2cdevname[SIZEOFPATH]; 38 | 39 | /* EEPROM 0x50: 40 | 20 char vendor_name[16]; 41 | 36 u8 extended_cc; fixup sets it to SFF8024_ECC_2_5GBASE_T = 0x1e 42 | 37 char vendor_oui[3]; 43 | 40 char vendor_pn[16]; 44 | */ 45 | 46 | static void help(void) 47 | { 48 | fprintf(stderr, 49 | "Usage:" 50 | " i2csfp I2CBUS command ...\n" 51 | " I2CBUS is one of:\n" 52 | " sfp-X for exclusive access (use restore when done)\n" 53 | " /dev/i2c-X for shared acces with sfp cage\n" 54 | " Command one of:\n" 55 | " listsfps Lists active sfp cages\n" 56 | " i2cdump Same as the i2cdump command\n" 57 | " eepromdump Same as i2cdump, including pages at 0x51\n" 58 | " eepromfix Edit eeprom contents\n" 59 | " restore Restores sfp cage after exclusive access\n" 60 | " byte Byte access on i2c address\n" 61 | " c22m Clause 22 MARVELL access on i2c address\n" 62 | " c22r Clause 22 ROLLBALL at 0x56 (read-only?)\n" 63 | " c45 Clause 45 access on i2c address\n" 64 | " rollball Rollball protocol (Clause 45 via 0x51)\n" 65 | " gpio Get/set gpio input/ouput\n" 66 | " rbpassword Extract Rollball eeprom password\n" 67 | " bruteforce Find password using brute force\n" 68 | "\n" 69 | " i2csfp I2CBUS i2cdump BUS-ADDRESS\n" 70 | " BUS-ADDRESS is an integer 0x00 - 0x7f\n" 71 | "\n" 72 | " i2csfp I2CBUS eepromdump [LASTPAGE]\n" 73 | " LASTPAGE is the last page number to show, default 3\n" 74 | "\n" 75 | " i2csfp I2CBUS eepromfix [-p PASSWORD] [-e EXTCC] [-v VDNAME] [-n VDPN]\n" 76 | " -p PASSWORD specify password, without this option uses rbpassword\n" 77 | " -V VDNAME specify vendor name\n" 78 | " -N VDPN specify vendor pn\n" 79 | " -E EXTCC specify extended cc\n" 80 | "\n" 81 | " i2csfp I2CBUS byte read|write [-p PASSWORD] [-v] BUS-ADDRESS REGISTER [VALUE]\n" 82 | " -p PASSWORD specify password\n" 83 | " -v verify write\n" 84 | " BUS-ADDRESS is an integer 0x00 - 0x7f\n" 85 | " REGISTER is an integer 0x00 - 0x7f\n" 86 | " VALUE is an integer 0x00 - 0xff\n" 87 | "\n" 88 | " i2csfp I2CBUS c22m read|write BUS-ADDRESS REGISTER [VALUE]\n" 89 | " BUS-ADDRESS is an integer 0x00 - 0x7f\n" 90 | " REGISTER is an integer 0x00 - 0x1f\n" 91 | " VALUE is an integer 0x00 - 0xffff\n" 92 | "\n" 93 | " i2csfp I2CBUS c22r read|write BUS-ADDRESS REGISTER [VALUE]\n" 94 | " BUS-ADDRESS is an integer 0x00 - 0x7f\n" 95 | " REGISTER is an integer 0x00 - 0x1f\n" 96 | " VALUE is an integer 0x00 - 0xffff\n" 97 | "\n" 98 | " i2csfp I2CBUS c45 read|write BUS-ADDRESS DEVAD REGISTER [VALUE]\n" 99 | " BUS-ADDRESS is an integer 0x00 - 0x7f\n" 100 | " DEVAD is an integer 0x00 - 0x1f\n" 101 | " REGISTER is an integer 0x00 - 0xffff\n" 102 | " VALUE is an integer 0x00 - 0xffff\n" 103 | "\n" 104 | " i2csfp I2CBUS rollball read|write DEVAD REGISTER [VALUE]\n" 105 | " DEVAD is an integer 0x00 - 0x1f\n" 106 | " REGISTER is an integer 0x00 - 0xffff\n" 107 | " VALUE is an integer 0x00 - 0xffff\n" 108 | "\n" 109 | " i2csfp I2CBUS gpio GPIONAME [on|off]\n" 110 | " GPIONAME [on|off] one the outputs: tx-disable, rate-select0, rate-select1\n" 111 | " GPIONAME one the inputs: mod-def0, los, tx-fault\n" 112 | "\n" 113 | " i2csfp I2CBUS rbpassword\n" 114 | "\n" 115 | " i2csfp I2CBUS bruteforce [-p] [MIN] [MAX]\n" 116 | " Runs brute force attack on sfp module\n" 117 | " -p specify password to start with (last 2 bytes zeroed)\n" 118 | " -E specify which attack: 1 (0x50) or 2 (0x56), default 1\n" 119 | " MIN is the first byte to try 0x00 - 0xff, default 0x00\n" 120 | " MAX is the last byte to try 0x00 - 0xff, default 0xff\n" 121 | ); 122 | } 123 | 124 | static int i2c_transfer(int file, struct i2c_msg * msgs, int count) 125 | { 126 | struct i2c_rdwr_ioctl_data msgset[1]; 127 | int res; 128 | 129 | msgset[0].msgs = msgs; 130 | msgset[0].nmsgs = count; 131 | 132 | res = ioctl(file, I2C_RDWR, &msgset); 133 | if (res < 0) { 134 | fprintf(stderr, "Error: i2c_transfer() failed: %s\n", strerror(errno)); 135 | return -errno; 136 | } 137 | 138 | return res; 139 | } 140 | 141 | static int i2c_write_byte(int file, uint8_t bus_addr, uint8_t reg, uint8_t val) 142 | { 143 | struct i2c_msg msgs[1]; 144 | uint8_t outbuf[2]; 145 | 146 | outbuf[0] = reg; 147 | outbuf[1] = val; 148 | 149 | msgs[0].addr = bus_addr; 150 | msgs[0].flags = 0; 151 | msgs[0].len = sizeof(outbuf); 152 | msgs[0].buf = outbuf; 153 | 154 | return i2c_transfer(file, msgs, ARRAY_SIZE(msgs)); 155 | } 156 | 157 | static int i2c_read_byte(int file, uint8_t bus_addr, uint8_t reg) 158 | { 159 | struct i2c_msg msgs[2]; 160 | uint8_t outbuf[1], inbuf[1]; 161 | int res; 162 | 163 | outbuf[0] = reg; 164 | 165 | msgs[0].addr = bus_addr; 166 | msgs[0].flags = 0; 167 | msgs[0].len = sizeof(outbuf); 168 | msgs[0].buf = outbuf; 169 | 170 | msgs[1].addr = bus_addr; 171 | msgs[1].flags = I2C_M_RD; 172 | msgs[1].len = sizeof(inbuf); 173 | msgs[1].buf = inbuf; 174 | 175 | res = i2c_transfer(file, msgs, ARRAY_SIZE(msgs)); 176 | if (res < 0) return res; 177 | 178 | return inbuf[0]; 179 | } 180 | 181 | 182 | static int i2c_mii_write_default_c22(int file, uint8_t phy_id, uint8_t reg, uint16_t val) 183 | { 184 | struct i2c_msg msgs[1]; 185 | uint8_t data[3]; 186 | int res, bus_addr; 187 | 188 | bus_addr = (phy_id < 0x40) ? phy_id + 0x40 : phy_id; 189 | 190 | data[0] = reg; 191 | data[1] = val >> 8; 192 | data[2] = val; 193 | 194 | msgs[0].addr = bus_addr; 195 | msgs[0].flags = 0; 196 | msgs[0].len = sizeof(data); 197 | msgs[0].buf = data; 198 | 199 | res = i2c_transfer(file, msgs, ARRAY_SIZE(msgs)); 200 | if (res < 0) return res; 201 | 202 | return 0; 203 | } 204 | 205 | static int i2c_mii_read_default_c22(int file, uint8_t phy_id, uint8_t reg) 206 | { 207 | struct i2c_msg msgs[2]; 208 | uint8_t addr[1], data[2]; 209 | int res, bus_addr; 210 | 211 | bus_addr = (phy_id < 0x40) ? phy_id + 0x40 : phy_id; 212 | 213 | addr[0] = reg; 214 | 215 | msgs[0].addr = bus_addr; 216 | msgs[0].flags = 0; 217 | msgs[0].len = sizeof(addr); 218 | msgs[0].buf = addr; 219 | 220 | msgs[1].addr = bus_addr; 221 | msgs[1].flags = I2C_M_RD; 222 | msgs[1].len = sizeof(data); 223 | msgs[1].buf = data; 224 | 225 | res = i2c_transfer(file, msgs, ARRAY_SIZE(msgs)); 226 | if (res < 0) return res; 227 | 228 | return data[0] << 8 | data[1]; 229 | } 230 | 231 | static int i2c_mii_write_default_c45(int file, uint8_t phy_id, uint8_t devad, uint16_t reg, uint16_t val) 232 | { 233 | struct i2c_msg msgs[1]; 234 | uint8_t data[5]; 235 | int res, bus_addr; 236 | 237 | bus_addr = (phy_id < 0x40) ? phy_id + 0x40 : phy_id; 238 | 239 | data[0] = devad; 240 | data[1] = reg >> 8; 241 | data[2] = reg; 242 | data[3] = val >> 8; 243 | data[4] = val; 244 | 245 | msgs[0].addr = bus_addr; 246 | msgs[0].flags = 0; 247 | msgs[0].len = sizeof(data); 248 | msgs[0].buf = data; 249 | 250 | res = i2c_transfer(file, msgs, ARRAY_SIZE(msgs)); 251 | if (res < 0) return res; 252 | 253 | return 0; 254 | } 255 | 256 | static int i2c_mii_read_default_c45(int file, uint8_t phy_id, uint8_t devad, uint16_t reg) 257 | { 258 | struct i2c_msg msgs[2]; 259 | uint8_t addr[3], data[2]; 260 | int res, bus_addr; 261 | 262 | bus_addr = (phy_id < 0x40) ? phy_id + 0x40 : phy_id; 263 | 264 | addr[0] = devad; 265 | addr[1] = reg >> 8; 266 | addr[2] = reg; 267 | 268 | msgs[0].addr = bus_addr; 269 | msgs[0].flags = 0; 270 | msgs[0].len = sizeof(addr); 271 | msgs[0].buf = addr; 272 | 273 | msgs[1].addr = bus_addr; 274 | msgs[1].flags = I2C_M_RD; 275 | msgs[1].len = sizeof(data); 276 | msgs[1].buf = data; 277 | 278 | res = i2c_transfer(file, msgs, ARRAY_SIZE(msgs)); 279 | if (res < 0) return res; 280 | 281 | return data[0] << 8 | data[1]; 282 | } 283 | 284 | #define ROLLBALL_PWD_ADDR 0x7b 285 | #define ROLLBALL_CMD_ADDR 0x80 286 | #define ROLLBALL_DATA_ADDR 0x81 287 | 288 | #define ROLLBALL_CMD_WRITE 0x01 289 | #define ROLLBALL_CMD_READ 0x02 290 | #define ROLLBALL_CMD_DONE 0x04 291 | 292 | static int i2c_transfer_rollball(int file, struct i2c_msg * msgs, int count) 293 | { 294 | int res, main_res, saved_page; 295 | 296 | saved_page = i2c_read_byte(file, 0x51, 0x7f); 297 | if (saved_page < 0) return saved_page; 298 | 299 | res = i2c_write_byte(file, 0x51, 0x7f, 0x03); 300 | if (res < 0) return res; 301 | 302 | main_res = i2c_transfer(file, msgs, count); 303 | 304 | res = i2c_write_byte(file, 0x51, 0x7f, saved_page); 305 | if (res < 0) return res; 306 | 307 | return main_res; 308 | } 309 | 310 | static int i2c_mii_write_rollball(int file, uint8_t devad, uint16_t reg, uint16_t val) 311 | { 312 | struct i2c_msg msgs[2]; 313 | uint8_t buf[6], cmdbuf[2], cmd_addr[1], cmd_res[1]; 314 | int res, i; 315 | 316 | buf[0] = ROLLBALL_DATA_ADDR; 317 | buf[1] = devad; 318 | buf[2] = reg >> 8; 319 | buf[3] = reg; 320 | buf[4] = val >> 8; 321 | buf[5] = val; 322 | 323 | cmdbuf[0] = ROLLBALL_CMD_ADDR; 324 | cmdbuf[1] = ROLLBALL_CMD_WRITE; 325 | 326 | msgs[0].addr = 0x51; 327 | msgs[0].flags = 0; 328 | msgs[0].len = sizeof(buf); 329 | msgs[0].buf = buf; 330 | 331 | msgs[1].addr = 0x51; 332 | msgs[1].flags = 0; 333 | msgs[1].len = sizeof(cmdbuf); 334 | msgs[1].buf = cmdbuf; 335 | 336 | res = i2c_transfer_rollball(file, msgs, ARRAY_SIZE(msgs)); 337 | if (res < 0) return res; 338 | 339 | cmd_addr[0] = ROLLBALL_CMD_ADDR; 340 | 341 | msgs[0].addr = 0x51; 342 | msgs[0].flags = 0; 343 | msgs[0].len = sizeof(cmd_addr); 344 | msgs[0].buf = cmd_addr; 345 | 346 | msgs[1].addr = 0x51; 347 | msgs[1].flags = I2C_M_RD; 348 | msgs[1].len = sizeof(cmd_res); 349 | msgs[1].buf = cmd_res; 350 | 351 | for (i = 0; i < 10; ++i) { 352 | usleep(20000); 353 | 354 | res = i2c_transfer_rollball(file, msgs, ARRAY_SIZE(msgs)); 355 | if (res < 0) return res; 356 | 357 | if (cmd_res[0] == ROLLBALL_CMD_DONE) 358 | return 0; 359 | } 360 | 361 | return -ETIMEDOUT; 362 | } 363 | 364 | static int i2c_mii_read_rollball(int file, uint8_t devad, uint16_t reg) 365 | { 366 | struct i2c_msg msgs[2]; 367 | uint8_t buf[4], cmdbuf[2], cmd_addr[1], cmd_res[6]; 368 | int res, i; 369 | 370 | buf[0] = ROLLBALL_DATA_ADDR; 371 | buf[1] = devad; 372 | buf[2] = reg >> 8; 373 | buf[3] = reg; 374 | 375 | cmdbuf[0] = ROLLBALL_CMD_ADDR; 376 | cmdbuf[1] = ROLLBALL_CMD_READ; 377 | 378 | msgs[0].addr = 0x51; 379 | msgs[0].flags = 0; 380 | msgs[0].len = sizeof(buf); 381 | msgs[0].buf = buf; 382 | 383 | msgs[1].addr = 0x51; 384 | msgs[1].flags = 0; 385 | msgs[1].len = sizeof(cmdbuf); 386 | msgs[1].buf = cmdbuf; 387 | 388 | res = i2c_transfer_rollball(file, msgs, ARRAY_SIZE(msgs)); 389 | if (res < 0) return res; 390 | 391 | cmd_addr[0] = ROLLBALL_CMD_ADDR; 392 | 393 | msgs[0].addr = 0x51; 394 | msgs[0].flags = 0; 395 | msgs[0].len = sizeof(cmd_addr); 396 | msgs[0].buf = cmd_addr; 397 | 398 | msgs[1].addr = 0x51; 399 | msgs[1].flags = I2C_M_RD; 400 | msgs[1].len = sizeof(cmd_res); 401 | msgs[1].buf = cmd_res; 402 | 403 | for (i = 0; i < 10; ++i) { 404 | usleep(20000); 405 | 406 | res = i2c_transfer_rollball(file, msgs, ARRAY_SIZE(msgs)); 407 | if (res < 0) return res; 408 | 409 | if (cmd_res[0] == ROLLBALL_CMD_DONE) 410 | return cmd_res[4] << 8 | cmd_res[5];; 411 | } 412 | 413 | return -ETIMEDOUT; 414 | } 415 | 416 | static int i2c_mii_init_rollball(int file) 417 | { 418 | struct i2c_msg msgs[1]; 419 | uint8_t data[5]; 420 | int res; 421 | 422 | /* 423 | for (i = 0; i < 4; ++i) { 424 | res = i2c_write_byte(file, 0x51, ROLLBALL_PWD_ADDR + i, 0xff); 425 | if (res < 0) return res; 426 | } 427 | */ 428 | data[0] = ROLLBALL_PWD_ADDR; 429 | data[1] = 0xff; 430 | data[2] = 0xff; 431 | data[3] = 0xff; 432 | data[4] = 0xff; 433 | 434 | msgs[0].addr = 0x51; 435 | msgs[0].flags = 0; 436 | msgs[0].len = sizeof(data); 437 | msgs[0].buf = data; 438 | 439 | res = i2c_transfer(file, msgs, ARRAY_SIZE(msgs)); 440 | if (res < 0) return res; 441 | 442 | return 0; 443 | } 444 | 445 | static int checksum(int file, int start, int end, bool fix) 446 | { 447 | int c, res, sum = 0; 448 | 449 | for (c = start; c <= end; c++) { 450 | res = i2c_read_byte(file, 0x50, c); 451 | if (res < 0) return res; 452 | sum = (sum + res) & 0xff; 453 | } 454 | res = i2c_read_byte(file, 0x50, c); 455 | if (res < 0) return res; 456 | if (res == sum) { 457 | printf("Checksum 0x%02x-0x%02x matched %02x\n", start, end, sum); 458 | } else { 459 | printf("Checksum 0x%02x-0x%02x failed, set at %02x, but should be %02x\n", 460 | start, end, res, sum); 461 | if (fix) { 462 | printf("Writing checksum %02x\n", sum); 463 | res = i2c_write_byte(file, 0x50, c, sum); 464 | usleep(EEPROMDELAY); 465 | if (res < 0) return res; 466 | } 467 | } 468 | 469 | return 0; 470 | } 471 | 472 | static int checksums(int file, bool fix) 473 | { 474 | int res; 475 | 476 | res = checksum(file, 0x0, 0x3e, fix); 477 | if (res < 0) return res; 478 | 479 | res = checksum(file, 0x40, 0x5e, fix); 480 | if (res < 0) return res; 481 | 482 | return 0; 483 | } 484 | 485 | static int fillstring(int file, const char *str, int start, int size) 486 | { 487 | int c, res, i=0, val; 488 | 489 | for (c = start; c < (start + size); c++) { 490 | if (i < strlen(str)) val = str[i++]; else val = ' '; 491 | res = i2c_write_byte(file, 0x50, c, val); 492 | usleep(EEPROMDELAY); 493 | if (res < 0) return res; 494 | } 495 | return 0; 496 | } 497 | 498 | static int fillpassword(int file, unsigned int pw) 499 | { 500 | int res; 501 | 502 | res = i2c_write_byte(file, 0x51, 0x7b, (pw>>24) & 0xff); 503 | if (res < 0) return res; 504 | res = i2c_write_byte(file, 0x51, 0x7c, (pw>>16) & 0xff); 505 | if (res < 0) return res; 506 | res = i2c_write_byte(file, 0x51, 0x7d, (pw>> 8) & 0xff); 507 | if (res < 0) return res; 508 | res = i2c_write_byte(file, 0x51, 0x7e, (pw ) & 0xff); 509 | if (res < 0) return res; 510 | 511 | return 0; 512 | } 513 | 514 | static int rbpassword(int file, uint32_t * pw) 515 | { 516 | int res, saved_page; 517 | 518 | saved_page = i2c_read_byte(file, 0x51, 0x7f); 519 | if (saved_page < 0) return saved_page; 520 | 521 | res = i2c_write_byte(file, 0x51, 0x7f, 0x03); 522 | if (res < 0) return res; 523 | 524 | *pw = 0; 525 | res = i2c_read_byte(file, 0x51, 0xfc); 526 | *pw |= res << 24; 527 | res = i2c_read_byte(file, 0x51, 0xfd); 528 | *pw |= res << 16; 529 | res = i2c_read_byte(file, 0x51, 0xfe); 530 | *pw |= res << 8; 531 | res = i2c_read_byte(file, 0x51, 0xff); 532 | *pw |= res; 533 | 534 | return i2c_write_byte(file, 0x51, 0x7f, saved_page); 535 | } 536 | 537 | static void printheader() 538 | { 539 | printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f " 540 | "0123456789abcdef\n"); 541 | } 542 | 543 | static int printline(int file, uint8_t bus_addr, int offs) 544 | { 545 | uint8_t chars[16]; 546 | char str[17]; 547 | int res, i; 548 | 549 | for (i = 0; i < sizeof(chars); i++) { 550 | res = i2c_read_byte(file, bus_addr, (offs + i) & 0xff); 551 | if (res < 0) return res; 552 | chars[i] = res; 553 | if ((res < 32) || (res > 126) ) res = '.'; 554 | str[i] = res; 555 | } 556 | str[i] = 0; 557 | 558 | printf("%02x: " 559 | "%02x %02x %02x %02x " 560 | "%02x %02x %02x %02x " 561 | "%02x %02x %02x %02x " 562 | "%02x %02x %02x %02x " 563 | " %s\n", 564 | offs, 565 | chars[ 0], chars[ 1], chars[ 2], chars[ 3], 566 | chars[ 4], chars[ 5], chars[ 6], chars[ 7], 567 | chars[ 8], chars[ 9], chars[10], chars[11], 568 | chars[12], chars[13], chars[14], chars[15], 569 | str 570 | ); 571 | 572 | return 0; 573 | } 574 | 575 | static int i2cdump(int file, int busaddr) 576 | { 577 | int i; 578 | 579 | printheader(); 580 | for (i = 0; i < 16; i++) 581 | printline(file, busaddr, i<<4); 582 | 583 | return 0; 584 | } 585 | 586 | static int printeeprom(int file, int lastpage) 587 | { 588 | int saved_page, i, j; 589 | 590 | printf("0x50:\n"); 591 | printheader(); 592 | for (i = 0; i < 16; i++) 593 | printline(file, 0x50, i<<4); 594 | printf("0x51:\n"); 595 | printheader(); 596 | for (i = 0; i < 8; i++) 597 | printline(file, 0x51, i<<4); 598 | 599 | saved_page = i2c_read_byte(file, 0x51, 0x7f); 600 | if (saved_page < 0) return saved_page; 601 | 602 | 603 | for (j = 0; j <= lastpage; j++) { 604 | i2c_write_byte(file, 0x51, 0x7f, j); 605 | printf("0x51 PAGE 0x%02x:\n", j); 606 | printheader(); 607 | for (i = 8; i < 16; i++) 608 | printline(file, 0x51, i<<4); 609 | } 610 | 611 | return i2c_write_byte(file, 0x51, 0x7f, saved_page); 612 | } 613 | 614 | static int bfread_1(int file) 615 | { 616 | return i2c_read_byte(file, 0x50, 0x14); 617 | } 618 | static int bfwrite_1(int file, int value) 619 | { 620 | return i2c_write_byte(file, 0x50, 0x14, value); 621 | } 622 | static int bfmod_1(int value) 623 | { 624 | return value ^ 1; 625 | } 626 | static int bfread_2(int file) 627 | { 628 | return i2c_mii_read_default_c22(file, 0x56, 8); 629 | } 630 | static int bfwrite_2(int file, int value) 631 | { 632 | return i2c_mii_write_default_c22(file, 0x56, 8, value); 633 | } 634 | static int bfmod_2(int value) 635 | { 636 | return value ^ 0x20; 637 | } 638 | 639 | static int bruteforcepart(int file, int value, bool check, int min, int max, 640 | int (*bfread)(), int (*bfwrite)(), int (*bfmod)()) 641 | { 642 | unsigned int c,d; 643 | int res; 644 | for (c = min; c <= max; c++) { 645 | // printf("Checking c=%02x\n", c); 646 | res = i2c_write_byte(file, 0x51, 0x7d, c); 647 | if (res < 0) return res; 648 | for (d = min; d <= max; d++) { 649 | res = i2c_write_byte(file, 0x51, 0x7e, d); 650 | if (res < 0) return res; 651 | res = bfwrite(file, value); 652 | if (res < 0) return res; 653 | if (check) { 654 | res = bfread(file); 655 | if (res < 0) return res; 656 | else if (res == value) { 657 | printf("Readback matched at 0xXXXX%02x%02x\n", c, d); 658 | return 1; 659 | } 660 | } 661 | } 662 | } 663 | 664 | if (!check) { 665 | res = bfread(file); 666 | if (res < 0) return res; 667 | else if (res == value) { 668 | printf("Readback matched at SOMEWHERE!\n"); 669 | return 1; 670 | } 671 | } 672 | return 0; 673 | } 674 | 675 | static int runbruteforce(int file, unsigned int start, int min, int max, 676 | int (*bfread)(), int (*bfwrite)(), int (*bfmod)()) 677 | { 678 | unsigned int a,b; 679 | int res, orig; 680 | if (max < min) max = min; 681 | uint8_t sa = start >> 24; if ((sa < min) || (sa > max)) sa = min; 682 | uint8_t sb = start >> 16; if ((sb < min) || (sb > max)) sb = min; 683 | 684 | orig = bfread(file); 685 | if (orig < 0) return orig; 686 | 687 | for (a = sa; a <= max; a++) { 688 | res = i2c_write_byte(file, 0x51, 0x7b, a); 689 | if (res < 0) return res; 690 | for (b = sb; b <= max; b++) { 691 | res = i2c_write_byte(file, 0x51, 0x7c, b); 692 | if (res < 0) return res; 693 | printf("Checking 0x%02x%02xXXXX (%d/%d)\n", a, b, 694 | 1 + (a -min) * (1+max-min) + (b-min), 695 | (1+max-min) * (1+max-min) ); 696 | fflush(stdout); 697 | res = bruteforcepart(file, bfmod(orig), false, min, max, bfread, bfwrite, bfmod); 698 | if (res < 0) { 699 | fprintf(stderr, "Error: bruteforcepart failed\n"); 700 | return res; 701 | } else if (res == 1) { 702 | /* FOUND */ 703 | res = bruteforcepart(file, orig, true, min, max, bfread, bfwrite, bfmod); 704 | if (res < 0) { 705 | fprintf(stderr, "Error: bruteforcepart failed\n"); 706 | return res; 707 | } 708 | return 1; 709 | } 710 | } 711 | } 712 | return 0; 713 | } 714 | 715 | static void exiterror(char * str) 716 | { 717 | if (file >= 0) close(file); 718 | fprintf(stderr, "%s", str); 719 | exit(1); 720 | } 721 | 722 | static void exithelp(char * str) 723 | { 724 | if (file >= 0) close(file); 725 | fprintf(stderr, "%s", str); 726 | help(); 727 | exit(1); 728 | } 729 | 730 | 731 | static int sysreadbe32(char * path) 732 | { 733 | int fd, res; 734 | __be32 num; 735 | 736 | fd = open(path, O_RDONLY); 737 | if (fd < 0) return fd; 738 | res = read(fd, &num, sizeof(num)); 739 | close(fd); 740 | if (res < 0) return res; 741 | return be32toh(num); 742 | } 743 | 744 | static int sysread3be32(char * path, unsigned int * uint1, unsigned int * uint2, unsigned int * uint3) 745 | { 746 | int fd, res; 747 | __be32 num[3]; 748 | 749 | fd = open(path, O_RDONLY); 750 | if (fd < 0) return fd; 751 | res = read(fd, &num, sizeof(num)); 752 | close(fd); 753 | if (res < 0) return res; 754 | 755 | *uint1 = be32toh(num[0]); 756 | *uint2 = be32toh(num[1]); 757 | *uint3 = be32toh(num[2]); 758 | return 0; 759 | } 760 | /* 761 | static int sysreadstring(char * path, char* buf, int len) 762 | { 763 | int fd, res; 764 | 765 | fd = open(path, O_RDONLY); 766 | if (fd < 0) return fd; 767 | res = read(fd, buf, len); 768 | close(fd); 769 | return res; 770 | } 771 | */ 772 | static int syswritestring(char * path, char* buf, int len) 773 | { 774 | int fd, res; 775 | 776 | fd = open(path, O_WRONLY); 777 | if (fd < 0) return fd; 778 | res = write(fd, buf, len); 779 | close(fd); 780 | return res; 781 | } 782 | 783 | static void listsfps() 784 | { 785 | DIR *dirpos; 786 | struct dirent* entry; 787 | 788 | if ((dirpos = opendir("/sys/bus/platform/drivers/sfp")) == NULL) return; 789 | while ((entry = readdir(dirpos)) != NULL) { 790 | if (entry->d_name[0] == '.') continue; 791 | if (!strcmp(entry->d_name, "bind")) continue; 792 | if (!strcmp(entry->d_name, "unbind")) continue; 793 | if (!strcmp(entry->d_name, "uevent")) continue; 794 | if (!strcmp(entry->d_name, "module")) continue; 795 | printf("%s\n", entry->d_name); 796 | } 797 | closedir(dirpos); 798 | return; 799 | } 800 | 801 | static int finddev(char* where, int phandle, char * found, int foundlen) 802 | { 803 | DIR *dirpos; 804 | struct dirent* entry; 805 | char path[SIZEOFPATH+64]; 806 | 807 | if ((dirpos = opendir(where)) == NULL) return -1; 808 | while ((entry = readdir(dirpos)) != NULL) { 809 | if (entry->d_name[0] == '.') continue; 810 | snprintf(path, SIZEOFPATH+64 -1, "%s/%s/of_node/phandle", where, entry->d_name); 811 | if (sysreadbe32(path) == phandle) { 812 | snprintf(found, foundlen, "/dev/%s", entry->d_name); 813 | closedir(dirpos); 814 | return 0; 815 | } 816 | } 817 | closedir(dirpos); 818 | return -1; 819 | } 820 | 821 | static int gpio_input(int fd, int pinnr) 822 | { 823 | struct gpio_v2_line_values values; 824 | struct gpio_v2_line_request req; 825 | int res; 826 | 827 | /* get req.fd */ 828 | memset(&req, 0, sizeof req); 829 | req.offsets[0] = pinnr; 830 | req.num_lines = 1; 831 | req.config.flags = GPIO_V2_LINE_FLAG_INPUT; 832 | req.config.num_attrs = 0; 833 | res = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req); 834 | if (res < 0) return res; 835 | 836 | /* readout */ 837 | values.mask = 1; 838 | res = ioctl(req.fd, GPIO_V2_LINE_GET_VALUES_IOCTL, &values); 839 | close(req.fd); 840 | if (res < 0) return res; 841 | 842 | return values.bits; 843 | } 844 | 845 | static int gpio_output(int fd, int pinnr, int value) 846 | { 847 | struct gpio_v2_line_values values; 848 | struct gpio_v2_line_request req; 849 | int res; 850 | 851 | memset(&req, 0, sizeof req); 852 | 853 | if (value == -1) { 854 | /* get req.fd without changing the value */ 855 | req.offsets[0] = pinnr; 856 | req.num_lines = 1; 857 | req.config.flags = 0; 858 | req.config.num_attrs = 0; 859 | res = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req); 860 | if (res < 0) return res; 861 | } else { 862 | /* get req.fd and set as output with initial value */ 863 | req.offsets[0] = pinnr; 864 | req.num_lines = 1; 865 | req.config.flags = GPIO_V2_LINE_FLAG_OUTPUT; 866 | req.config.num_attrs = 1; 867 | req.config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES; 868 | req.config.attrs[0].mask = 1; 869 | req.config.attrs[0].attr.values = value; 870 | res = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req); 871 | if (res < 0) return res; 872 | } 873 | /* readout */ 874 | values.mask = 1; 875 | res = ioctl(req.fd, GPIO_V2_LINE_GET_VALUES_IOCTL, &values); 876 | if (res >= 0) res = values.bits; 877 | 878 | close(req.fd); 879 | return res; 880 | } 881 | 882 | static int gpio(int file, char * sfpname, char * gpioname, int boolval) 883 | { 884 | char path[SIZEOFPATH]; 885 | char pinctrl[SIZEOFPATH]; 886 | int res, fd; 887 | unsigned int phandle, pinnr, active; 888 | 889 | snprintf(path, SIZEOFPATH-1, "/sys/devices/platform/%s/of_node/%s-gpios", sfpname, gpioname); 890 | res = sysread3be32(path, &phandle, &pinnr, &active); 891 | if (res < 0) return res; 892 | 893 | finddev("/sys/bus/gpio/devices", phandle, pinctrl, SIZEOFPATH); 894 | 895 | printf("%s TEST %x %x %x\n", pinctrl, phandle, pinnr, active); 896 | 897 | fd = open(pinctrl, O_RDWR | O_CLOEXEC); 898 | if (fd < 0) return fd; 899 | 900 | if ( !strcmp(gpioname, "tx-disable") || 901 | !strcmp(gpioname, "rate-select0") || 902 | !strcmp(gpioname, "rate-select1")) { 903 | if ((boolval != -1) && (active & 1)) boolval = boolval ^ 1; 904 | res = gpio_output(fd, pinnr, boolval); 905 | } else if (!strcmp(gpioname, "mod-def0") || 906 | !strcmp(gpioname, "los") || 907 | !strcmp(gpioname, "tx-fault")) { 908 | res = gpio_input(fd, pinnr); 909 | } else { 910 | res = -1; 911 | } 912 | if ((res >= 0) && (active & 1)) res = res ^ 1; 913 | close(fd); 914 | return res; 915 | } 916 | 917 | 918 | int main(int argc, char *argv[]) 919 | { 920 | char *end; 921 | int busaddr, devad, dregister, value = 0; 922 | int opt, res = 0, verify = 0; 923 | const char *password = NULL; 924 | const char *vendorname = NULL; 925 | const char *vendorpn = NULL; 926 | const char *extcc = NULL; 927 | uint32_t password_hex = 0; 928 | int extcc_hex = 0; 929 | char path[SIZEOFPATH]; 930 | 931 | while ((opt = getopt(argc, argv, "p:V:N:E:vh")) != -1) { 932 | switch (opt) { 933 | case 'p': password = optarg; break; 934 | case 'V': vendorname = optarg; break; 935 | case 'N': vendorpn = optarg; break; 936 | case 'E': extcc = optarg; break; 937 | case 'v': verify = 1; break; 938 | case 'h': 939 | case '?': help(); exit(opt == '?'); 940 | } 941 | } 942 | 943 | 944 | 945 | if (password) { 946 | password_hex = strtol(password, &end, 0) & 0xffffffff; 947 | if (*end) exithelp("Error: Password is not a number!\n"); 948 | } 949 | if (extcc) { 950 | extcc_hex = strtol(extcc, &end, 0) & 0xff; 951 | if (*end) exithelp("Error: Ext CC is not a number!\n"); 952 | } 953 | 954 | if (argc == optind + 1) { 955 | if (!strcmp(argv[optind], "listsfps")) { 956 | listsfps(); 957 | exit(0); 958 | } 959 | } 960 | 961 | if (argc < optind + 2) exithelp("Error: Not enough arguments!!\n"); 962 | 963 | if (argv[optind][0] == '/') { 964 | i2cname = argv[optind]; 965 | } else { 966 | snprintf(path, SIZEOFPATH-1, "/sys/devices/platform/%s/of_node/i2c-bus", argv[optind]); 967 | finddev("/sys/bus/i2c/devices", sysreadbe32(path), i2cdevname, SIZEOFPATH); 968 | i2cname = i2cdevname; 969 | syswritestring("/sys/bus/platform/drivers/sfp/unbind", argv[optind], strlen(argv[optind])); 970 | } 971 | 972 | file = open(i2cname, O_RDWR); 973 | if (file < 0) { 974 | fprintf(stderr, "Error: Could not open file " 975 | "`%s': %s\n", argv[optind], strerror(errno)); 976 | exit(1); 977 | } 978 | 979 | if (!strcmp(argv[optind+1], "byte")) { 980 | 981 | if (argc < optind + 5) exithelp("Error: Not enough arguments!!\n"); 982 | 983 | busaddr = strtol(argv[optind+3], &end, 0); 984 | if (*end || busaddr < 0) exithelp("Error: bus address is not a number!\n"); 985 | 986 | dregister = strtol(argv[optind+4], &end, 0); 987 | if (*end || dregister < 0 || dregister > 0xff) exithelp("Error: dregister invalid!\n"); 988 | 989 | if (password) { 990 | res = fillpassword(file, password_hex); 991 | if (res < 0) exiterror("Error: Cannot fill in password!\n"); 992 | } 993 | 994 | if (argv[optind+2][0] == 'r') { 995 | res = i2c_read_byte(file, busaddr, dregister); 996 | if (res < 0) fprintf(stderr, "Error: i2c_read_byte failed\n"); 997 | else printf("0x%02x\n", res); 998 | } else if (argv[optind+2][0] == 'w') { 999 | if (argc < optind + 6) exithelp("Error: Not enough arguments!!\n"); 1000 | 1001 | value = strtol(argv[optind+5], &end, 0); 1002 | if (*end || value < 0 || value > 0xff) exithelp("Error: value invalid!\n"); 1003 | 1004 | res = i2c_write_byte(file, busaddr, dregister, value); 1005 | if (res < 0) fprintf(stderr, "Error: i2c_write_byte failed\n"); 1006 | } 1007 | if (verify) { 1008 | res = i2c_read_byte(file, busaddr, dregister); 1009 | if (res < 0) fprintf(stderr, "Error: i2c_read_byte failed\n"); 1010 | else if (res != value) printf("Warning - data mismatch - wrote " 1011 | "0x%02X, read back 0x%02X\n", value, res); 1012 | else printf("Value 0x%02X written, readback matched\n", value); 1013 | } 1014 | 1015 | if (password) { 1016 | res = fillpassword(file, 0xffffffff); 1017 | if (res < 0) exiterror("Error: Cannot fill in password!\n"); 1018 | } 1019 | 1020 | } else if (!strncmp(argv[optind+1], "c22", 3)) { 1021 | 1022 | if (argc < optind + 5) exithelp("Error: Not enough arguments!!\n"); 1023 | 1024 | busaddr = strtol(argv[optind+3], &end, 0); 1025 | if (*end || busaddr < 0) exithelp("Error: bus address is not a number!\n"); 1026 | 1027 | dregister = strtol(argv[optind+4], &end, 0); 1028 | if (*end || dregister < 0 || dregister > 0x1f) exithelp("Error: dregister invalid!\n"); 1029 | 1030 | /* ROLLBALL 0x56 has slightly different implementation then MARVELL */ 1031 | if (argv[optind+1][3] == 'r') dregister = dregister << 1; 1032 | 1033 | if (argv[optind+2][0] == 'r') { 1034 | res = i2c_mii_read_default_c22(file, busaddr, dregister); 1035 | if (res < 0) fprintf(stderr, "Error: i2c_mii_read_default_c22 failed\n"); 1036 | else printf("0x%04x\n", res); 1037 | } else if (argv[optind+2][0] == 'w') { 1038 | if (argc < optind + 6) exithelp("Error: Not enough arguments!!\n"); 1039 | 1040 | value = strtol(argv[optind+5], &end, 0); 1041 | if (*end || value < 0 || value > 0xffff) exithelp("Error: value invalid!\n"); 1042 | 1043 | res = i2c_mii_write_default_c22(file, busaddr, dregister, value); 1044 | if (res < 0) fprintf(stderr, "Error: i2c_mii_write_default_c22 failed\n"); 1045 | } 1046 | 1047 | } else if (!strcmp(argv[optind+1], "c45")) { 1048 | 1049 | if (argc < optind + 6) exithelp("Error: Not enough arguments!!\n"); 1050 | 1051 | busaddr = strtol(argv[optind+3], &end, 0); 1052 | if (*end || busaddr < 0) exithelp("Error: bus address is not a number!\n"); 1053 | 1054 | devad = strtol(argv[optind+4], &end, 0); 1055 | if (*end || devad < 0 || devad > 0x1f) exithelp("Error: device address is not a number!\n"); 1056 | 1057 | dregister = strtol(argv[optind+5], &end, 0); 1058 | if (*end || dregister < 0 || dregister > 0xffff) exithelp("Error: dregister invalid!\n"); 1059 | 1060 | if (argv[optind+2][0] == 'r') { 1061 | res = i2c_mii_read_default_c45(file, busaddr, devad, dregister); 1062 | if (res < 0) fprintf(stderr, "Error: i2c_mii_read_default_c45 failed\n"); 1063 | else printf("0x%04x\n", res); 1064 | } else if (argv[optind+2][0] == 'w') { 1065 | if (argc < optind + 7) exithelp("Error: Not enough arguments!!\n"); 1066 | 1067 | value = strtol(argv[optind+6], &end, 0); 1068 | if (*end || value < 0 || value > 0xffff) exithelp("Error: value invalid!\n"); 1069 | 1070 | res = i2c_mii_write_default_c45(file, busaddr, devad, dregister, value); 1071 | if (res < 0) fprintf(stderr, "Error: i2c_mii_write_default_c45 failed\n"); 1072 | } 1073 | 1074 | } else if (!strcmp(argv[optind+1], "rollball")) { 1075 | 1076 | if (argc < optind + 5) exithelp("Error: Not enough arguments!!\n"); 1077 | 1078 | devad = strtol(argv[optind+3], &end, 0); 1079 | if (*end || devad < 0 || devad > 0x1f) exithelp("Error: device address is not a number!\n"); 1080 | 1081 | dregister = strtol(argv[optind+4], &end, 0); 1082 | if (*end || dregister < 0 || dregister > 0xffff) exithelp("Error: dregister invalid!\n"); 1083 | 1084 | res = i2c_mii_init_rollball(file); 1085 | if (res < 0) fprintf(stderr, "Error: i2c_mii_init_rollball failed\n"); 1086 | 1087 | if (argv[optind+2][0] == 'r') { 1088 | res = i2c_mii_read_rollball(file, devad, dregister); 1089 | if (res < 0) fprintf(stderr, "Error: i2c_mii_read_rollball failed\n"); 1090 | else printf("0x%04x\n", res); 1091 | } else if (argv[optind+2][0] == 'w') { 1092 | if (argc < optind + 6) exithelp("Error: Not enough arguments!!\n"); 1093 | 1094 | value = strtol(argv[optind+5], &end, 0); 1095 | if (*end || value < 0 || value > 0xffff) exithelp("Error: value invalid!\n"); 1096 | 1097 | res = i2c_mii_write_rollball(file, devad, dregister, value); 1098 | if (res < 0) fprintf(stderr, "Error: i2c_mii_write_rollball failed\n"); 1099 | } 1100 | 1101 | } else if (!strcmp(argv[optind+1], "bruteforce")) { 1102 | 1103 | int min = 0x00, max = 0xff; 1104 | 1105 | if (argc >= optind + 4) { 1106 | min = strtol(argv[optind+2], &end, 0); 1107 | if (*end || min < 0 || min > 0xff) exithelp("Error: MIN invalid!\n"); 1108 | max = strtol(argv[optind+3], &end, 0); 1109 | if (*end || max < 0 || max > 0xff) exithelp("Error: MAX invalid!\n"); 1110 | } 1111 | if (!extcc) { 1112 | res = runbruteforce(file, password_hex, min, max, bfread_1, bfwrite_1, bfmod_1); 1113 | } else if (extcc_hex == 1) { 1114 | res = runbruteforce(file, password_hex, min, max, bfread_1, bfwrite_1, bfmod_1); 1115 | } else if (extcc_hex == 2) { 1116 | res = runbruteforce(file, password_hex, min, max, bfread_2, bfwrite_2, bfmod_2); 1117 | } 1118 | if (res < 0) fprintf(stderr, "Error: bruteforce failed\n"); 1119 | 1120 | } else if (!strcmp(argv[optind+1], "rbpassword")) { 1121 | 1122 | rbpassword(file, &password_hex); 1123 | printf("RollBall Password used: 0x%08x\n", password_hex); 1124 | 1125 | } else if (!strcmp(argv[optind+1], "i2cdump")) { 1126 | 1127 | if (argc < optind + 3) exithelp("Error: Not enough arguments!!\n"); 1128 | 1129 | busaddr = strtol(argv[optind+2], &end, 0); 1130 | if (*end || busaddr < 0) exithelp("Error: bus address is not a number!\n"); 1131 | 1132 | i2cdump(file, busaddr); 1133 | 1134 | } else if (!strcmp(argv[optind+1], "eepromdump")) { 1135 | 1136 | int lastpage = 3; 1137 | 1138 | if (argc >= optind + 3) { 1139 | lastpage = strtol(argv[optind+2], &end, 0); 1140 | if (*end || lastpage < 0 || lastpage > 0xff) exithelp("Error: LASTPAGE invalid!\n"); 1141 | } 1142 | 1143 | printeeprom(file, lastpage); 1144 | 1145 | } else if (!strcmp(argv[optind+1], "eepromfix")) { 1146 | 1147 | checksums(file, false); 1148 | 1149 | if (!password) { 1150 | rbpassword(file, &password_hex); 1151 | printf("RollBall Password used: 0x%08x\n", password_hex); 1152 | } 1153 | 1154 | res = fillpassword(file, password_hex); 1155 | if (res < 0) exiterror("Error: Cannot fill in password!\n"); 1156 | 1157 | if (vendorname) { 1158 | fillstring(file, vendorname, 20, 16); 1159 | printf("Changed Vendor name to: %.16s\n", vendorname); 1160 | 1161 | } 1162 | if (extcc) { 1163 | i2c_write_byte(file, 0x50, 36, extcc_hex); 1164 | usleep(EEPROMDELAY); 1165 | printf("Changed EXT_CC to: 0x%02x\n", extcc_hex); 1166 | } 1167 | if (vendorpn) { 1168 | fillstring(file, vendorpn, 40, 16); 1169 | printf("Changed Vendor PN to: %.16s\n", vendorpn); 1170 | } 1171 | 1172 | checksums(file, true); 1173 | 1174 | fillpassword(file, 0xffffffff); 1175 | 1176 | } else if (!strcmp(argv[optind+1], "gpio")) { 1177 | 1178 | int boolval = -1; 1179 | 1180 | if (argc < optind + 3) exithelp("Error: Not enough arguments!!\n"); 1181 | 1182 | if (argc >= optind + 4) { 1183 | if (argv[optind+3][0] == '0') boolval = 0; 1184 | else if (argv[optind+3][1] == 'f') boolval = 0; 1185 | else if (argv[optind+3][0] == '1') boolval = 1; 1186 | else if (argv[optind+3][1] == 'n') boolval = 1; 1187 | } 1188 | 1189 | res = gpio(file, argv[optind], argv[optind+2], boolval); 1190 | 1191 | if (res < 0) fprintf(stderr, "Error: gpio() failed\n"); 1192 | else printf("%d\n", res); 1193 | 1194 | } else if (!strcmp(argv[optind+1], "restore")) { 1195 | 1196 | if (argv[optind][0] != '/') { 1197 | syswritestring("/sys/bus/platform/drivers/sfp/bind", argv[optind], strlen(argv[optind])); 1198 | } 1199 | } 1200 | 1201 | if (file >= 0) close(file); 1202 | exit(0); 1203 | } 1204 | --------------------------------------------------------------------------------