xref: /aosp_15_r20/external/flashrom/doc/dev_guide/development_guide.rst (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1=================
2Development Guide
3=================
4
5We welcome contributions from every human being, corporate entity or club.
6
7This document describes the rules and recommendations about the development, contribution and review processes.
8
9If you introduce new features (not flash chips, but stuff like partial
10programming, support for new external programmers, voltage handling, etc)
11please **discuss your plans** on the :ref:`mailing list` first. That way, we
12can avoid duplicated work and know about how flashrom internals need to be
13adjusted and you avoid frustration if there is some disagreement about the
14design.
15
16You can `look at the latest flashrom development efforts in Gerrit <https://review.coreboot.org/q/project:flashrom>`_.
17
18Set up the git repository and dev environment
19=============================================
20
21#. Clone git repository
22
23   * If using https: :code:`git clone "https://review.coreboot.org/flashrom"`
24   * If using ssh: :code:`git clone "ssh://<gerrit_username>@review.coreboot.org:29418/flashrom"`
25
26#. Follow the build guidelines to install dependencies :doc:`building_from_source`
27
28#. Install Git hooks: :code:`./util/git-hooks/install.sh`
29
30#. Add upstream as a remote:
31
32   * If using https: :code:`git remote add -f upstream https://review.coreboot.org/flashrom`
33   * If using ssh: :code:`git remote add -f upstream ssh://<gerrit_username>@review.coreboot.org:29418/flashrom`
34
35#. Check out a new local branch that tracks :code:`upstream/master`: :code:`git checkout -b <branch_name> upstream/master`
36
37#. Every patch is required to be signed-off (see also :ref:`sign-off`).
38   Set up your ``user.name`` and ``user.email`` in git config, and don't forget
39   to ``-s`` when creating a commit.
40
41#. See also build guidelines :doc:`building_from_source` and `git docs <https://git-scm.com/doc>`_
42
43Creating your patch
44===================
45
46In short, commit your changes with a descriptive message and remember to sign off
47on the commit (``git commit -s``).
48
49.. _commit-message:
50
51Commit message
52--------------
53
54Commit messages shall have the following format::
55
56    <component>: Short description (up to 72 characters)
57
58    This is a long description. Max width of each line in the description
59    is 72 characters. It is separated from the summary by a blank line. You
60    may skip the long description if the short description is sufficient,
61    for example "flashchips: Add FOO25Q128" to add FOO25Q128 chip support.
62
63    You may have multiple paragraphs in the long description, but please
64    do not write a novel here. For non-trivial changes you must explain
65    what your patch does, why, and how it was tested.
66
67    Finally, follow the sign-off procedure to add your sign-off!
68
69    Signed-off-by: Your Name <your@domain>
70
71Commit message should include:
72
73* Commit title
74* Commit description: explain what the patch is doing, or what it is fixing.
75* Testing information: how did you test the patch.
76* Signed-off-by line (see below :ref:`sign-off`)
77* If applicable, link to the ticket in the bugtracker `<https://ticket.coreboot.org/projects/flashrom>`_
78* Change-Id for Gerrit. If commit message doesn't have Change-Id, you forgot to install git hooks.
79
80.. _sign-off:
81
82Sign-off procedure
83^^^^^^^^^^^^^^^^^^
84
85We employ a similar sign-off procedure as the `Linux kernel developers
86<http://web.archive.org/web/20070306195036/http://osdlab.org/newsroom/press_releases/2004/2004_05_24_dco.html>`_
87do. Add a note such as
88
89:code:`Signed-off-by: Random J Developer <[email protected]>`
90
91to your email/patch if you agree with the Developer's Certificate of Origin 1.1
92printed below. Read `this post on the LKML
93<https://lkml.org/lkml/2004/5/23/10>`_ for rationale (spoiler: SCO).
94
95You must use your known identity in the ``Signed-off-by`` line and in any
96copyright notices you add. Anonymous patches lack provenance and cannot be
97committed!
98
99Developer's Certificate of Origin 1.1
100^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101
102    By making a contribution to this project, I certify that:
103
104    (a) The contribution was created in whole or in part by me and I have
105    the right to submit it under the open source license indicated in the file; or
106
107    (b) The contribution is based upon previous work that, to the best of my
108    knowledge, is covered under an appropriate open source license and I have the
109    right under that license to submit that work with modifications, whether created
110    in whole or in part by me, under the same open source license (unless I am
111    permitted to submit under a different license), as indicated in the file; or
112
113    (c) The contribution was provided directly to me by some other person who
114    certified (a), (b) or (c) and I have not modified it; and
115
116    (d) In the case of each of (a), (b), or (c), I understand and agree that
117    this project and the contribution are public and that a record of the contribution
118    (including all personal information I submit with it, including my sign-off) is
119    maintained indefinitely and may be redistributed consistent with this project or the
120    open source license indicated in the file.
121
122.. note::
123
124   The `Developer's Certificate of Origin 1.1
125   <http://web.archive.org/web/20070306195036/http://osdlab.org/newsroom/press_releases/2004/2004_05_24_dco.html>`_
126   is licensed under the terms of the `Creative Commons Attribution-ShareAlike
127   2.5 License <http://creativecommons.org/licenses/by-sa/2.5/>`_.
128
129Coding style
130------------
131
132Flashrom generally follows Linux kernel style:
133https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst
134
135The notable exception is line length limit. Our guidelines are:
136
137* 80-columns soft limit for most code and comments. This is to encourage simple design and concise naming.
138* 112-columns hard limit. Use this to reduce line breaks in cases where they
139  harm grep-ability or overall readability, such as print statements and
140  function signatures. Don't abuse this for long variable/function names or
141  deep nesting.
142* Tables are the only exception to the hard limit and may be as long as needed
143  for practical purposes.
144
145Our guidelines borrow heavily from `coreboot coding style
146<https://doc.coreboot.org/contributing/coding_style.html>`_ and `coreboot Gerrit
147guidelines <https://doc.coreboot.org/contributing/gerrit_guidelines.html>`_,
148and most of them apply to flashrom as well. The really important part is about
149the :ref:`sign-off procedure <sign-off>`.
150
151We try to **reuse as much code as possible** and create new files only if
152absolutely needed, so if you find a function somewhere in the tree which
153already does what you want, please use it.
154
155Testing a patch
156---------------
157
158We expect the patch to be appropriately tested by the patch owner.
159Please add the testing information in commit message, for example that could be some of these:
160programmer you were using, programmer params, chip, OS, operations you were running
161(read/write/erase/verify), and anything else that is relevant.
162
163.. _working-with-gerrit:
164
165Working with Gerrit
166===================
167
168All of the patches and code reviews need to go via
169`Gerrit on review.coreboot.org <https://review.coreboot.org/#/q/project:flashrom>`_.
170While it is technically possible to send a patch to the mailing list, that patch
171still needs to be pushed to Gerrit by someone. We treat patches on the mailing list as a very
172exceptional situation. Normal process is to push a patch to Gerrit.
173Please read below for instructions and check `official Gerrit documentation <https://gerrit-review.googlesource.com/Documentation/>`_.
174
175Creating an account
176---------------------
177
178#. Go to https://review.coreboot.org/login and sign in using the credentials of
179   your choice.
180#. Edit your settings by clicking on the gear icon in the upper right corner.
181#. Set your Gerrit username (this may be the different from the username of an
182   external account you log in with).
183#. Add an e-mail address so that Gerrit can send notifications to you about
184   your patch.
185#. Upload an SSH public key, or click the button to generate an HTTPS password.
186#. After account created, set either "Full name" or "Display name", it is used by Gerrit
187   for code review emails.
188
189.. _pushing-a-patch:
190
191Pushing a patch
192---------------
193
194Before pushing a patch, make sure it builds on your environment and all unit tests pass (see :doc:`building_from_source`).
195
196To push patch to Gerrit, use the follow command: :code:`git push upstream HEAD:refs/for/main`.
197
198* If using HTTPS you will be prompted for the username and password you
199  set in the Gerrit UI.
200* If successful, the Gerrit URL for your patch will be shown in the output.
201
202There is an option to add a topic to the patch. For one-off standalone patches this
203is not necessary. However if your patch is a part of a larger effort, especially if the
204work involves multiple contributors, it can be useful to mark that the patch belongs
205to a certain topic.
206
207Adding a topic makes it easy to search "all the patches by the topic", even if the patches
208have been authored by multiple people.
209
210To add a topic, push with the command: :code:`git push upstream HEAD:refs/for/master%topic=example_topic`.
211Alternatively, you can add a topic from a Gerrit UI after the patch in pushed
212(on the top-left section) of patch UI.
213
214Checking the CI
215---------------
216
217Every patch needs to get a ``Verified +1`` label, typically from Jenkins. Once the patch is pushed
218to Gerrit, Jenkins is added automatically and runs its build script. The script builds the patch with
219various config options, and runs unit tests (for more details see source code of ``test_build.sh``).
220Then, Jenkins gives the patch ``+1`` or ``-1`` vote, indicating success or fail.
221
222In case of failure, follow Jenkins link (which it adds as a comment to the patch), open Console output,
223find the error and try to fix it.
224
225In addition to building and running unit tests, Jenkins also runs a scan-build over the patch. Ideally
226you should check that your patch does not introduce new warnings. To see scan-build report, follow
227Jenkins link -> Build artifacts -> scan build link for the given run.
228
229Adding reviewers to the patch
230-----------------------------
231
232After pushing the patch, ideally try to make sure there are some reviewers added to your patch.
233
234flashrom has MAINTAINERS file with people registered for some areas of the code. People who
235are in MAINTAINERS file will be automatically added as reviewers if the patch touches that
236area. However, not all areas are covered in the file, and it is possible that for the patch you
237sent no one is added automatically.
238
239If you know someone in the dev community who can help with patch review, add the person(s) you know.
240
241In general, it's a good idea to add someone who has a knowledge of whatever the patch is doing,
242even if the person has not been added automatically.
243
244If you are new, and don't know anyone, and no one has been added automatically: you can add
245Anastasia Klimchuk (aklm) as a reviewer.
246
247Going through code reviews
248--------------------------
249
250You will likely get some comments on your patch, and you will need to fix the comments.
251After doing the work locally, amend your commit ``git commit --amend -s`` and push to Gerrit again.
252Check that Change-Id in commit message stays the same. This way Gerrit knows your change belongs
253to the same patch, and will upload new change as new patchset for the same patch.
254
255After uploading the work, go through comments and respond to them. Mark as Done the ones you done
256and mark them as resolved. If there is something that is impossible to do, or maybe you have more questions,
257or maybe you are not sure what you are asked about: respond to a comment **without marking it as resolved**.
258
259It is completely fine to ask a clarifying questions if you don't understand what the comment is asking you to do.
260If is also fine to explain why a comment can't be done, if you think it can't be done.
261
262The patch reviews may take some time, but please don't get discouraged.
263We have quite high standards regarding code quality.
264
265Initial review should include a broad indication of acceptance or rejection of
266the idea/rationale/motivation or the implementation
267
268In general, reviews should focus on the architectural changes and things that
269affect flashrom as a whole. This includes (but is by no means limited to)
270changes in APIs and types, safety, portability, extensibility, and
271maintainability. The purpose of reviews is not to create perfect patches, but
272to steer development in the right direction and produce consensus within the
273community. The goal of each patch should be to improve the state of the project
274- it does not need to fix all problems of the respective field perfectly.
275
276   New contributors may need more detailed advices and should be told about
277   minor issues like formatting problems more precisely. The result of a review
278   should either be an accepted patch or a guideline how the existing code
279   should be changed to be eventually accepted.
280
281To get an idea whether the patch is ready or not, please check :ref:`merge-checklist`.
282
283If you sent a patch and later lost interest or no longer have time to follow up on code review,
284please add a comment saying so. Then, if any of our maintainers are interested in finishing the work,
285they can take over the patch.
286
287Downloading patch from Gerrit
288-----------------------------
289
290Sometimes you may need to download a patch into your local repository. This can be needed for example:
291
292* if you want to test someone else's patch,
293* if multiple developers are collaborating on a patch,
294* if you are continuing someone else's work, when original author left or unable to continue.
295
296First prepare local repository: sync to head or to desired tag / commit.
297
298Open patch in Gerrit, open "three dot" menu on top-right, open Download patch. Copy Cherry-pick command (pick
299the relevant tab for you: anonymous http / http / ssh) and run the copied command in your local repo.
300
301Now you have the commit locally and can do the testing or futher developing. To upload your local changes,
302push patch to Gerrit again (see :ref:`pushing-a-patch`).
303
304Make sure people involved in the patch agree that you are pushing new version of someone else's patch,
305so this does not come at a surprise for an original author.
306
307Merging patches
308---------------
309
310Merging to branches is limited to the "flashrom developers" group on Gerrit (see also :doc:`/about_flashrom/team`).
311
312The list of requirements for the patch to be ready for merging is below, see :ref:`merge-checklist`.
313Some of the requirements are enforced by Gerrit, but not all of them. In general, a person who clicks
314Submit button is responsible to go through Merge checklist. Code reviewers should be aware of the checklist
315as well.
316
317Patch owners can use the checklist to detect whether the patch is ready for merging or not.
318
319.. _merge-checklist:
320
321Merge checklist
322^^^^^^^^^^^^^^^
323
324#. Every patch has to be reviewed and needs at least one +2 that was not given by the commit's author.
325   Ideally, people who were actively reviewing the patch and adding comments, would be the ones approving it.
326#. If a patch is authored by more than one person (Co-developed-by), each author may +2 the other author's changes.
327#. Patch needs to get Verified +1 vote, typically from Jenkins build bot. This means the patch builds successfully
328   and all unit tests pass.
329#. Commit message should have Signed-off-by line, see :ref:`sign-off` and align with the rest
330   of the rules for :ref:`commit-message`
331#. All the comments need to be addressed, especially if there was a negative vote in the process of review (-1 or -2).
332#. flashrom developers are people from literally all around the planet, and various timezones. We usually wait
333   for 3 days (3 * 24hours) after the patch is fully approved just in case of last minute concerns from all timezones.
334#. In the case of emergency, merging should not take place within less than 24 hours after the review
335   started (i.e. the first message by a reviewer on Gerrit).
336
337To help search for patches which are potential candidates for merging, you can try using this search in Gerrit::
338
339   status:open project:flashrom -is:wip -label:Verified-1 label:Verified+1 -label:Code-Review<0 age:3d is:mergeable is:submittable -has:unresolved
340
341Note the search is not a replacement for Merge checklist, but it can help find candidates for merging.
342
343.. _bugtracker:
344
345Bugtracker
346==========
347
348We have a bugtracker on `<https://ticket.coreboot.org/projects/flashrom>`_.
349Anyone can view tickets, but to be able to create/update/assign tickets you need an account.
350
351Mirrors
352========
353
354The only official repository is https://review.coreboot.org/flashrom ; GitHub and GitLab are just mirrors.
355**Reviewers do not look at pull requests** on mirrors.
356Even if pull requests were automatically transferred to Gerrit,
357requirements such as :ref:`sign-off` still present a problem.
358
359The quickest and best way to get your patch reviewed and merged is by sending
360it to review.coreboot.org (see :ref:`working-with-Gerrit`). Conveniently, you can use your GitHub, GitLab or
361Google account as an OAuth2 `login method <https://review.coreboot.org/login>`_.
362